如何在 Twig 中进行复数翻译?

发布于 2024-12-04 11:38:37 字数 501 浏览 1 评论 0原文

如何使用语言文件 (messages.en.xliff) 中的密钥翻译当前的硬编码文本?

我尝试使用

{% trans %} translation_key{% endtrans %}

但没有成功。 Symfony 返回此错误

消息必须是“ProjectEventsBundle:Default:show_event.html.twig”中的简单文本

500 内部服务器错误 - Twig_Error_Syntax

{% transchoice count %}
{0} The current hardcoded text|{1} is attending|{2} are attending|]2,Inf] and %count% - 2 others are attending
{% endtranschoice %}

提前致谢。

How can I translate the current hardcoded text with the key from the language file (messages.en.xliff)?

I tried to use the

{% trans %} translation_key{% endtrans %}

with no success. Symfony returns this error

A message must be a simple text in 'ProjectEventsBundle:Default:show_event.html.twig'

500 Internal Server Error - Twig_Error_Syntax

{% transchoice count %}
{0} The current hardcoded text|{1} is attending|{2} are attending|]2,Inf] and %count% - 2 others are attending
{% endtranschoice %}

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

大海や 2024-12-11 11:38:37

我将使用这样的解决方案:

message.en.xliff:

<trans-unit id="1">
    <source>some.translation.key</source>
    <target>{0} no.attendee|{1} one attendee|{2} two attendees|{3} three attendees|]3,Inf] many attendees</target>
</trans-unit>

twig模板:

{{ 'some.translation.key'|transchoice(count) }}

如果您需要提出一些参数,则应将它们作为第二个参数传递。

这是过滤器的原型:

public function transchoice($message, $count, array $arguments = array(), $domain = "messages", $locale = null)

I would use a solution like this:

messages.en.xliff:

<trans-unit id="1">
    <source>some.translation.key</source>
    <target>{0} no.attendee|{1} one attendee|{2} two attendees|{3} three attendees|]3,Inf] many attendees</target>
</trans-unit>

Twig template:

{{ 'some.translation.key'|transchoice(count) }}

If you need to put some arguments, you should pass them as second argument.

Here's the prototype of the filter:

public function transchoice($message, $count, array $arguments = array(), $domain = "messages", $locale = null)
飘过的浮云 2024-12-11 11:38:37

这个主题相当古老,但我建议您做类似的事情:

在您的 messages.LOCALE.yml 中

you.translaction.key: "{1}1 Comment|]1,Inf]%count% Comments"

在您的树枝模板中

{% set count = 2 %}

{% transchoice count with {'%count%': count} %}you.translaction.key{% endtranschoice %}

干杯,

西蒙

This subject is quite old, but I would suggest you to do something like that :

In your messages.LOCALE.yml

you.translaction.key: "{1}1 Comment|]1,Inf]%count% Comments"

In your twig template

{% set count = 2 %}

{% transchoice count with {'%count%': count} %}you.translaction.key{% endtranschoice %}

Cheers,

Simon

一向肩并 2024-12-11 11:38:37

Symfony 文档 找到这个:

Symfony2 提供了专门的 Twig 标签(trans 和 transchoice)帮助静态文本块的消息翻译:

{% trans %}Hello %name%{% endtrans %}

{% transchoice count %}

{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples

{% endtranschoice %}

transchoice 标签自动从当前上下文获取 %count% 变量并将其传递给翻译器。仅当您使用遵循 %var% 模式的占位符时,此机制才有效。

Found this from Symfony Documentation:

Symfony2 provides specialized Twig tags (trans and transchoice) to help with message translation of static blocks of text:

{% trans %}Hello %name%{% endtrans %}

{% transchoice count %}

{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples

{% endtranschoice %}

The transchoice tag automatically gets the %count% variable from the current context and passes it to the translator. This mechanism only works when you use a placeholder following the %var% pattern.

<逆流佳人身旁 2024-12-11 11:38:37

多一个参数的示例:

{{ 'label.policy_expires_in'|transchoice(expiresInDays, {}, 'VopInsPolicyBundle') }}

Example with one more parameter:

{{ 'label.policy_expires_in'|transchoice(expiresInDays, {}, 'VopInsPolicyBundle') }}
毁梦 2024-12-11 11:38:37

我找到了解决方案。它有点脏,但它正在工作。如果您发现更好的方法,请不要忘记发布。

    {% set noattendee %}{% trans %} no.attendee {% endtrans %}{% endset %}
    {% set oneattendee %}{% trans %} one.attendee {% endtrans %}{% endset %}
    {% set twoattendees %}{% trans %} two.attendees {% endtrans %}{% endset %}
    {% set treeattendees %}{% trans with {'%people%': people} %} tree.attendees {% endtrans %}{% endset %}
    {% set manyattendees %}{% trans with {'%people%': people} %} many.attendees {% endtrans %}{% endset %}

    {% transchoice count with {
        '%noattendee%': noattendee,
        '%oneattendee%': oneattendee,
        '%twoattendees%': twoattendees,
        '%treeattendees%': treeattendees,
        '%manyattendees%': manyattendees}
    %}
        {0}  %noattendee%|{1}  %oneattendee%|{2} %twoattendees%|{3} %treeattendees%|]3,Inf] %manyattendees%
    {% endtranschoice %}

I found a solution. It's a little bit dirty but it's working. If you find a better ways, don't forget to post it.

    {% set noattendee %}{% trans %} no.attendee {% endtrans %}{% endset %}
    {% set oneattendee %}{% trans %} one.attendee {% endtrans %}{% endset %}
    {% set twoattendees %}{% trans %} two.attendees {% endtrans %}{% endset %}
    {% set treeattendees %}{% trans with {'%people%': people} %} tree.attendees {% endtrans %}{% endset %}
    {% set manyattendees %}{% trans with {'%people%': people} %} many.attendees {% endtrans %}{% endset %}

    {% transchoice count with {
        '%noattendee%': noattendee,
        '%oneattendee%': oneattendee,
        '%twoattendees%': twoattendees,
        '%treeattendees%': treeattendees,
        '%manyattendees%': manyattendees}
    %}
        {0}  %noattendee%|{1}  %oneattendee%|{2} %twoattendees%|{3} %treeattendees%|]3,Inf] %manyattendees%
    {% endtranschoice %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文