Get translated text from an external source to a directory

Perhaps I do not see the obvious solution or am not mistaken ...

I have a limited amount of text, words in the database that I want to display, translated to users in flash / jinja / babel webapp. eg. "running" is the possible value of the "activity" column, and it should be "laufen" for my German users.

Words in templates and code are extracted and put into the directory, but how can I get additional words in the directory? Is there a simple text extractor file?

The only thing I could think of was just to create a .py file and put a lot of lines of _ ('...') in it, but that is just wrong ... isn't it?

+6
source share
2 answers

I created message.txt with my words, such as calls to the gettext function:

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

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

 [python: messages.txt] 

simple, simple, stupid, but it works.

+3
source

Start with http://flask.pocoo.org/snippets/4/ first .

Secondly, you need to save these "limited" values ​​as integers or enumerations in the database, and then create a lookup table for all these enumerations in the code (as 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 the template is now simple, like:

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

To make the i18n_val and i18n_all variables available for all templates, simply register them using context processors .

+1
source

Source: https://habr.com/ru/post/905384/


All Articles