symfony - Use placeholders in translation using tags -
in symfony / twig, use tags using percentages in translated block. example:
hello {{nickname}}
would become
{% trans %}hello %nickname%{% endtrans %}
this works expected. array placeholders pass twig, automatically mapped %placeholder%. no work involved. works php array controller being:
array('nickname' => 'rolandow')
when want use nickname inside translation block, have surround percentages %. unfortunately, doesn't seem work when pass trans
.
now translate whole block of text, using tags. can't figure out how can use tags in translation. so, twig this:
{{ say.hello|trans }}
and translation snippet
<trans-unit id="1"> <source>say.hello</source> <target>hello %nickname%, how doing today? lots-of-text-here</target> </trans-unit>
i got working using in template, feels doing things twice. need put array of placeholder trans function again. example:
{{ say.hello|trans('%nickname%' : nickname) }}
if want use other tags given twig in controller, need pass them translator well. can't pass complete array somehow?
there several questions here let's cover them.
1) twig's behaviour not doctrine query, each parameter must bounded. can pass array contains unused parameters trans
, if don't want specify {'key': 'value', 'key2': 'value2'...}
filter, pass entire array (example: | trans(array)
). that's @luke point.
2) can translate block of texts using several ways, simple {% set %}
. {% set %}
tag can used 2 ways :
{% set var = expression %}
or{% set var1, var2 = expression1, expression2 %}
known , used way: put value inside 1 or several variables.{% set var %}
block of text{% endset %}
allow set entire block of text inside variable. useful if want put block filter (such as,escape
, or in case,trans
).
so translate block of text, you'll like:
{% set variable %} block translate %placeholder% {% endset %} {{ variable | trans(array) }}
anyway, don't see interest of translating whole block in 1 time : use | trans
after property (such say.hello
), , can't imagine xlf/yml translation file such design. if want use translator fulfill placeholders, use twig written job :-)
3) replacing placeholder
%placeholder%
in parameters array's keys : point of twig is: put want placeholder. in such way, if translated sentence contains several %
, can use $something$
, #something#
or something
placeholder.
if array keys not contain %
, need add them, don't have choice. if want on twig file, can create macro job you, , put in file import in base layout.
something :
{% macro trans_pct(property, params) %} {% set newparams = [] } {% if params %} {% key, value in params %} {% set newparams['%' ~ key ~ '%'] = value %} {% endfor %} {% endif %} {{ property | trans(newparams) }} {% endmacro %}
and use {{ _self.trans_pct('hello.say', array) | trim }}
.
notes :
_self
template stored macro (see documentation more details).trim
used because wrote macro indentation , line breaks (that's cleaner read). spaces are, default, printed.
Comments
Post a Comment