Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

translation - How to translate with pluralization in Twig?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...