Symfony translation with placeholder doesn't work

倖福魔咒の 提交于 2021-02-05 09:13:26

问题


I'm trying to translate my app with Symfony translate (5.1.*). I use Symfony 5.1. Basic translation work fine but I have problem with variable in twig.

When I do {% trans with {'%name%': 'World'} from 'app' %}Hello %name%{% endtrans %}

It works fine and the result is Hello World as expected. But if I do

php bin/console translation:update --force en
php bin/console cache:clear

to generate the translation file, the result is Hello %name%.

If in the translation file, I delete this reference:

<trans-unit id="yhpYN0i" resname="Hello %name%">
    <source>Hello %name%</source>
    <target>Hello %name%</target>
</trans-unit>

the result is again Hello World.

Anybody has any idea why the translation file doesn't work when using a variable?


回答1:


If you are using the ICU message format, the placeholders have to be {}. As the documentation states:

In the previous translation format, placeholders were often wrapped in % (e.g. %name%). This % character is no longer valid with the ICU MessageFormat syntax, so you must rename your parameters if you are upgrading from the previous format.

You'd have to change your translation file to:

<target>Hello {name}</target>

And remove % from your twig helper call

{% trans with {'name': 'World'} from 'app' %}Hello %name%{% endtrans %}

To keep using the old format if you don't need the pluralization rules provided by ICU you can rename the translation file to remove the +intl-icu suffix.



来源:https://stackoverflow.com/questions/64528409/symfony-translation-with-placeholder-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!