API Docs

Extension

Invenio internationalization module.

class invenio_i18n.ext.InvenioI18N(app=None, date_formats=None, localeselector=None, timezoneselector=None, entry_point_group='invenio_i18n.translations')[source]

Invenio I18N extension.

Initialize extension.

Parameters:
  • app – Flask application.
  • data_formats – Override default date/time formatting.
  • localeselector – Callback function used for locale selection. (Default: invenio_i18n.selectors.get_locale())
  • timezoneselector – Callback function used for timezone selection. (Default: BABEL_DEFAULT_TIMEZONE)
  • entry_point_group – Entrypoint used to load translations from. Set to None to not load translations from entry points.
get_languages()[source]

Get list of languages.

get_locales()[source]

Get a list of supported locales.

Computes the list using I18N_LANGUAGES configuration variable.

init_app(app)[source]

Flask application initialization.

The initialization will:

  • Set default values for the configuration variables.
  • Load translations from paths specified in I18N_TRANSLATIONS_PATHS.
  • Load translations from app.root_path>/translations if it exists.
  • Load translations from a specified entry point.
  • Add toutc and tousertimezone template filters.
  • Install a custom JSON encoder on app.
init_config(app)[source]

Initialize configuration.

iter_languages()[source]

Iterate over list of languages.

language

Get current language code.

locale

Get current locale.

timezone

Get current timezone.

invenio_i18n.ext.get_lazystring_encoder(app)[source]

Return a JSONEncoder for handling lazy strings from Babel.

Installed on Flask application by default by InvenioI18N.

Translation Domain

Flask-BabelEx domain for merging translations from many directories.

class invenio_i18n.babel.MultidirDomain(paths=None, entry_point_group=None, domain='messages')[source]

Domain supporting merging translations from many catalogs.

The domain contains an internal list of paths that it loads translations from. The translations are merged in order of the list of paths, hence the last path in the list will overwrite strings set by previous paths.

Entry points are added to the list of paths before the paths.

Initialize domain.

Parameters:
  • paths – List of paths with translations.
  • entry_point_group – Name of entry point group.
  • domain – Name of message catalog domain. (Default: 'messages')
add_entrypoint(entry_point_group)[source]

Load translations from an entry point.

add_path(path)[source]

Load translations from an existing path.

get_translations()[source]

Return the correct gettext translations for a request.

This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found.

has_paths()[source]

Determine if any paths have been specified.

invenio_i18n.babel.set_locale(*args, **kwds)[source]

Set Babel localization in request context.

Parameters:ln – Language identifier.

Jinja2 filters

Babel datetime localization template filters for Jinja.

See full documentation of corresponding methods in Flask-BabelEx: https://pythonhosted.org/Flask-BabelEx/

invenio_i18n.jinja2.filter_language_name(lang_code)[source]

Convert language code into display name in current locale.

Installed on application as language_name.

invenio_i18n.jinja2.filter_language_name_local(lang_code)[source]

Convert language code into display name in local locale.

Installed on application as language_name_local.

invenio_i18n.jinja2.filter_to_user_timezone(dt)[source]

Convert a datetime object to the user’s timezone.

Installed on application as tousertimezone.

invenio_i18n.jinja2.filter_to_utc(dt)[source]

Convert a datetime object to UTC and drop tzinfo.

Installed on application as toutc.

Locale/timezone selectors

Default locale and timezone selectors for Flask-BabelEx.

See Flask-BabelEx documentation for corresponding methods.

invenio_i18n.selectors.get_locale()[source]

Get locale.

Searches for locale in the following the order:

  • User has specified a concrete language in the query string.
  • Current session has a language set.
  • User has a language set in the profile.
  • Headers of the HTTP request.
  • Default language from BABEL_DEFAULT_LOCALE.

Will only accept languages defined in I18N_LANGUAGES.

invenio_i18n.selectors.get_timezone()[source]

Get default timezone (i.e. BABEL_DEFAULT_TIMEZONE).

Views

Views for Invenio-I18N.

invenio_i18n.views.create_blueprint(register_default_routes=True)[source]

Create Invenio-I18N blueprint.

invenio_i18n.views.get_redirect_target()[source]

Get URL to redirect to and ensure that it is local.

invenio_i18n.views.is_local_url(target)[source]

Determine if URL is safe to redirect to.

invenio_i18n.views.set_lang(lang_code=None)[source]

Set language in session and redirect.

Date/time formatting

For formatting date and time using the current locale settings, you may use the methods provided by Flask-BabelEx.

These methods are also available as Jinja filters: