如何在 transchoice() 中使用标签

How use labels in transchoice()

我需要根据性别和数量翻译法语短语。 在 message.fr.yml 中,我有:

key.toto: "male_singular: tu es venu 1 fois donc 1 vie|female_singular: tu es venue 1 fois donc 1 vie|male_plural: tu es venu %count% fois donc %count% vies|male_singular: tu es venue %count% fois donc %count% vies"

我在 Symfony\Component\Translation\MessageSelector 的文档中看到 transchoice()

The indexed solution can also contain labels (e.g. one: There is one apple).

This is purely for making the translations more clear - it does not affect the functionality.

The two methods can also be mixed:

{0} There are no apples|one: There is one apple|more: There are %count% apples

但是当我用这样的调用调用 transchoice 时

transchoice("key.toto", "female_singular", array(), "messages")

我有:

"tu es venu 1 fois donc 1 vie"

而不是:

"tu es venue 1 fois donc 1 vie"

我更喜欢使用标签而不是 {},因为消息文件由另一家企业翻译。

有什么想法吗?

来自 Translation 组件 documentation:

The tags are really only hints for translators and don't affect the logic used to determine which plural form to use.

As tags are optional, the translator doesn't use them (the translator will only get a string based on its position in the string).

打开transChoice()方法的the source code可以确定:

public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
    ...

    return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
}

如您所见,transChoice()$number 参数转换为 int。

Manual 表示 PHP"female_singular" 转换为 0:

<?php

echo (int) "female_singular"; //0

因此,当您未在转换选择字符串中指定范围时,Translation 组件会根据字符串位置进行选择。

在你的例子中(number = 0)它是字符串:"tu es venu 1 fois donc 1 vie".