b85411
b85411

Reputation: 10040

Transchoice in Symfony translations

I have these PHP translation strings in my language file:

'string.month'                         => '1 month',
'string.months'                        => '%month% months',

And I'm using this in my Twig file:

{% transchoice data.frequencyInMonths() with {'%month%': data.frequencyInMonths()} from "strings" %}
{0,[2,Inf]} string.months|trans({'%month%' : data.frequencyInMonths()})|{1} string.month|raw
{% endtranschoice %}

Obviously this isn't working though. I just want to get string.month used data.frequencyInMonths() == 1, and string.months with the number injected in for everything else (ie. 0 and 2 to Infinity).

How do I do this?

Thanks

Upvotes: 1

Views: 813

Answers (1)

szecsikecso
szecsikecso

Reputation: 301

The simplest way to solve your problem is here:

translate file code:

'string.months':       'one month|%count% months'

twig file code:

{% set count = data.frequencyInMonths() %}
{{ "string.months"|transchoice(count) }}

The complex way, if you need a special 0 month message (none month):

translate file code:

'string.months': '{0} none months|{1} one month|]1,Inf] %count% months'

twig file code is same as the last time:

{% set count = data.frequencyInMonths() %}
{{ "string.months"|transchoice(count) }}

Remark: You don't have use ' character at the first value of the translation, so you can use any time:

string.month: 'something about month'

If your translation(the second value) is simple string. So if the "something about month" string isn't contain any special character, then you can you can use:

string.month: something about month

Upvotes: 1

Related Questions