Oslo Internationalization Utilities
The oslo.i18n library provides utilities for internationalization (i18n) features, primarily for translating text strings within applications or libraries. It is part of the OpenStack Oslo project and is currently at version 6.7.2. New releases often align with OpenStack development cycles, indicating an active maintenance and development cadence.
Warnings
- gotcha Marker functions like `_()` must receive string literals as arguments, not variables. For example, `_('My message.')` is correct, while `_(variable_containing_msg)` is wrong, as translation tools rely on static string analysis.
- breaking Starting with the OpenStack Pike series, log translation guidelines changed significantly. Marker functions like `_LI()`, `_LW()`, `_LE()`, and `_LC()` should ONLY be used for messages that go *directly* and *exclusively* to the log. If a message might be exposed to the user (e.g., via an exception or API response), the primary `_()` marker MUST be used.
- gotcha Lazy translated message objects (returned by `_()` when lazy translation is enabled) do not support all string manipulation operations, specifically concatenation with the `+` operator. They are designed for string interpolation using the `%` operator or `.format()` (though `%` is more commonly shown in examples).
- deprecated The `oslo_i18n.gettextutils.Message` class was an internal implementation detail and was not part of the public API. Direct instantiation or reliance on this class is discouraged and may break in future versions.
Install
-
pip install oslo-i18n
Imports
- TranslatorFactory
from oslo_i18n import TranslatorFactory
- translate
from oslo_i18n import translate
- _ (primary translation marker)
from myapp._i18n import _
- _LI, _LW, _LE, _LC (log translation markers)
from myapp._i18n import _LI
Quickstart
import os
from oslo_i18n import TranslatorFactory, enable_lazy, translate
# 1. Create an integration module (conceptually, e.g., myapp._i18n.py)
# In a real project, this would be in a separate file and imported.
DOMAIN = 'myproject'
_translators = TranslatorFactory(domain=DOMAIN)
_ = _translators.primary # Primary translation marker
_LW = _translators.log_warning # Log warning translation marker
# 2. Enable lazy translation (optional, but common)
enable_lazy()
# 3. Mark strings for translation
message = _('Hello, %(name)s!') % {'name': 'World'}
warning_message = _LW('Something went wrong: %s') % 'file not found'
# 4. Simulate a different locale for translation at display/log time
# In a real app, this might come from user settings or request headers
current_locale = os.environ.get('TEST_LOCALE', 'en_US') # Example: 'es_ES'
# 5. Translate and display
# Note: 'message' is a lazy translation object until explicitly translated or cast to string.
# The 'translate' function is used when the target locale is known.
translated_message = translate(message, desired_locale=current_locale)
print(f"Original (lazy) message type: {type(message)}")
print(f"Translated message for '{current_locale}': {translated_message}")
# Log messages would typically be handled by a logging configuration
# with a TranslationHandler, but for demonstration, we can show direct translation.
print(f"Translated log warning for '{current_locale}': {translate(warning_message, desired_locale=current_locale)}")