【问题标题】:PHP plural gettext with formatted numbers带有格式化数字的PHP复数gettext
【发布时间】:2015-12-08 10:52:23
【问题描述】:

我希望能够格式化(多语言)句子,例如:

I have 12,345 widgets.

在我的 .po 中有

msgid "I only have %d widget."
msgid_plural "I have %d widgets."
msgstr[0] "I don't have any widgets."
msgstr[1] "I only have %d widget."
msgstr[2] "I have %d widgets."

ngettext("I only have %d widget.", "I have %d widgets.", 12345);

如果我使用 number_format(12345),我会返回一串 "12,345",它不能用于检测复数 (the docs say that it must be an int)。

有没有办法让gettext 提供一个格式化的号码?

【问题讨论】:

    标签: php multilingual gettext


    【解决方案1】:

    您可以将sprintfnumber_format 结合使用,例如:

    sprintf(ngettext("I only have %s widget.", "I have %s widgets.", 1), number_format(1, 0, '.', ','));
    sprintf(ngettext("I only have %s widget.", "I have %s widgets.", 999), number_format(999, 0, '.', ','));
    sprintf(ngettext("I only have %s widget.", "I have %s widgets.", 1000), number_format(1000, 0, '.', ','));
    

    这将返回:

    I only have 1 widget.
    I have 999 widgets.
    I have 1,000 widgets.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多