API

oslo.i18n

class oslo.i18n.TranslatorFactory(domain, localedir=None)

Create translator functions

log_critical

Translate critical-level log messages.

log_error

Translate error-level log messages.

log_info

Translate info-level log messages.

log_warning

Translate warning-level log messages.

primary

The default translation function.

See also

An example of using a TranslatorFactory is provided in Creating an Integration Module.

oslo.i18n.enable_lazy(enable=True)

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.

Parameters:enable (bool) – Flag indicating whether lazy translation should be turned on or off. Defaults to True.

See also

Lazy Translation

oslo.i18n.translate(obj, desired_locale=None)

Gets the translated unicode representation of the given object.

If the object is not translatable it is returned as-is.

If the desired_locale argument 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

oslo.i18n.get_available_languages(domain)

Lists the available languages for the given translation domain.

Parameters:domain – the domain to get languages for

oslo.i18n.log

logging utilities for translation

class oslo.i18n.log.TranslationHandler(locale=None, target=None)

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 = oslo.i18n.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.

Table Of Contents

Previous topic

Guidelines for Use In OpenStack

Next topic

Contributing

This Page