简体   繁体   English

在Symfony 2中使用Twig:嵌入块内的翻译不起作用

[英]Using Twig in Symfony 2: Translation inside an embeded block does not work

I am using Twig inside my Symfony 2 WebApp project. 我在Symfony 2 WebApp项目中使用Twig I use {% embed SomeTamplate %} to include the content of one template file in another. 我使用{% embed SomeTamplate %}将一个模板文件的内容包含在另一个模板文件中。 This workes fine, but translation is not working inside the embedded file. 这工作正常,但嵌入文件内的翻译不起作用。

Page Template: 页面模板:

{% extends 'AppBundle::layout.html.twig' %}
{% trans_default_domain mypages' %}

1: {{ 'pages.home.sometext'|trans }}

{% embed "block.html.twig" with {'classes': 'homepage-hero'} %}
    {% block content %}
        2: {{ 'pages.home.sometext'|trans }}
    {% endblock %}
{% endembed %}

{% embed "block.html.twig" with {'classes': 'red-bg'} %}
    {% block content %}
        3: {{ 'pages.home.sometext'|trans }}
    {% endblock %}
{% endembed %}

Block template: 块模板:

{% trans_default_domain mypages' %}

<div class="full-width-block{% if classes is defined %} {{ classes }}{% endif %}">
    X: {{ 'pages.home.sometext'|trans }}

    {% block content %}
    {% endblock %}
</div>

Output: 输出:

1: SomeText

X: SomeText
2: pages.home.sometext

X: SomeText
3: pages.home.sometext

So: While the translation works fine within the two template files, the same text constant within the embedded block, is not translated. 所以:虽然翻译在两个模板文件中工作正常,但嵌入块中的相同文本常量不会被翻译。 How can I fix this? 我怎样才能解决这个问题?

the domain must be enclosed in quotes, just put the last quotation mark. 域名必须用引号括起来,只需加上最后一个引号即可。 Visit http://symfony.com/doc/current/book/translation.html 访问http://symfony.com/doc/current/book/translation.html

Put it this way: 这样说吧:

{% trans_default_domain "mypages" %} {%trans_default_domain“mypages”%}

I hope your problem is solved 我希望你的问题得到解决

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

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