4

I am using flask-babel for translating a Flask-based web application. I really want to know how can I translate the content of a variable, say foo.

I try {{ _(foo) }}, but when I update the .po files like this:

pybabel extract -F babel.cfg -k lazy_gettext -o messages.pot .
pybabel update -i messages.pot -d translations

nothing is displayed for translating with the content of the foo var.

All is OK with constants strings, like {{ _("goo")}}.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
somenxavier
  • 1,206
  • 3
  • 20
  • 43
  • 1
    If I try: `{% trans %}{{activity.title}}{% endtrans %}`, I get `jinja2.exceptions.TemplateSyntaxError jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got '.'` – somenxavier Jan 03 '16 at 17:22

2 Answers2

3

You cannot extract a variable, because in the expression _(some_variable) some_variable is looked up at run-time. If you are taking some_variable from a list of static values then the static values will be extracted. For example:

COLORS_OF_MAGIC = [_("green"), _("red"), _("blue"), _("white"), _("black")]

def color_for_index(index):
    if not (0 < index > len(COLORS_OF_MAGIC)):
        index = 0

    return COLORS_OF_MAGIC[index]

def do_work():
    index = get_index_from_user()
    some_variable = color_for_index(index)
    return _("You chose ") + _(some_variable) + _(" as the color of magic")

will have the values: green, red, blue, white, black, You chose, and as the color of magic in the PO file.

If, on the other hand, you are trying to translate user-provided strings then you will need another solution, as Babel is a static translation service.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • So is there any solution for dynamic translation? – somenxavier Jan 04 '16 at 16:48
  • [This](http://stackoverflow.com/questions/21497489/translating-strings-from-database-flask-babel) say the same: babel cannot translate dynamics fields, but who can. – somenxavier Jan 04 '16 at 16:57
  • Is this the sort of application that you could / should ask the user to provide alternative translations for their text (e. g. a CMS) or something where the end-user will generally *not* be providing translations and someone might want to see the content in another language (e. g. a blog)? – Sean Vieira Jan 04 '16 at 18:17
  • I have the values of my variables from a file. Is there any way to translate that? I could define: `COLORS_OF_MAGIC = yaml.load(file)`? – somenxavier Jan 05 '16 at 17:20
  • you example does not work. I only get `You chose` and `as the color of magic` in my .pot file. No value of "green",... – somenxavier Jan 05 '16 at 17:29
  • @somenxavier - my fault entirely - I forgot the `_(...)` invocation around the static values for the colors. – Sean Vieira Jan 05 '16 at 18:24
  • ok. Turning to my original question, can I load the contents of one file statically? And so take advance to this trick? – somenxavier Jan 06 '16 at 16:14
  • @somenxavier - that would be better done by a build script that takes the contents of that file and adds it to the PO file, honestly (if I am understanding you correctly). – Sean Vieira Jan 06 '16 at 17:01
  • 1
    it's a pitty to have to build a custom script. I thought Babel was enough capable of doing this. I have to reinvent the wheel... – somenxavier Jan 06 '16 at 17:16
0

Ran into the same problem. I needed to translate month names that passed to jinja2 at runtime. The solution I came up with is to pass translated names. Then all I had to do is declare a static list of names as mentioned in the accepted answer. Hope this helps.

mljli
  • 593
  • 5
  • 23