babel flask jinja javascript hell babel flask jinja javascript hell flask flask

babel flask jinja javascript hell


I tried a lot to make flask babel translate texts from my js file and just import the script to the html with a:

<script src="myscripts.js"></script>

so I found some good links:

  1. Flask-Babel localized strings within js

  2. Babel.cfg

So I understood that I need to make my own babel.cfg file: that includes [javascript: /.js] in the header.

Although I did it, and executed the code:

pybabel extract -F babel.cfg -o messages.pot

It still didn't translated from javascript... so for now I am putting the script in an html:

<html><script>//my script with {{_('translating')}}</script></html>

And including this html where I need the script with an:

{% include 'myscript.html' %}

I know it is horrible, but is the only think that is working for me now


To use jinja notation in js you should wrap it with "":

use:

var x = "{{_('translation here')}}"

or:

var x = "{{gettext('translation here')}}"

But I think the standard way is to pass the translation from back-end:

from flask_babel import gettextfoo = gettext(a)return render_template(HTML, a=a)