如何使用Symfony2转换器在twig模板中翻译连接字符串

时间:2022-06-04 06:38:39

I have a translation yml file like this:

我有一个翻译yml文件,如下所示:

tag:
  myfirsttag: Tag number one
  secondtag: Tag number two
  ....

and twig template like

和树枝模板一样

    <select name="tag" required="required">
        {% for tag in tag_list %}
            <option value="{{ tag }}">{{ "tag." ~ tag | trans(domain='mydomain') }}</option>
        {% endfor %}
    </select>

So here is the problem. Items in select are rendered like "tag.myfirsttag", not translated. If I replace "tag." ~ tag with hardcoded string like "tag.myfirsttag" it works well. So obviously it is related to concatenation but official docs doesn't say anything about it.

所以这就是问题所在。 select中的项目呈现为“tag.myfirsttag”,未翻译。如果我替换“标签”。 〜标记有像“tag.myfirsttag”这样的硬编码字符串,效果很好。显然它与连接有关,但官方文档没有说明任何内容。

To be more clear and simple

更清晰简单

I can translate

我可以翻译

{{ "hello.world" | trans(domain='mydomain') }}

but can't translate

但无法翻译

{{ "hello." ~ "world" | trans(domain='mydomain') }}

3 个解决方案

#1


31  

The solution is to put the string into parentheses as described here:

解决方案是将字符串放入括号中,如下所述:

works:

作品:

{{ 'hello.world' | trans }}

doesn't work:

不起作用:

{{ 'hello.' ~ 'world' | trans }}

works:

作品:

{{ ('hello.' ~ 'world') | trans }}

#2


2  

to translate contact strings you have to make this thing:

翻译你必须做的事情的联系人字符串:

{{ ("some string " ~ entity.type ~ " another string")|trans }}

{{(“some string”~entity.type~“another string”)| trans}}

But try writing string to translate like params: eg:

但是尝试编写字符串来翻译像params:例如:

some.funny.string

some.funny.string

#3


0  

Is it an associative array, right? Then you should be looping over key=>value pair

它是一个关联数组,对吧?然后你应该循环key => value对

<select name="tag" required="required">
    {% for key,tag in tag_list %}
      <option value="{{ key }}">{{ tag | trans(domain='mydomain') }}</option>
    {% endfor %}
</select>

Or is your array deeper:

或者你的阵列更深入:

<select name="tag" required="required">
    {% for tag in tag_list %}
      {% for key,value in tag %}
        <option value="{{ key }}">{{ value | trans(domain='mydomain') }}</option>
      {% endfor %}
    {% endfor %}
</select>

#1


31  

The solution is to put the string into parentheses as described here:

解决方案是将字符串放入括号中,如下所述:

works:

作品:

{{ 'hello.world' | trans }}

doesn't work:

不起作用:

{{ 'hello.' ~ 'world' | trans }}

works:

作品:

{{ ('hello.' ~ 'world') | trans }}

#2


2  

to translate contact strings you have to make this thing:

翻译你必须做的事情的联系人字符串:

{{ ("some string " ~ entity.type ~ " another string")|trans }}

{{(“some string”~entity.type~“another string”)| trans}}

But try writing string to translate like params: eg:

但是尝试编写字符串来翻译像params:例如:

some.funny.string

some.funny.string

#3


0  

Is it an associative array, right? Then you should be looping over key=>value pair

它是一个关联数组,对吧?然后你应该循环key => value对

<select name="tag" required="required">
    {% for key,tag in tag_list %}
      <option value="{{ key }}">{{ tag | trans(domain='mydomain') }}</option>
    {% endfor %}
</select>

Or is your array deeper:

或者你的阵列更深入:

<select name="tag" required="required">
    {% for tag in tag_list %}
      {% for key,value in tag %}
        <option value="{{ key }}">{{ value | trans(domain='mydomain') }}</option>
      {% endfor %}
    {% endfor %}
</select>