The openstack_dashboard.openstack.common.gettextutils ModuleΒΆ

gettext for openstack-common modules.

Usual usage in an openstack.common module:

from openstack_dashboard.openstack.common.gettextutils import _
class openstack_dashboard.openstack.common.gettextutils.Message[source]

Bases: unicode

A Message object is a unicode object that can be translated.

Translation of Message is done explicitly using the translate() method. For all non-translation intents and purposes, a Message is simply unicode, and can be treated as such.

Message.translate(desired_locale=None)[source]

Translate this message to the desired locale.

Parameters:desired_locale – The desired locale to translate the message to, if no locale is provided the message will be translated to the system’s default locale.
Returns:the translated message in unicode
class openstack_dashboard.openstack.common.gettextutils.TranslationHandler(locale=None, target=None)[source]

Bases: logging.handlers.MemoryHandler

Handler that translates records before logging them.

The TranslationHandler takes a locale and a target logging.Handler object to forward LogRecord objects to after translating them. This handler depends on Message objects being logged, instead of regular strings.

The handler can be configured declaratively in the logging.conf as follows:

[handlers] keys = translatedlog, translator

[handler_translatedlog] class = handlers.WatchedFileHandler args = (‘/var/log/api-localized.log’,) formatter = context

[handler_translator] class = openstack.common.log.TranslationHandler target = translatedlog args = (‘zh_CN’,)

If the specified locale is not available in the system, the handler will log in the default locale.

TranslationHandler.emit(record)[source]
TranslationHandler.setFormatter(fmt)[source]
openstack_dashboard.openstack.common.gettextutils.enable_lazy()[source]

Convenience function for configuring _() to use lazy gettext

Call this at the start of execution to enable the gettextutils._ function to use lazy gettext functionality. This is useful if your project is importing _ directly instead of using the gettextutils.install() way of importing the _ function.

openstack_dashboard.openstack.common.gettextutils.get_available_languages(domain)[source]

Lists the available languages for the given translation domain.

Parameters:domain – the domain to get languages for
openstack_dashboard.openstack.common.gettextutils.install(domain, lazy=False)[source]

Install a _() function using the given translation domain.

Given a translation domain, install a _() function using gettext’s install() function.

The main difference from gettext.install() is that we allow overriding the default localedir (e.g. /usr/share/locale) using a translation-domain-specific environment variable (e.g. NOVA_LOCALEDIR).

Parameters:
  • domain – the translation domain
  • lazy – indicates whether or not to install the lazy _() function. The lazy _() introduces a way to do deferred translation of messages by installing a _ that builds Message objects, instead of strings, which can then be lazily translated into any available locale.
openstack_dashboard.openstack.common.gettextutils.translate(obj, desired_locale=None)[source]

Gets the translated unicode representation of the given object.

If the object is not translatable it is returned as-is. If the locale is None the object is translated to the system locale.

Parameters:
  • obj – the object to translate
  • desired_locale – the locale to translate the message to, if None the default system locale will be used
Returns:

the translated object in unicode, or the original object if it could not be translated

Previous topic

The openstack_dashboard.openstack.common.jsonutils Module

Next topic

The openstack_dashboard.openstack.common.log Module

This Page