8

Ok I have the following app:

babel.cfg

[python: **.py] [jinja2: **/templates/**.html] extensions=jinja2.ext.autoescape,jinja2.ext.with_ 

lucy/init.py

babel = Babel() def create_app(object_name): app = Flask(__name__) app.config.from_object(object_name) app.register_blueprint(main_blueprint) app.register_blueprint(category_blueprint) app.register_blueprint(item_blueprint) babel.init_app(app) db.init_app(app) return app @babel.localeselector def get_locale(): return 'de' 

lucy/controllers/main.py

main_blueprint = Blueprint( 'main', __name__, template_folder='../templates/main', ) @main_blueprint.route('/debug') def debug(): print get_locale() return gettext('A simple string') 

I ran the following commands:

  1. pybabel extract -F babel.cfg -o messages.pot .
  2. pybabel init -i messages.pot -d translations -l de
  3. pybabel compile -d translations/

This is what my project structure looks like:

. |-- README.md |-- babel.cfg |-- fabfile.py |-- lucy | |-- __init__.py | |-- config.py | |-- controllers | | |-- __init__.py | | |-- category.py | | |-- item.py | | `-- main.py | |-- forms.py | |-- models.py | |-- static | | |-- css | | `-- js | `-- templates | |-- boilerplate | | |-- items.html | | `-- layout.html | |-- category | | `-- show.html | |-- item | | |-- index.html | | `-- show.html | `-- main | `-- signup.html |-- manage.py |-- messages.pot |-- requirements.txt |-- tests | |-- __init__.py | `-- test_models.py `-- translations `-- de `-- LC_MESSAGES |-- messages.mo `-- messages.po 

These are the results from translation:

messages.pot

# Translations template for PROJECT. # Copyright (C) 2016 ORGANIZATION # This file is distributed under the same license as the PROJECT project. # FIRST AUTHOR <EMAIL@ADDRESS>, 2016. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" "POT-Creation-Date: 2016-07-19 16:07+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <[email protected]>\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.3.4\n" #: lucy/controllers/main.py:20 msgid "A simple string" msgstr "" 

translation/de/LC_MESSAGES/message.po

# German translations for PROJECT. # Copyright (C) 2016 ORGANIZATION # This file is distributed under the same license as the PROJECT project. # FIRST AUTHOR <EMAIL@ADDRESS>, 2016. # msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" "POT-Creation-Date: 2016-07-19 16:07+0800\n" "PO-Revision-Date: 2016-07-19 16:07+0800\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language: de\n" "Language-Team: de <[email protected]>\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.3.4\n" #: lucy/controllers/main.py:20 msgid "A simple string" msgstr "german string berlin" 

translation/de/LC_MESSAGES/message.mo

??,<=?M?A simple stringProject-Id-Version: PROJECT VERSION Report-Msgid-Bugs-To: EMAIL@ADDRESS POT-Creation-Date: 2016-07-19 16:07+0800 PO-Revision-Date: 2016-07-19 16:07+0800 Last-Translator: FULL NAME <EMAIL@ADDRESS> Language: de Language-Team: de <[email protected]> Plural-Forms: nplurals=2; plural=(n != 1) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Generated-By: Babel 2.3.4 german string berlin 

When I visit /debug, I see that my locale is de. However, I still see A simple string being outputted. Any ideas why?

6
  • 3
    try using lazy_gettext. Commented Jul 19, 2016 at 8:43
  • I tried - that didn't work. Why would it make a difference? Commented Jul 19, 2016 at 8:52
  • 2
    I found out what went wrong. After an hour of printing debug statements in my virtual env library I realized they were looking for the translations folder at the Lucy/lucy directory rather than the Lucy directory... Commented Jul 19, 2016 at 17:02
  • 1
    @Sparrowcide how did you fix it? Commented Sep 25, 2017 at 13:51
  • 1
    I know I'm late but for me using lazy_gettext works. The problem is that form works outside the request lifecycle, so using gettext() they are evaluated before the are actually used. Using lazy_gettext they are evaluated at time of using. Commented Mar 20, 2020 at 13:54

3 Answers 3

10

For me the solution was to set the BABEL_TRANSLATION_DIRECTORIES variable. It contains a string with paths separated by ';'.

In my config (only one location):

BABEL_TRANSLATION_DIRECTORIES = '/home/work/python/project/translations'

Sign up to request clarification or add additional context in comments.

2 Comments

Was this in your babel.cfg file?
@StanJames this would be in the app.config object app.config['BABEL_TRANSLATION_DIRECTORIES'] You should be setting this somewhere along with, for example SQLALCHEMY_DATABASE_URI - this might be being read from a config file
5

My answer is the same as fmelan's, but I use absolute path:

from flask import Flask import os base_dir = os.path.abspath(os.path.dirname(__file__)) app = Flask(__name__) app.config["BABEL_TRANSLATION_DIRECTORIES"] = os.path.join(base_dir, "app/translation") 

Comments

3

Expanding from fmelan's answer: if your translations directory is on the same place as your flask_app.py, then you can just do:

app.config['BABEL_TRANSLATION_DIRECTORIES'] = './translations' 

...and stop worrying on whether the absolute path works wherever the app is running.

For me this nicely solved a quite weird behaviour when deploying the page to PythonAnywhere.

2 Comments

As of flask-babel 2.0, this seems to already be the default
Maybe, but I guess PythonAnywhere messes with the absolute paths, and so I had to include that for it to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.