javascriptpythonjinja2babeljsflask-babel

How to translate using flask-babel on javascript?


I have a python project with jinja templates and flask-babel working good. My problem are javascript files. I have all the js files inside a folder called 'static', but after using:

var food = gettext('food');

Then updating and compiling, the browser returns an error :

gettext is not defined

I've seen some projects using babel.js or jsil18n but I'm not sure if I really need that. How can I define gettext the same same way I did with my jinja templates?


Solution

  • If food is a string, then you can use the following syntax provided you have the translation for food in your messages.po file:

    var food = "{{_('food')}}";