Php ZF2复数转换器,为什么是数组?

Php ZF2复数转换器,为什么是数组?,php,zend-framework2,zend-translate,Php,Zend Framework2,Zend Translate,在zf2中,您拥有视图帮助器。可按如下方式使用 echo $this->translatePlural('car', 'cars', $num); 因此,当$num==1时,它表示car否则cars(取决于复数规则,但现在使用英语规则) 要翻译这个,请按文件。您必须使用类似的数组 'car' => [ 'auto', 'autos', ], 现在,如果$num==1elseautos,您将获得auto 如果您的代码中还有其他地方需要翻译car,您可以执行translate

zf2
中,您拥有视图帮助器。可按如下方式使用

echo $this->translatePlural('car', 'cars', $num);
因此,当
$num==1
时,它表示
car
否则
cars
(取决于复数规则,但现在使用英语规则)

要翻译这个,请按文件。您必须使用类似的数组

'car' => [
  'auto',
  'autos',
],
现在,如果
$num==1
else
autos
,您将获得
auto

如果您的代码中还有其他地方需要翻译
car
,您可以执行
translate('car')
,但这将返回到一个通知
数组,用于字符串coverstion
,这显然不是我们想要的

因此,为了能够同时翻译
car
并使用复数形式,我现在必须将代码修改为类似的内容

echo $this->translatePlural('car_plural', 'cars', $num);
语言配置

'car_plural' => [
  'auto',
  'autos',
],
'car' => 'auto',
您需要为
car\u plural
添加英文翻译。因此,
translatePlural
默认消息在默认设置中变得非常无用,除非您使用
translatePlural
不是为了翻译,而是为了在
car
cars
之间进行选择而不翻译它

但是,如果只查看了
translatePlural
num
,则使用
pluralRule
在单数(car)或复数(cars)之间进行选择。然后使用
translate
helper来翻译单数或复数,你可以有一个简单的配置,就像和默认的消息一样,再次突然工作

'car' => 'auto',
'cars' => 'autos',
没有奇怪的钥匙,你不需要英文翻译或其他奇怪的东西

因此,问题/tldr


为什么ZF2复数使用数组,而不只是检查要使用哪个数组并转换该数组?在我看来,这是一种更好的方法。

我也对此感到困惑。在一位同事的帮助和大量研究之后,我发现这有一个合理的架构原因。(我知道!我也很惊讶!)

事实证明,有些语言对单数和复数有不同的定义。有些只有一种形式同时用作单数和复数,而有些则有三种或更多的复数形式

translatePlural方法无法满足这些其他语言形式,因此它引用了处理此问题的Gettext方法。在gettext文件中,英语的复数翻译如下:

msgid "Found %d Result"
msgid_plural "Found %d Results"
msgstr[0] "Found %d Result"
msgstr[1] "Found %d Results"
msgid "Found %d Result"
msgid_plural "Found %d Results"
msgstr[0] "singular translation"
msgstr[1] "plural translation"
msgstr[2] "super plural translation"
因此,msgid使用单数/复数值查找数组,然后使用语言的复数索引选择msgstr[0]或msgstr[1]

在有3种复数形式的语言中,本地化如下所示:

msgid "Found %d Result"
msgid_plural "Found %d Results"
msgstr[0] "Found %d Result"
msgstr[1] "Found %d Results"
msgid "Found %d Result"
msgid_plural "Found %d Results"
msgstr[0] "singular translation"
msgstr[1] "plural translation"
msgstr[2] "super plural translation"

这就是Zend使用数组的原因,也是为什么仅仅在$singular和$multilar之间切换是不够的。(我在我的博客上发布了这个答案的一个版本,但我不知道在答案中链接博客是否合乎犹太教原则)

看看这个讨论: