【发布时间】:2016-04-24 22:25:36
【问题描述】:
假设我有一个这样的组件:
<FormattedMessage
id='ui.widget.cycleOffsetSelector.timeCycle.label'
defaultMessage="This {cycle}"
values={{cycle: props.cycle}}
/>
因为我的示例中的props.cycle 实际上是一个时间段,例如day、week、month 等,所以在将它传递到 FormattedMessage 之前,我也需要翻译该文本。在翻译中进行这种翻译的正确方法是什么?
我能想到的最好的:
const intlPeriod = {
day: intl.formatMessage({id: 'timePeriod.week', defaultMessage: 'day'}),
week: intl.formatMessage({id: 'timePeriod.week', defaultMessage: 'week'}),
month: intl.formatMessage({id: 'timePeriod.month', defaultMessage: 'month'}),
};
<FormattedMessage
id='ui.widget.cycleOffsetSelector.timeCycle.label'
defaultMessage="This {cycle}"
values={{cycle: intlPeriod[cycle]}}
/>
这是唯一的方法吗?
我看到在 react-intl 的 node-js 包中,在 lib\locale-data\[lang].js 文件中,已经为 day、week、month 等定义了值。有没有办法访问/直接在我的应用中使用这些字符串?
我正在使用react-intl 的v2 分支。
【问题讨论】:
标签: reactjs react-intl