PHP 带格式化数字的复数 gettext

PHP plural gettext with formatted numbers

我希望能够将(多语言)句子格式化为:

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 提供格式化的数字?

您可以将 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, '.', ','));

这将 return:

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