Get translatable text from an external source into catalog

I created a messages.txt with my "words" like gettext function calls:

_('cycling')
_('running')

and added it to my babel.cfg as python source:

[python: messages.txt]

plain, simple, stupid, but works.


First, start with http://flask.pocoo.org/snippets/4/.

Secondly, you need to store these 'limited' values as integers or enums in database and then create the lookup table for all these enums in code (so Babel knows about them):

i18n_val = {0: _('running'), ...}
# Or multi-level dict with different categories:
i18n_all = {
  'activity': {
     0: _('running'), ...
  'foo': {
     0: _('bar..'), ...
  }
}

And accessing the translated string from template is now as simple as:

{{ i18n_val[obj.activity] }}
{{ i18n_all['activity'][obj.activity] }}

In order to make the i18n_val and i18n_all variables available for all the templates, just register them with context processors.