如何在 Twig 中进行复数翻译?
如何使用语言文件 (messages.en.xliff
) 中的密钥翻译当前的硬编码文本?
我尝试使用
{% trans %} translation_key{% endtrans %}
但没有成功。 Symfony 返回此错误
消息必须是“ProjectEventsBundle:Default:show_event.html.twig”中的简单文本
500 内部服务器错误 - Twig_Error_Syntax
{% transchoice count %}
{0} The current hardcoded text|{1} is attending|{2} are attending|]2,Inf] and %count% - 2 others are attending
{% endtranschoice %}
提前致谢。
How can I translate the current hardcoded text with the key from the language file (messages.en.xliff
)?
I tried to use the
{% trans %} translation_key{% endtrans %}
with no success. Symfony returns this error
A message must be a simple text in 'ProjectEventsBundle:Default:show_event.html.twig'
500 Internal Server Error - Twig_Error_Syntax
{% transchoice count %}
{0} The current hardcoded text|{1} is attending|{2} are attending|]2,Inf] and %count% - 2 others are attending
{% endtranschoice %}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将使用这样的解决方案:
message.en.xliff:
twig模板:
如果您需要提出一些参数,则应将它们作为第二个参数传递。
这是过滤器的原型:
I would use a solution like this:
messages.en.xliff:
Twig template:
If you need to put some arguments, you should pass them as second argument.
Here's the prototype of the filter:
这个主题相当古老,但我建议您做类似的事情:
在您的 messages.LOCALE.yml 中
在您的树枝模板中
干杯,
西蒙
This subject is quite old, but I would suggest you to do something like that :
In your messages.LOCALE.yml
In your twig template
Cheers,
Simon
从 Symfony 文档 找到这个:
Symfony2 提供了专门的 Twig 标签(trans 和 transchoice)帮助静态文本块的消息翻译:
transchoice 标签自动从当前上下文获取 %count% 变量并将其传递给翻译器。仅当您使用遵循 %var% 模式的占位符时,此机制才有效。
Found this from Symfony Documentation:
Symfony2 provides specialized Twig tags (trans and transchoice) to help with message translation of static blocks of text:
The transchoice tag automatically gets the %count% variable from the current context and passes it to the translator. This mechanism only works when you use a placeholder following the %var% pattern.
多一个参数的示例:
Example with one more parameter:
我找到了解决方案。它有点脏,但它正在工作。如果您发现更好的方法,请不要忘记发布。
I found a solution. It's a little bit dirty but it's working. If you find a better ways, don't forget to post it.