Internationalization and localization

Langage base object

class optimus.i18n.lang.LangBase(code=None, label=None)[source]

Langage base object to encapsulate the language label, code and other details.

Alternative and External code are not really used internally in optimus, there are only for some template usage.

The instance will also supply a “language_name” and “region_name” class attributes, which are the result of splitting the code on two parts. “region_name” is None by default, as the region name is optional in language identifier.

Code and name is not validated, you are responsible to ensure they are valid.

See http://www.i18nguy.com/unicode/language-identifiers.html for more details on language identifiers.

Example:

class LangFr(LangBase):
    code = 'fr'
    label = 'France'

Or:

lang = LangBase(code="zh_CN", label="Chinese")
Keyword Arguments:
 
  • code (string) – Langage identifier.
  • label (string) – Langage label like “Français” for fr.
label

Default language label if not given in kwargs.

Type:string
code

Default language identifier if not given in kwargs.

Type:string
alt_code

Alternative code, will be equal to “code” if not set.

Type:string
external_code

External code for some external apps, will be equal to alt_code if not set.

Type:string
split_code(code)[source]

Split language identifier to language name and region name (if any).

Parameters:code (string) – Langage identifier.
Returns:A pair of language name and possibly region name, if code does not contain any region it will be None.
Return type:tuple

I18n management

I18n management support for Optimus environnment.

Only “messages.*” files for POT and PO files are managed and no other catalog type.

class optimus.i18n.manager.I18NManager(settings)[source]

I18n manager for translation catalogs

Made to work simply within Optimus environnment, so not all of babel options are used. This way the manager can work cleanly and is more easy to use.

Parameters:settings (conf.model.SettingsModel) – Settings registry instance.
catalog_name

Catalog filename template.

Type:string
catalog_path

Catalog language directory template.

Type:string
header_comment

Header comment to prepend to catalog files.

Type:string
settings

Settings registry instance.

Type:conf.model.SettingsModel
logger

Optimus logger.

Type:logging.Logger
get_template_path()[source]

Return the full path to the catalog template file

Returns:Catalog template file path.
Return type:string
get_catalog_dir(locale)[source]

Return the full path to a translations catalog directory

Parameters:locale (string) – Language identifier.
Returns:Catalog directory path.
Return type:string
get_po_filepath(locale)[source]

Return the full path to a translations catalog file

Parameters:locale (string) – Language identifier.
Returns:Catalog file path.
Return type:string
get_mo_filepath(locale)[source]

Return the full path to a compiled translations catalog file

Parameters:locale (string) – Language identifier.
Returns:Compiled catalog file path.
Return type:string
check_locales_dir()[source]

Check if LOCALES_DIR directory exists

Returns:True if base catalog directory exists.
Return type:boolean
check_template_path()[source]

Check if the catalog template exists

Returns:True if catalog template file exists.
Return type:boolean
check_catalog_path(locale)[source]

Check if a translations catalog exists

Parameters:locale (string) – Language identifier.
Returns:True if catalog file exists.
Return type:boolean
parse_languages(languages)[source]

Allways return a list of locale name from languages even if items are simple string or tuples. If tuple, assume its first item is the locale name to use.

Parameters:languages (list) – List of languages identifiers.
Returns:Dictionnary of languages identifiers.
Return type:dict
init_locales_dir()[source]

Create catalog base directory defined from LOCALES_DIR settings if it does not allready exists.

build_pot(force=False)[source]

Extract translation strings and create Portable Object Template (POT) from enabled source directories using defined extract rules.

Note

May only work on internal ‘_pot’ to return without touching ‘self._pot’.

Keyword Arguments:
 force (boolean) – Default behavior is to proceed only if POT file does not allready exists except if this argument is True.
Returns:Catalog template object.
Return type:babel.messages.catalog.Catalog
pot

Return the catalog template

Get it from memory if allready opened, if allready exists then open it, else extract it and create it.

Returns:Catalog template object.
Return type:babel.messages.catalog.Catalog
safe_write_po(catalog, filepath, **kwargs)[source]

Safely write or overwrite a PO(T) file.

Try to write catalog to a temporary file then move it to its final destination only writing operation did not fail. This way initial file is not overwrited when operation has failed.

Original code comes from babel.messages.frontend.

Parameters:
  • catalog (babel.messages.catalog.Catalog) – Catalog object to write.
  • filepath (string) – Catalog file path destination.
  • **kwargs – Additional arbitrary keyword argumentsto pass to write_po() babel function.
Returns:

Catalog template object.

Return type:

babel.messages.catalog.Catalog

clone_pot()[source]

Helper to clone POT catalog from writed file (not the one in memory) without to touch to _pot attribute.

Returns:Clone catalog template object.
Return type:babel.messages.catalog.Catalog
init_catalogs(languages=None)[source]

Create PO catalogs from POT if they dont allready exists

Keyword Arguments:
 languages (list) – List of languages to process. Default is None so languages are taken from LANGUAGES settings.
Returns:List of language identifiers for created catalogs.
Return type:list
update_catalogs(languages=None)[source]

Update PO catalogs from POT

Keyword Arguments:
 languages (list) – List of languages to process. Default is None so languages are taken from LANGUAGES settings.
Returns:List of language identifiers for updated catalogs.
Return type:list
compile_catalogs(languages=None)[source]

Compile PO catalogs to MO files

Note

Errors have no test coverage since read_po() pass them through warnings print to stdout and this is not blocking or detectable. And so the code continue to the compile part.

Keyword Arguments:
 languages (list) – List of languages to process. Default is None so languages are taken from LANGUAGES settings.
Returns:List of language identifiers for compiled catalogs.
Return type:list