我正在尝试在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可以访问。
我犯了错误的任何想法,或者我在整个概念中遗漏了一些东西?