简体   繁体   English

react-intl - 访问嵌套消息

[英]react-intl - accessing nested messages

I'm trying to use react-intl package inside an app.我正在尝试在应用程序中使用react-intl包。 The app is rendered on the server so I wrote some code to determine which language to use and serve into IntlProvider .该应用程序在服务器上呈现,因此我编写了一些代码来确定使用哪种语言并将其提供给IntlProvider

Translations were provided in messages.js file and they look like this:翻译在messages.js文件中提供,它们看起来像这样:

export default {
  en: {
    message: '...some message',
    nested: {
      anotherMessage: '...another message',
    }
  }
  de: {
    // ...
  }
}

What I do is something like this:我所做的是这样的:

// import messages from './messages.js'
// Check the locale for the user (based on cookies or other things)
const locale = ...
// Get the required messages
const messagesForLocale= = messages[locale];
// Supply the messages to the IntlProvider
<IntlProvider locale={locale} messages={messagesForLocale}>
  // ...
</IntlProvider>

Then when I use FormattedMessage component I can't access the nested message ( anotherMessage ) with code like this:然后,当我使用FormattedMessage组件时,我无法使用如下代码访问嵌套消息( anotherMessage ):

<FormattedMessage id="nested.anotherMessage" ... />

But message is accessible.但是message是可以访问的。

Any ideas where I made the mistake, or maybe I'm missing something in the whole concept?我犯了错误的任何想法,或者我在整个概念中遗漏了什么?

Since React Intl v2 no longer supports nested messages objects, the messages need to be flattening.由于 React Intl v2 不再支持嵌套消息对象,因此需要将消息展平。

export const flattenMessages = ((nestedMessages, prefix = '') => {
  if (nestedMessages === null) {
    return {}
  }
  return Object.keys(nestedMessages).reduce((messages, key) => {
    const value       = nestedMessages[key]
    const prefixedKey = prefix ? `${prefix}.${key}` : key

    if (typeof value === 'string') {
      Object.assign(messages, { [prefixedKey]: value })
    } else {
      Object.assign(messages, flattenMessages(value, prefixedKey))
    }

    return messages
  }, {})
})

// Use flattenMessages
<IntlProvider locale={locale} messages={flattenMessages(messagesForLocale)}>

refs:参考:

react-intl does not support nested messages anymore. react-intl不再支持嵌套消息。 If you still want to organize your messages that way, you can use the flat library to correct your messages structure beforehand.如果您仍然想以这种方式组织您的消息,您可以使用flat库提前更正您的消息结构。

import flatten from 'flat'

<IntlProvider locale={locale} messages={flatten(messagesForLocale)}>

A contributor of react-intl claims that the main reason for only supporting a flat message structure is: react-intl一位贡献者声称,仅支持平面消息结构的主要原因是:

Simplicity and flexibility is the main reason.简单性和灵活性是主要原因。 With the flat object, people can write whatever message ids/keys they want and they won't be interpreted with special semantics.使用平面对象,人们可以编写他们想要的任何消息 ID/键,并且不会用特殊的语义来解释它们。

View the issue Support nested messages-object comment on GitHub.在 GitHub 上查看问题支持嵌套消息对象评论。

Yes, customization using flattenMessages is the best way I found.是的,使用flattenMessages 进行定制是我发现的最好方法。

Here is the video demo for your reference.这是视频演示供您参考。

https://egghead.io/lessons/react-convert-a-hard-coded-string-using-react-intl-formattedmessage https://egghead.io/lessons/react-convert-a-hard-coded-string-using-react-intl-formattedmessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM