2012-01-09 67 views
4

我正面臨一個問題,我在Symfony2中使用twigjs和assetic來動態地呈現我的一些模板。Twigjs和動態翻譯

我已閱讀文檔,源代碼和測試。

TransFilterCompilerTest.php/testCompileDynamicTranslations似乎在我的樹枝模板

{{ 'foo' | trans }}

將在twigjs模板,通過

'this.env_.filter("trans",'...我twigjs一個

在我的編譯取代js我只有sb.append(twig.filter.escape(this.env_, "posted", "html", null, true));

你知道爲什麼嗎?

謝謝!

+1

該問題似乎與TwigJs \ JsCompiler :: getDefine('locale')返回null有關。我似乎無法找到它的配置。 – 2012-03-08 19:22:35

回答

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