The cinder.openstack.common.gettextutils Module

gettext for openstack-common modules.

Usual usage in an openstack.common module:

from cinder.openstack.common.gettextutils import _
class LocaleHandler(locale, target)

Bases: logging.Handler

Handler that can have a locale associated to translate Messages.

A quick example of how to utilize the Message class above. LocaleHandler takes a locale and a target logging.Handler object to forward LogRecord objects to after translating the internal Message.

emit(record)
class Message(msg, domain)

Bases: UserString.UserString, object

Class used to encapsulate translatable messages.

data
locale
enable_lazy()

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.

get_available_languages(domain)

Lists the available languages for the given translation domain.

Parameters:domain – the domain to get languages for
get_localized_message(message, user_locale)

Gets a localized version of the given message in the given locale.

install(domain, lazy=False)

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.

Previous topic

The cinder.openstack.common.fileutils Module

Next topic

The cinder.openstack.common.importutils Module

This Page