【问题标题】:Fractional plural forms in react-intl or intl-messageformat (javascript i18n packages)react-intl 或 intl-messageformat 中的小数复数形式(javascript i18n 包)
【发布时间】:2017-04-26 16:00:08
【问题描述】:

我正在使用react-intl 包进行国际化。反过来,该包依赖于intl-messageformat。我一直在搜索这两个包中的文档,但仍然对是否可以将分数变量的消息国际化感到困惑。例如,正如here 所讨论的:

某些语言使用“1.2茶匙s”但“2.1茶匙”的等价物

在使用react-intl/intl-messageformat 时,有没有办法计算分数?到目前为止,我只在文档中看到过整数示例。

【问题讨论】:

    标签: internationalization react-intl messageformat.js


    【解决方案1】:

    使用FormattedNumber 处理小数。例如:

        it('formats numbers with decimal separators', () => {
            const el = <FormattedNumber value={0.1} minimumFractionDigits={2} />;
    
            renderer.render(el, intlProvider.getChildContext());
            expect(renderer.getRenderOutput()).toEqualJSX(
                <span>0.10</span>
            );
        });
    

    对复数使用不同的值:

        it('pluralizes labels in strings', () => {
            const el = (
                <FormattedMessage
                    id="num_emails"
                    defaultMessage="You have 1.{teaspoons, plural, one {# teaspoons} other {# teaspoons}}."
                    values={{
                        teaspoons: 2,
                    }}
                />
          );
    

    和单数:

        it('pluralizes labels in strings', () => {
            const el = (
                <FormattedMessage
                    id="num_emails"
                    defaultMessage="You have 2.{teaspoons, plural, one {# teaspoons} other {# teaspoons}}."
                    values={{
                        teaspoons: 1,
                    }}
                />
          );
    

    参考文献

    【讨论】:

    • 哦,但这不是我要问的!我的问题不是关于如何处理十进制数,而是关于是否可以根据十进制数呈现正确的名词形式(请参阅我引用的示例中的茶匙/茶匙)。
    • 您在编辑后的答案中建议的方法绝对很有趣!我没有想到这一点。但是,这种方法假定您传递的数字始终是小数(并且小数点之前的数字是硬编码的)。有没有更通用的方法,可以处理“1 茶匙”、“2 茶匙”、“1.1 茶匙”和“1.2 茶匙”的情况?
    • 有一个selectFormat,其作用类似于switch/case 语句。有一个 test case 和一个 tutorial 演示了它是如何工作的。
    • 对。但这意味着我可以将一组非常有限的预定义值传递给formatMessage :-(
    • 传递一个基于一些通用算法返回计算值的函数来克服这个限制。有一个 function-as-child pattern 在 react 中涵盖了这一点,并由 react-intl 支持,主要用于富文本。
    猜你喜欢
    • 1970-01-01
    • 2013-01-23
    • 2021-09-22
    • 1970-01-01
    • 2018-11-22
    • 2021-04-25
    • 2020-09-06
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多