Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

babel flask jinja javascript hell

Is there a way for me to include flask jinja enclosed babel translations inside of my javascript file and load it in as if it was a static file? Or is the only way for me to do this to include this part of my code in the .html template file? I'm asking because I want to abstract this part of my code out so it can be reused in other areas, but if I do this, I lose the translations and am not able to obtain necessary information.

Please advice. I want to import javascript file but also be able to use

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

inside of my javascript file and also be able to pull out the translations via babel to be later internationalized via .po file.

Thanks! Cheers

like image 326
user805981 Avatar asked Jun 01 '26 11:06

user805981


1 Answers

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

like image 64
Dinidiniz Avatar answered Jun 04 '26 01:06

Dinidiniz



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!