Twigjs and dynamic translations

I have a problem, I use twigjs and assetic in Symfony2 to dynamically display some of my templates.

I read the document, source and tests.

In TransFilterCompilerTest.php/testCompileDynamicTranslations it seems that <

{{ 'foo' | trans }} {{ 'foo' | trans }} in my branch template

will be replaced in the twigjs template by

'this.env_.filter("trans",'... in my twigjs one

but in my compiled js I only have sb.append(twig.filter.escape(this.env_, "posted", "html", null, true));

Do you have any idea why?

Thanks!

+4
source share
1 answer

After some more research, I found out that although the translation compilation filter was added a few months ago in the JMSTwigJsBundle, the necessary functions were recently added to Assetic. Using released versions of libraries will not work. At this point, you need to use the wizard from git.

Using deps file ...

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

The languages ​​supported by the site should be specified as a parameter. I added it to the config.yml file.

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

Finally, the set of files should indicate that it depends on the language.

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

Here is a sample template for completeness:

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

The twig.js bootstrap file must also be loaded before the template is defined. The template call will be as expected:

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

Source: https://habr.com/ru/post/1390128/


All Articles