Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 translations in external js file [closed]

I want to put some translations to functions in external Java Script file. It is possible to parse js file e.g. like Twig template?


2 Answers

I can recommend the BazingaJsTranslationBundle (the same Bundle that @smoreno suggested, but the name changed in the meantime).

If you don't want to use a Bundle, there is a nice and easy to use workaround: You can define the translated string in a HTML twig file and call it in your external JavaScript file. For example like this

HTML file

{# index.html.twig #}
<html>
    <head></head>
    <body>
        {# Your stuff #}
        <script type="text/javascript">
            var myTranslatedString = '{{ 'my_string'|trans }}';
        </script>
    </body>
</html>

Translation file

# messages.en.yml
my_string: 'My Content'

JavaScript file

// script.js
alert(myTranslatedString); // Will output 'My Content'
like image 199
Gottlieb Notschnabel Avatar answered Dec 14 '25 11:12

Gottlieb Notschnabel


Yes. Just make yourself a whatever.js.twig file and have at it. You will need to make sure the headers and such get properly set.

like image 28
Cerad Avatar answered Dec 14 '25 12:12

Cerad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!