Internationalization
Info
This feature is provided by Flask-Babel-Hyper
Marking strings for translation
Use gettext functions to mark all strings requiring translation.
In templates:
{{_("this string will be translated")}}
In python:
from hyperflask import _
_("this string will be translated")
Other available functions:
Full name | Short name | Description |
---|---|---|
gettext | _ | Return a localized string |
ngettext | _n | Like gettext(), but consider plural forms |
pgettext | _p | Like gettext(), but specify the context |
lazy_gettext | _lazy | Like gettext(), but evaluation is delayed |
Tip
Format strings should be resolved after the translation has happenned.
Bad: _("hello %(name)s" % {"name": "world"})
Good: _("hello %(name)s") % {"name": "world"}
Creating a translation
Run the following command:
$ hyperflask babel init LOCALE
Where LOCALE should be a locale code (eg: fr
)
Translate the generated po file in app/translations/{LOCALE}/LC_MESSAGES using a tool like poedit.
Once all the translations have been completed, run the following command:
$ hyperflask babel compile
Locale detection
Locale is automatically selected from, in order:
- the
locale
query string parameter - the browser's Accept-Language header
- the default locale
When the locale is set via the query string parameter, it is stored in the session until further change.
Updating translations
Run the following command:
$ hyperflask babel update
Add missing translations in po files then run the following command:
$ hyperflask babel compile