API Documentation

The Translation Manager Module (hoshi.i18n)

This module provides the main interfaces to hoshi.

hoshi.i18n._mtime(filepath)[source]
class hoshi.i18n.TranslationManager(supported_languages, catalog_dirs)[source]

Bases: object

The TranslationManager object is the primary interface of the library to application code. Most applications using hoshi will instantiate this class exactly once and hold the instance in a globally accessible namespace, such as the application or config object.

This class manages the translation contexts, locales, and external translators.

Parameters
  • supported_languages – A list of languages / locales the application intends to support

  • catalog_dirs – A list of directories which hold the translation templates and catalogs

_install_object_translator(objtype, translator)[source]
install_object_translator(objtype, translator)[source]
install()[source]
_install_catalogs()[source]
property catalog_dirs
property log
property primary_language
install_language(language)[source]

Install a single language to the application’s locale set. This function uses babel to install the appropriate locale, which can later be used to render things like dates, numbers, and currencies.

Locales should not be used directly, and instead should be used using the translator.

It is currently not intended for languages to be installed on the fly. Do this before creating any contexts or attempting any translations.

Parameters

language – locale code in the form ‘en_US’

static _pot_path(context_name, catalog_dir)[source]
_create_context(context_name, catalog_dir, metadata)[source]
static _po_path(context_name, language, catalog_dir)[source]
static _mo_path(context_name, language, catalog_dir)[source]
static _pass_through_strings(catalog)[source]
install_translator(translator)[source]
translate(source_language, target_language, string)[source]
_translate_string(language, string)[source]
_translate_strings(catalog)[source]
_create_context_lang(context_name, language, catalog_dir, metadata)[source]
_update_context_lang(context_name, language, catalog_dir)[source]
_compile_context_lang(context_name, language, catalog_dir, metadata)[source]
install_context(context_name, language, catalog_dir=None, metadata=None)[source]

Install an i18n context. Language would be a locale code of the form “en_US”. While not mandated, context name would typically be of the form “<module>”.

i18n Contexts are objects which can manage specific i18n strategies and contain and apply special helper functions for translating a string.

install_change_handler(context_name, handler)[source]
set_language(context_name, language, fallback=True)[source]
set_global_language(language)[source]
_i18n_msg(context, message)[source]

Translate an atomic string message using the provided context. Conversion by this function applies a standard gettext / po mechanism. If the string is not included in the i18n catalogs, it will be added for later manual translation. Note that the po files will be updated only on the next application run.

_translate(context, obj, *args, **kwargs)[source]

Translate a translatable object using the provided context. This function should dispatch to specific functions depending on the type of the object. If the context has special helper / preprocessing functions installed, they are applied here. - Numbers, dates, times, currencies : Locale - Strings : _i18n

translator(context_name, language=None)[source]

Generate and return an i18n function which uses the provided context. This can be used to replicate the typical _() structure.

locale(language)[source]

Return the babel locale associated with the provided language.

current_language(context)[source]
current_locale(context)[source]

External Translators Integration

Language Sets

Internal Utility Modules