0

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?

Magnus
  • 17,157
  • 19
  • 104
  • 189

1 Answers1

0

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?

Guido Flohr
  • 1,871
  • 15
  • 28
  • This is sort of what I ended up doing. I am using `gettext()` directly instead of the Twig translation extension to generate the translated string in my controller, and then I pass it to the view. What I had in mind in the question, though, was if there was a way to get the translation extension to lookup the correct translation for a string that follows a certain pattern, and translate it but copy "variables" (the number in the above example) from the source string to the translated one... guess not though. – Magnus Feb 22 '18 at 15:56
  • I don't really understand your plan. But there are two ways of interpolating variables. Either the "items with number {{ number }} in {{ country }}" form, or the nested gettext invocation from my answer. So your guess is probably right. – Guido Flohr Feb 22 '18 at 16:23