【问题标题】:How do I create a plural translation using Twig and the i18n extension?如何使用 Twig 和 i18n 扩展创建复数翻译?
【发布时间】:2018-02-06 15:50:37
【问题描述】:

我正在使用带有 Twig 的 Slim PHP 框架和 Twig 扩展 i18n。我需要创建一个复数翻译来显示数组中的消息数量。我正在使用文本编辑器创建 .po 文件,并使用 Poedit 将其编译为 .mo 文件。

这是我的模板:

        {% set count=messages|length %}
        {% trans %}
            Showing the last message.
        {% plural count %}
            Showing the last {{count}} messages.
        {% endtrans %}<br>

这是我的.po 文件(用于瑞典语翻译):

msgid "Showing the last message."
msgid_plural "Showing the last %count messages."
msgstr[0] "Visar det senaste meddelandet."
msgstr[1] "Visar de %count senaste meddelandena."

这不起作用,它给了我

Visar de %count senaste meddelandena.

即使count 是 16。

我哪里错了?

【问题讨论】:

  • 应该是%count% 而不是%countdocumentation 有这个例子:“在 gettext 查找期间,这些占位符被转换。{{ name }} 变为 %name%,因此该字符串的 gettext msgid 将是 Hello %name%!。”
  • @martias 你是对的,只需将%count 替换为%count% 就可以了!我错过了文档中的那一段,我使用的模板是一些在线po-edit-site的演示,在示例中使用了%s,所以我只是假设这是正确的语法。

标签: internationalization twig slim gettext twig-extension


【解决方案1】:

%count 应该是 %count%documentation of the i18n extension 有这个例子:

{% trans %}
    Hello {{ name }}!
{% endtrans %}

在 gettext 查找期间,这些占位符被转换。 {{ name }} 变为 %name%,因此该字符串的 gettext msgid 将是 Hello %name%!

【讨论】:

    【解决方案2】:

    The documentation提出如下用法

    {% transchoice count %}
        {1} Showing the last message.|]1,Inf[ Showing the %count% messages.
    {% endtranschoice %}
    

    一个更复杂的例子:

    {% transchoice count with {'%name%': 'Fabien'} from 'app' %}
        {0} %name%, there are no apples|{1} %name%, there is one apple|]1,Inf[ %name%, there are %count% apples
    {% endtranschoice %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多