【发布时间】:2018-01-28 17:39:21
【问题描述】:
我正在尝试在应用程序中使用 react-intl 包。该应用程序在服务器上呈现,因此我编写了一些代码来确定使用哪种语言并将其提供给IntlProvider。
messages.js 文件中提供了翻译,它们看起来像这样:
export default {
en: {
message: '...some message',
nested: {
anotherMessage: '...another message',
}
}
de: {
// ...
}
}
我做的是这样的:
// 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>
然后当我使用FormattedMessage 组件时,我无法使用如下代码访问嵌套消息(anotherMessage):
<FormattedMessage id="nested.anotherMessage" ... />
但是message 是可以访问的。
我犯了错误的任何想法,或者我在整个概念中遗漏了什么?
【问题讨论】:
标签: javascript reactjs react-intl