请使用ngettext 函数处理此类问题。它
允许您正确处理英语和其他语言中的复数
语言,一劳永逸。你可以这样使用它:
printf(ngettext("%d Comment", "%d Comments", $numComments), $numComments);
ngettext 函数将返回第一个格式字符串 ("%d
Comment") 如果只有一个注释和第二种格式
字符串 ("%d Comments") 如果还有更多。 printf 函数将
然后将数字放入字符串中。
这可能看起来需要做很多工作,但它非常强大:它有效
对于具有一种以上复数形式(!)的语言——它们
实际存在。 PHP手册给出了“window”这个词的例子
在一些异国情调中变成“1 okno”、“2 okna”和“5 oken”
我不认识的语言...
如果您因此使用ngettext,那么您未来的用户
来自遥远国家的人会非常感谢你:-)
编辑: 正如 cmets 中所建议的,有一个函数可以
执行上述操作:
function pluralize($num, $singleWord, $pluralWord) {
return printf(ngettext($singleWord, $pluralWord, $num), $num);
}
默认情况下,xgettext 不会识别这个新功能,但你可以
使用--keyword 标志添加它。给定一个文件test.php 与
echo ngettext("foo", "foos", 1);
echo pluralize(2, "bar", "bars");
你可以提取字符串
xgettext --keyword=pluralize:2,3 test.php
生成的messages.po 文件包含如下条目:
#: test.php:7
msgid "foo"
msgid_plural "foos"
msgstr[0] ""
msgstr[1] ""
#: test.php:8
msgid "bar"
msgid_plural "bars"
msgstr[0] ""
msgstr[1] ""
译者将填写每个复数形式并正确
在消息目录标题中形成“Plural-Forms”行,您将
能够支持所有语言。