简体   繁体   English

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

[英]How to translate concatenated string in twig template using Symfony2 translator

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. select中的项目呈现为“tag.myfirsttag” ,未翻译。 If I replace "tag." ~ tag 如果我替换"tag." ~ tag "tag." ~ tag with hardcoded string like "tag.myfirsttag" it works well. "tag." ~ tag硬编码字符串,如"tag.myfirsttag"它运作良好。 So obviously it is related to concatenation but official docs doesn't say anything about it. 显然它与连接有关,但官方文档没有说明任何内容。

To be more clear and simple 要更加清晰和简单

I can translate 我可以翻译

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

but can't translate 但无法翻译

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

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

to translate contact strings you have to make this thing: 翻译你必须做的事情的联系人字符串:

{{ ("some string " ~ entity.type ~ " another string")|trans }} {{(“some string”~entity.type~“另一个字符串”)| trans}}

But try writing string to translate like params: eg: 但是尝试编写字符串来翻译像params:例如:

some.funny.string some.funny.string

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM