GAE webapp application internationalization with Babel

六眼飞鱼酱① 提交于 2019-12-03 08:20:08


How would you go about internationalizing a Google App Engine webapp application using BABEL? I am looking here for all the stages:

  1. Marking the strings to be translated.
  2. Extracting them.
  3. Traslating
  4. Configuring your app to load the right language requested by the browser


1) use _() (or gettext()) in your code and templates. Translated strings set in the module globals or class definitions should use some form of lazy gettext(), because i18n won't be available when the modules are imported.

2) Extract all translations using pybabel. Here we pass two directories to be scanned: the templates dir and the app dir. This will create a messages.pot file in the /locale directory with all strings found in these directories. babel.cfg is the extraction configuration that varies depending on the template engine you use:

$ pybabel extract -F ./babel.cfg -o ./locale/messages.pot ./templates/ ./app/

3) Initialize a directory for each language. This is done only once. Here we initialize three translations, en_US, es_ES and pt_BR, and use the messages.pot file created on step 2:

$ pybabel init -l en_US -d ./locale -i ./locale/messages.pot 
$ pybabel init -l es_ES -d ./locale -i ./locale/messages.pot 
$ pybabel init -l pt_BR -d ./locale -i ./locale/messages.pot

Translate the messages. They will be in .mo files in each translation directory. After all locales are translated, compile them:

$ pybabel compile -f -d ./locale

Later, if new translations are added, repeat step 2 and update them using the new .pot file:

$ pybabel update -l pt_BR -d ./locale/ -i ./locale/messages.pot

Then translate the new strings and compile the translations again.

4) The strategy here may vary. For each request you must set the correct translations to be used, and probably want to cache loaded translations to reuse in subsequent requests.