带有格式化数字的PHP复数gettext

时间:2015-12-08 10:52:23

标签: php multilingual gettext

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

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",但不能用于检测复数(CameraCaptureSession.StateCallback)。

有没有办法让gettext提供格式化数字?

1 个答案:

答案 0 :(得分: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.