0

I'm using the Slim PHP micro framework with Twig and the translation extension i18n.

I have a need to output translated country names, and would prefer to do it using the translation extension rather than building an array and fetching the name manually in PHP.

So I tried this in my Twig template:

{% set country="trans-country-name("~isoCountryCode~")" %}
{% trans %}
    This is a list of all the numbers in {{country}}.    
{% endtrans %}

and this in my .po file:

msgid "This is a list of all the numbers in %country%."
msgstr "Detta är en lista över samtliga nummer i %country%."

msgid "trans-country-name(NO)"
msgstr "Norge"

I was hoping that this would result in

"Detta är en lista över samtliga nummer i Norge."

but instead I just get

"Detta är en lista över samtliga nummer i trans-country-name(NO)."

Is it possible to have the translation extension parse trans-country-name(NO) as text to be translated?

Magnus
  • 17,157
  • 19
  • 104
  • 189

1 Answers1

0

I think I was going about it sort of backwards. The solution seems obvious in hindsight - simply use the | trans filter on the country variable first, and then just use that straight in the text to be translated.

Twig template:

{% set country = ("trans-country-name("~numbers[0].countryNames.code~")") | trans %}
{% trans %}This is a list of all the numbers in {{country}}.{% endtrans %}

.po file

msgid "This is a list of all the numbers in %country%."
msgstr "Detta är en lista över samtliga telefonnummer i %country%."

msgid "trans-country-name(NO)"
msgstr "Norge"

My final solution was to scrap the country code in favour of simply using the country names directly instead:

{% set country=numbers[0].countryNames.displayName | trans %}
{% trans %}This is a list of all the numbers in {{country}}.{% endtrans %}

 

msgid "This is a list of all the numbers in %country%."
msgstr "Detta är en lista över samtliga telefonnummer i %country%."

msgid "Norway"
msgstr "Norge"
Magnus
  • 17,157
  • 19
  • 104
  • 189