0

我正在使用带有 Twig 和翻译扩展 i18n 的 Slim PHP 微框架。

我需要输出翻译后的国家/地区名称,并且更愿意使用翻译扩展来完成它,而不是构建一个数组并在 PHP 中手动获取名称。

所以我在我的 Twig 模板中尝试了这个:

{% set country="trans-country-name("~isoCountryCode~")" %}
{% trans %}
    This is a list of all the numbers in {{country}}.    
{% endtrans %}

这在我的.po文件中:

msgid "This is a list of all the numbers in %country%."
msgstr "Detta är en lista över samtliga nummer i %country%."

msgid "trans-country-name(NO)"
msgstr "Norge"

我希望这会导致

"Detta är en lista över samtliga nummer i Norge."

但相反,我只是得到

"Detta är en lista över samtliga nummer i trans-country-name(NO)."

是否可以将翻译扩展解析trans-country-name(NO)为要翻译的文本?

4

1 回答 1

0

我想我是在倒退。事后看来,解决方案似乎很明显——只需先| trans对变量使用过滤器country,然后直接在要翻译的文本中使用它。

树枝模板:

{% set country = ("trans-country-name("~numbers[0].countryNames.code~")") | trans %}
{% trans %}This is a list of all the numbers in {{country}}.{% endtrans %}

.po文件

msgid "This is a list of all the numbers in %country%."
msgstr "Detta är en lista över samtliga telefonnummer i %country%."

msgid "trans-country-name(NO)"
msgstr "Norge"

我的最终解决方案是取消国家代码,而直接使用国家名称:

{% set country=numbers[0].countryNames.displayName | trans %}
{% trans %}This is a list of all the numbers in {{country}}.{% endtrans %}

 

msgid "This is a list of all the numbers in %country%."
msgstr "Detta är en lista över samtliga telefonnummer i %country%."

msgid "Norway"
msgstr "Norge"
于 2018-02-07T07:25:01.903 回答