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.

When lazy translation is enabled in the application (see enable_lazy()), the TranslationHandler uses its locale configuration setting to determine how to translate LogRecord objects before forwarding them to the logging.Handler.

When lazy translation is disabled, the message in the LogRecord is converted to unicode without any changes and then forwarded to the logging.Handler.

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.

oslo_i18n.fixture

Table Of Contents

Previous topic

Guidelines for Use In OpenStack

Next topic

Policy History

This Page