i18n Translations¶
Introduction¶
- F.A.B. has support for seven languages (planning for some more):
Chinese
Dutch
English
French
German
Japanese
Polish
Portuguese
Portuguese Brazil
Russian
Spanish
Greek
Korean
Italian
This means that all messages, builtin on the framework are translated to these languages.
You can add your own translations for your application, using Flask-BabelPkg, this is a fork from Flask-Babel, created because it was not possible to separate package translations from the application translations.
You can add your own translations, and your own language support. Take a look at Flask-Babel for setup an babel initial configuration.
Initial Configuration¶
On you projects root create a directory named babel, then create and edit a file named babel.cfg with the following content (this configuration is already made on the base skeleton application):
[python: **.py]
[jinja2: **/templates/**.html]
encoding = utf-8
First, create your translations, for example to portuguese, spanish and german, execute on you projects root:
pybabel init -i ./babel/messages.pot -d app/translations -l pt
pybabel init -i ./babel/messages.pot -d app/translations -l es
pybabel init -i ./babel/messages.pot -d app/translations -l de
Next extract your strings to be translated, execute on you projects root:
$ flask fab babel-extract
If you want to, or if you’re using a version prior to 1.3.0 you can use:
pybabel extract -F ./babel/babel.cfg -k lazy_gettext -o ./babel/messages.pot .
Quick How to¶
Let’s work with the contacts application example, so you want to add translations for the menus “List Groups” and “List Contacts”.
from flask.ext.babelpkg import lazy_gettext as _
class GroupModelView(ModelView):
datamodel = SQLAInterface(ContactGroup)
related_views = [ContactModelView]
label_columns = {'name':_('Name')}
genapp.add_view(GroupModelView(), "List Groups",icon = "th-large", label=_('List Groups'),
category = "Contacts", category_icon='fa-envelope', category_label=_('Contacts'))
genapp.add_view(ContactModelView(), "List Contacts",icon = "earphone", label=_('List Contacts'),
category = "Contacts")
1 - Run the extraction, from the root directory of your project:
$ flask fab babel-extract
If you want to, or if you’re using a version prior to 1.3.0 you can use:
pybabel extract -F ./babel/babel.cfg -k lazy_gettext -o ./babel/messages.pot .
2 - Make your translations
On app/translations/pt/LC_MESSAGES/messages.po you will find the messages you added to translate:
msgid "Name" msgstr "" msgid "Contacts" msgstr "" msgid "List Groups" msgstr "" msgid "List Contacts" msgstr ""Translate them:
msgid "Name" msgstr "Nome" msgid "Contacts" msgstr "Contactos" msgid "List Groups" msgstr "Lista de Grupos" msgid "List Contacts" msgstr "Lista de Contactos"
3 - Compile your translations, from the root directory of your project:
$ flask fab babel-compile
4 - Add your language support to the framework
On config tell the framework the languages you support. With this you will render a menu with the corresponding country flags. use the config var ‘LANGUAGES’ with a dict whose first key is a string with the corresponding babel language code, the value is another dict with two keys ‘flag’ and ‘name’, with the country flag code, and text to be displayed:
LANGUAGES = { 'en': {'flag':'gb', 'name':'English'}, 'pt': {'flag':'pt', 'name':'Portuguese'} }
And thats it!