dongqiao3214 2014-10-19 08:31
浏览 12
已采纳

ZF2复数转换器,为什么是阵列?

In zf2 you have the view helper translatePlural . Which can be use as followed;

echo $this->translatePlural('car', 'cars', $num);

So when $num == 1 it says car else cars (depending on the plural rule but for now use the english rule).

To translate this, by file. You have to use an array like;

'car' => [
  'auto',
  'autos',
],

So now you get auto if $num == 1 else autos.

If you have some where else in your code to translate car you could do translate('car') but that will return to a notice array to string coverstion, which is obvious not what we want.

So to be able to also translate just car and use the plural I now have to modify the code to something like;

echo $this->translatePlural('car_plural', 'cars', $num);

And the language config;

'car_plural' => [
  'auto',
  'autos',
],
'car' => 'auto',

And you need to have to add an english translation for car_plural. So the translatePlural default messages become prety much useless in a default setup, unless you use translatePlural not for translation but for just to choose between car or cars and not translating it.

But if instead the translatePlural just looked at $num then use the pluralRule to choose between singular (car) or plural (cars). Then use the translate helper to translate the singular or plural, you can have an easy config like and the default message all of sudden work again;

'car' => 'auto',
'cars' => 'autos',

No weird keys, you don't need english translation's or something else weird.

So the question /tldr;

Why does ZF2 plural uses an array and not just check which one to use and translate that one? Seems to me a way better method.

  • 写回答

1条回答 默认 最新

  • douyi1944 2015-03-10 00:18
    关注

    I, too, was baffled by this. After some assistance from a coworker and a lot of research, I found that there was a sound architectural reason for this. (I know! I'm surprised, too!)

    It turns out that some languages have a different definition of singular vs plural. Some only have 1 form that serves as both singular and plural, and others have 3 or more plural forms.

    The translatePlural method can't satisfy these other language forms, so instead it references the Gettext way of handling this. In a gettext file, plural translations for English are handled like this:

    msgid "Found %d Result"
    msgid_plural "Found %d Results"
    msgstr[0] "Found %d Result"
    msgstr[1] "Found %d Results"
    

    So the singular/plural values are used to look up the array by the msgid, and then the language's plural index is used to choose either msgstr[0] or msgstr[1].

    In a language that has 3 plural forms, the localization would look like this:

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

    So this is why Zend uses an array, and why just flipping between $singular and $plural is insufficient. (I posted a version of this answer on my blog, but I don't know if it's kosher to link blogs in answers)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?