【问题标题】:What does name: ' XXX |||| XXXs', mean in i18n?什么名字:'XXX |||| XXXs',在 i18n 中是什么意思?
【发布时间】:2021-08-12 11:31:48
【问题描述】:

这段代码来自recat-admindemoi18n翻译,

     invoices: {
        name: 'Invoice |||| Invoices',
        fields: {
            date: 'Invoice date',
            customer_id: 'Customer',
            command_id: 'Order',
            date_gte: 'Passed Since',
            date_lte: 'Passed Before',
            total_gte: 'Min amount',
            address: 'Address',
        },
    },

你能解释一下这行是什么意思吗?

name: 'Invoice |||| Invoices'

我从来没有遇到过这样的代码||||

谢谢你:)

【问题讨论】:

标签: typescript react-admin react-i18next


【解决方案1】:

react-admin 使用 Polyglot 作为其默认的 i18n 提供程序。这是您定义消息的单数和复数形式的方式:

name: 'Singular |||| Plural'

在某些语言中,您可能需要两个以上的变体:

name: 'WhenZero |||| WhenOne |||| WhenMoreThanOne'

在 react-admin 中,你可以调用 useTranslate 钩子来获取 translate 函数。此函数接受 2 个参数:

  • 翻译键
  • 要在翻译后的消息中插入的变量对象。

如果您阅读 polyglot 文档,您会看到可以在第二个参数中传递一个特殊变量,称为 smart_count。这个变量被 polyglot 用来返回正确的复数形式的消息。

react-admin 使用此功能来获取正确的标题。在资源名称的情况下,我们为列表页和菜单项显示复数形式,为创建、编辑和显示显示单数形式。

【讨论】:

  • 谢谢!好的,我明白了。但是它怎么知道什么时候用在什么地方呢?
  • 我添加了更多细节
猜你喜欢
  • 1970-01-01
  • 2020-04-07
  • 2010-09-29
  • 2011-08-12
  • 2017-06-11
  • 2018-03-05
  • 2023-03-27
  • 2016-08-17
  • 2010-12-28
相关资源
最近更新 更多