oslo_i18n package¶
Submodules¶
oslo_i18n.fixture module¶
Test fixtures for working with oslo_i18n.
- class oslo_i18n.fixture.PrefixLazyTranslation(languages=None, locale=None)¶
Bases:
Fixture
Fixture to prefix lazy translation enabled messages
Use of this fixture will cause messages supporting lazy translation to be replaced with the message id prefixed with ‘domain/language:’. For example, ‘oslo/en_US: message about something’. It will also override the available languages returned from oslo_18n.get_available_languages to the specified languages.
This will enable tests to ensure that messages were translated lazily with the specified language and not immediately with the default language.
NOTE that this does not work unless lazy translation is enabled, so it uses the ToggleLazy fixture to enable lazy translation.
- Parameters:
languages (list of strings) – list of languages to support. If not specified (None) then [‘en_US’] is used.
- setUp()¶
Prepare the Fixture for use.
This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.
After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).
- Raises:
MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
- Returns:
None.
- Changed in 1.3:
The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
- Changed in 1.3.1:
BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.
- class oslo_i18n.fixture.ToggleLazy(enabled)¶
Bases:
Fixture
Fixture to toggle lazy translation on or off for a test.
- setUp()¶
Prepare the Fixture for use.
This should not be overridden. Concrete fixtures should implement _setUp. Overriding of setUp is still supported, just not recommended.
After setUp has completed, the fixture will have one or more attributes which can be used (these depend totally on the concrete subclass).
- Raises:
MultipleExceptions if _setUp fails. The last exception captured within the MultipleExceptions will be a SetupError exception.
- Returns:
None.
- Changed in 1.3:
The recommendation to override setUp has been reversed - before 1.3, setUp() should be overridden, now it should not be.
- Changed in 1.3.1:
BaseException is now caught, and only subclasses of Exception are wrapped in MultipleExceptions.
- class oslo_i18n.fixture.Translation(domain='test-domain')¶
Bases:
Fixture
Fixture for managing translatable strings.
This class provides methods for creating translatable strings using both lazy translation and immediate translation. It can be used to generate the different types of messages returned from oslo_i18n to test code that may need to know about the type to handle them differently (for example, error handling in WSGI apps, or logging).
Use this class to generate messages instead of toggling the global lazy flag and using the regular translation factory.
- immediate(msg)¶
Return a string as though it had been translated immediately.
- Parameters:
msg (str or unicode) – Input message string. May optionally include positional or named string interpolation markers.
- lazy(msg)¶
Return a lazily translated message.
- Parameters:
msg (str or unicode) – Input message string. May optionally include positional or named string interpolation markers.
oslo_i18n.log module¶
logging utilities for translation
- class oslo_i18n.log.TranslationHandler(locale=None, target=None)¶
Bases:
MemoryHandler
Handler that translates records before logging them.
When lazy translation is enabled in the application (see
enable_lazy()
), theTranslationHandler
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.
- emit(record)¶
Emit a record.
Append the record. If shouldFlush() tells us to, call flush() to process the buffer.
- setFormatter(fmt)¶
Set the formatter for this handler.