Twigjs和动态翻译

时间:2012-01-09 22:06:21

标签: php symfony twig assetic

我遇到了问题,我在Symfony2 twigjs和assetic中使用动态渲染我的一些模板。

我已经阅读了文档,来源和测试。

TransFilterCompilerTest.php/testCompileDynamicTranslations似乎是

我的树枝模板中的

{{ 'foo' | trans }}

将在twigjs模板中替换

在我的twigjs中

'this.env_.filter("trans",'...

在我编译的js中

我只有sb.append(twig.filter.escape(this.env_, "posted", "html", null, true));

你知道为什么吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

经过一番调查后,我发现虽然几个月前在JMSTwigJsBundle中添加了翻译编译过滤器,但最近才向Assetic添加了所需的功能。使用已发布的库版本将无法正常工作。现在,必须使用git中的master。

使用deps文件...

[assetic]
    git=http://github.com/kriswallsmith/assetic.git

[AsseticBundle]
    git=http://github.com/symfony/AsseticBundle.git
    target=/bundles/Symfony/Bundle/AsseticBundle

必须将站点支持的语言指定为参数。我将它添加到我的config.yml文件中。

parameters:
    assetic.variables:
        locale: ['en', 'fr']

最后,文件集必须表明它根据语言环境而变化。

{% javascripts vars=["locale"]
    '@AcmeBundle/Resources/views/Default/some_template.html.twig'
    filter="twig_js"
%}  

以下是完整性的示例模板:

{% twig_js name="some_template" %}
<b>{{'test.say.hello' | trans({"%name%": name|default("World")})}}</b>

还必须在模板定义之前加载twig.js引导程序文件。调用模板符合预期:

Twig.render(some_template, {name: 'CoBaLt2760'})