经过进一步调查,我发现虽然翻译编译过滤器是几个月前在 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'})