【问题标题】:How to translate with pluralization in Twig?如何在 Twig 中进行复数翻译?
【发布时间】:2011-09-12 07:47:45
【问题描述】:

如何使用语言文件 (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 %}

提前致谢。

【问题讨论】:

    标签: translation symfony twig


    【解决方案1】:

    这个主题很老了,但我建议你做这样的事情:

    在你的messages.LOCALE.yml中

    you.translaction.key: "{1}1 Comment|]1,Inf]%count% Comments"
    

    在你的树枝模板中

    {% set count = 2 %}
    
    {% transchoice count with {'%count%': count} %}you.translaction.key{% endtranschoice %}
    

    干杯,

    西蒙

    【讨论】:

      【解决方案2】:

      我会使用这样的解决方案:

      messages.en.xliff:

      <trans-unit id="1">
          <source>some.translation.key</source>
          <target>{0} no.attendee|{1} one attendee|{2} two attendees|{3} three attendees|]3,Inf] many attendees</target>
      </trans-unit>
      

      树枝模板:

      {{ 'some.translation.key'|transchoice(count) }}
      

      如果你需要输入一些参数,你应该将它们作为第二个参数传递。

      这是过滤器的原型:

      public function transchoice($message, $count, array $arguments = array(), $domain = "messages", $locale = null)
      

      【讨论】:

      • 使用key时,翻译系统直接选择正确的复数形式。
      【解决方案3】:

      Symfony Documentation找到这个:

      Symfony2 提供专门的 Twig 标签(trans 和 transchoice)来帮助静态文本块的消息翻译:

      {% trans %}Hello %name%{% endtrans %}
      
      {% transchoice count %}
      
      {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
      
      {% endtranschoice %}
      

      transchoice 标签自动从当前上下文中获取 %count% 变量并将其传递给翻译器。此机制仅在您使用遵循 %var% 模式的占位符时才有效。

      【讨论】:

        【解决方案4】:

        我找到了解决方案。它有点脏,但它正在工作。如果您找到更好的方法,请不要忘记发布。

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

        【讨论】:

        • 这是一个黑客。 Nikola Petkanski 写下要走的路。
        • 完全由你决定,但也许删除这个答案?这是相当一致的不喜欢:)
        【解决方案5】:

        多参数示例:

        {{ 'label.policy_expires_in'|transchoice(expiresInDays, {}, 'VopInsPolicyBundle') }}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-16
          • 1970-01-01
          相关资源
          最近更新 更多