开发者

How to translate with pluralization in Twig?

开发者 https://www.devze.com 2023-04-04 00:11 出处:网络
How can I translate the current hardcoded t开发者_运维问答ext with the key from the language file (messages.en.xliff)?

How can I translate the current hardcoded t开发者_运维问答ext 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.


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)


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


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.


Example with one more parameter:

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


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 %}
0

精彩评论

暂无评论...
验证码 换一张
取 消