如何在 Twig 中进行复数翻译? [英] How to translate with pluralization in Twig?
本文介绍了如何在 Twig 中进行复数翻译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用语言文件 (messages.en.xliff
) 中的密钥翻译当前的硬编码文本?
How can I translate the current hardcoded text with the key from the language file (messages.en.xliff
)?
我尝试使用
{% trans %} translation_key{% endtrans %}
没有成功.Symfony 返回此错误
with no success. Symfony returns this error
消息必须是 'ProjectEventsBundle:Default:show_event.html.twig' 中的简单文本
A message must be a simple text in 'ProjectEventsBundle:Default:show_event.html.twig'
500 内部服务器错误 - Twig_Error_Syntax
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 %}
提前致谢.
推荐答案
我找到了解决方案.它有点脏,但它正在工作.如果您找到更好的方法,请不要忘记发布.
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.
{% set noattendee %}{% trans %} no.attendee {% endtrans %}{% endset %}
{% set oneattendee %}{% trans %} one.attendee {% endtrans %}{% endset %}
{% set twoattendees %}{% trans %} two.attendees {% endtrans %}{% endset %}
{% set treeattendees %}{% trans with {'%people%': people} %} tree.attendees {% endtrans %}{% endset %}
{% set manyattendees %}{% trans with {'%people%': people} %} many.attendees {% endtrans %}{% endset %}
{% transchoice count with {
'%noattendee%': noattendee,
'%oneattendee%': oneattendee,
'%twoattendees%': twoattendees,
'%treeattendees%': treeattendees,
'%manyattendees%': manyattendees}
%}
{0} %noattendee%|{1} %oneattendee%|{2} %twoattendees%|{3} %treeattendees%|]3,Inf] %manyattendees%
{% endtranschoice %}
这篇关于如何在 Twig 中进行复数翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文