使用占位符的Symfony翻译不起作用 [英] Symfony translation with placeholder doesn't work

查看:61
本文介绍了使用占位符的Symfony翻译不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Symfony翻译(5.1.*)翻译我的应用.我使用Symfony 5.1.基本的翻译工作正常,但是我在细枝中的变量方面遇到了问题.

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.

当我这样做时 {%trans with {'%name%':'World'} from'app'%}你好%name%{%endtrans%}

工作正常,结果是 Hello World .但是如果我这样做

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

生成翻译文件,结果为 Hello%name%.

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>

结果再次是 Hello World .

任何人都知道为什么使用变量时翻译文件不起作用?

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

推荐答案

如果您使用的是 ICU消息格式,占位符必须为 {} .如文档所述:

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

在以前的翻译格式中,通常将占位符包装在%(例如%name%).此%字符在ICU中不再有效MessageFormat语法,因此,如果您是从以前的格式升级.

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 %}

如果不需要ICU提供的复数规则,则可以继续使用旧格式,可以重命名翻译文件以删除 + intl-icu 后缀.

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.

这篇关于使用占位符的Symfony翻译不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆