Translate parts of a string individually using Twig and the i18n extension?

239 views Asked by At

I have a string that looks like

This is a list of all items with number 123456 in United States.

and I want to translate it to Swedish as

Detta är en lista över alla artiklar med nummer 123456 i USA.

The problem is that the number 123456 and the country name United States are generated dynamically, but the string is delivered in its final form to the Twig template (it's used for <meta name="description" ... />.

I already have a .po file with country names that I'm translating, including

English             Swedish
United States       USA 

The generated meta description string can have any combination of numbers and country names, so I can't hardcode any translations for the entire string. Is there any way I can re-use my existing translations for the country names and have them translated inside the meta description string, and have the number just stay as it is? Or do I need to somehow split the string up first, and then translate each part individually?

1

There are 1 answers

2
Guido Flohr On

I am not a PHP programmer, so I use pseude code:

num_items = 42;
country = "United States";
string = sprintf(gettext("... items with number %d in %s"),
                 num_items, gettext(country));

Is that what you are looking for?