如何使用 defineMessages 方法定义本地化?
How to define localization with defineMessages method?
我想本地化我的组件。我使用 yahoo/react-intl 库。我定义了一些消息
const messages = defineMessages({
greeting: {
id: 'app.home.greeting',
description: 'Message to greet the user.',
defaultMessage: 'Hello!'
},
});
我不知道如何为消息定义可本地化的文本,所以我收到了这个错误
app.js:27785[React Intl] Missing message: "app.home.greeting" for locale: "nl", using default message as fallback.
使用示例
<input type="text" {...firstName} placeholder={intl.formatMessage(messages.greeting)} />
有谁知道如何定义本地化消息?
使用 defineMessages 似乎无法做到这一点。
提前致谢。
defineMessages
旨在与 babel-plugin of react-intl.
配合使用
事实上,它只是在提取代码中某处定义的消息时帮助您。这在大型代码库中很有用,可以使消息 ID 和翻译保持同步。
但是,无论您是否使用defineMessages
,您仍然需要提供这样的翻译:
const translations = {
'en-US': {
'app.home.greeting': 'Hello!',
'app.home.color.description': 'Please choose a color',
},
'en-GB': {
'app.home.greeting': 'Hello!',
'app.home.color.description': 'Please choose a colour',
},
de: {
'app.home.greeting': 'Hallo!',
'app.home.color.description': 'Bitte eine Farbe auswählen',
}
}
<IntlProvider locale="en" messages={translations.en}>
<FormattedMessage
id='app.home.color.description'
description='A task description to choose a color'
defaultMessage='Please choose a color'
/>
<input type="text" {...firstName} placeholder={intl.formatMessage(messages.greeting)} />
</IntlProvider>
当然你不需要把所有东西都包在一个IntlProvider
中。只需在 top-level/app 组件周围添加一个 IntlProvider
。
我想本地化我的组件。我使用 yahoo/react-intl 库。我定义了一些消息
const messages = defineMessages({
greeting: {
id: 'app.home.greeting',
description: 'Message to greet the user.',
defaultMessage: 'Hello!'
},
});
我不知道如何为消息定义可本地化的文本,所以我收到了这个错误
app.js:27785[React Intl] Missing message: "app.home.greeting" for locale: "nl", using default message as fallback.
使用示例
<input type="text" {...firstName} placeholder={intl.formatMessage(messages.greeting)} />
有谁知道如何定义本地化消息? 使用 defineMessages 似乎无法做到这一点。 提前致谢。
defineMessages
旨在与 babel-plugin of react-intl.
事实上,它只是在提取代码中某处定义的消息时帮助您。这在大型代码库中很有用,可以使消息 ID 和翻译保持同步。
但是,无论您是否使用defineMessages
,您仍然需要提供这样的翻译:
const translations = {
'en-US': {
'app.home.greeting': 'Hello!',
'app.home.color.description': 'Please choose a color',
},
'en-GB': {
'app.home.greeting': 'Hello!',
'app.home.color.description': 'Please choose a colour',
},
de: {
'app.home.greeting': 'Hallo!',
'app.home.color.description': 'Bitte eine Farbe auswählen',
}
}
<IntlProvider locale="en" messages={translations.en}>
<FormattedMessage
id='app.home.color.description'
description='A task description to choose a color'
defaultMessage='Please choose a color'
/>
<input type="text" {...firstName} placeholder={intl.formatMessage(messages.greeting)} />
</IntlProvider>
当然你不需要把所有东西都包在一个IntlProvider
中。只需在 top-level/app 组件周围添加一个 IntlProvider
。