Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use placeholders in translation using tags

In Symfony / Twig, I could use tags by using percentages in my translated block. For example:

Hello {{nickname}}

would become

{% trans %}Hello %nickname%{% endtrans %}

This works as expected. The array with placeholders that I pass to Twig, are automatically mapped to %placeHolder%. No extra work involved. So this works with my PHP array from the controller being:

Array('nickname' => 'rolandow')

When I want to use the nickname inside the translation block, all I have to do is surround it with percentages %. Unfortunately, this doesn't seem to work when I pass it to trans.

Now I would like to translate a whole block of text, using tags. I can't figure out how I can use the tags in my translation. So, my twig would look something like this:

{{ say.hello|trans }}

And my translation snippet

<trans-unit id="1">
  <source>say.hello</source>
  <target>Hello %nickName%, how are you doing today? lots-of-text-here</target>
</trans-unit>

I got it working by using this in my template, but it feels like doing things twice. I now need to put the array of placeholder into the trans function again. For example:

{{ say.hello|trans('%nickName%' : nickName) }}

If I want to use other tags that are given to twig in my controller, I need to pass them to the translator as well. Can't I just pass the complete array somehow?

like image 739
rolandow Avatar asked Apr 05 '13 13:04

rolandow


People also ask

What are tags and placeholders?

Represents inline HTML in source content. Tags do not need to be translated, simply inserted into the correct place within the target. By default, HTML tag attributes are captured as part of the tag itself and therefore are not translatable.

What are tags in translation?

The formatting commands are referred to as tags, and when placed correctly they decide how the translated text will look like in final format. In most cases, the translator places the tags in the corresponding places in his translation so they more or less match the tag placement in the original.

What is a placeholder in localization?

Placeholders are words in a string that should not be translated, but should be included in the translation using the same syntax.

What is a placeholder in Java?

A Placeholder is a predefined location in a JSP that displays a single piece of web content at a time that is dynamically retrieved from the BEA Virtual Content Repository.


1 Answers

{{ say.hello|trans('%nickname%': 'rolandow') }}

like image 159
Amit Bariya Avatar answered Oct 12 '22 00:10

Amit Bariya