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?
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
{# index.html.twig #}
<html>
<head></head>
<body>
{# Your stuff #}
<script type="text/javascript">
var myTranslatedString = '{{ 'my_string'|trans }}';
</script>
</body>
</html>
# messages.en.yml
my_string: 'My Content'
// script.js
alert(myTranslatedString); // Will output 'My Content'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With