【问题标题】:Can I use React-Intl's Built-In Names for Time Periods in my App?我可以在我的应用程序中使用 React-Intl 的内置名称来表示时间段吗?
【发布时间】:2016-04-24 22:25:36
【问题描述】:

假设我有一个这样的组件:

<FormattedMessage
    id='ui.widget.cycleOffsetSelector.timeCycle.label'
    defaultMessage="This {cycle}"
    values={{cycle: props.cycle}}
/>

因为我的示例中的props.cycle 实际上是一个时间段,例如dayweekmonth 等,所以在将它传递到 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 文件中,已经为 dayweekmonth 等定义了值。有没有办法访问/直接在我的应用中使用这些字符串?

我正在使用react-intlv2 分支。

【问题讨论】:

    标签: reactjs react-intl


    【解决方案1】:

    ui.widget.cycleOffsetSelector.timeCycle.label 消息应该是这样的:

    en-US
    
    This {cycle, select,
        day {day}
        week {week}
        month {month}
    }
    
    es-US
    
    {cycle, select,
        day {Este día}
        week {Esta semana}
        month {Este mes}
    }
    

    这允许西班牙语版本根据 cycle 的值在阳性和阴性 Este/Etsa 之间正确切换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      • 2023-03-04
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2018-08-25
      相关资源
      最近更新 更多