flufl-i18n

6.0.0 · active · verified Thu Apr 16

flufl.i18n is a Python library providing a high-level, convenient API for managing internationalization (i18n) translation contexts. It supports both single-context applications like command-line tools and more complex, multi-context applications such as servers. The current version is 6.0.0, released in November 2025, with a consistent release cadence for updates and Python version compatibility.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic setup for a single-context application using `initialize()` and the `_()` translation function. It simulates setting environment variables (`LANG`, `LOCPATH`) and binding the translation function. For actual use, you would prepare `.po` and `.mo` files for your application.

import os
from flufl.i18n import initialize

# --- Create a dummy messages.py for demonstration ---
# In a real application, this would be a Python package
# containing your compiled .mo files in subdirectories.
# e.g., messages/en/LC_MESSAGES/my_app_name.mo
#      messages/xx/LC_MESSAGES/my_app_name.mo (for our example 'xx')

# Simulate a simple translation catalog (rot13 for 'xx' locale)
# In a real scenario, this would involve gettext and .po/.mo files.
class MockTranslator:
    def gettext(self, message):
        if os.environ.get('LANG') == 'xx':
            return message.encode('rot13').decode('utf-8')
        return message

class MockApplication:
    def __init__(self, name, localepath):
        self.name = name
        self.localepath = localepath

    def gettext(self):
        return MockTranslator()

# Patch initialize for this example to use our mock app
# In a real app, initialize() would handle catalog loading from LOCPATH
def _mock_initialize(app_name, package_path=None):
    print(f"Initializing i18n for app: {app_name}")
    print(f"Using LOCPATH: {os.environ.get('LOCPATH')}")
    print(f"Using LANG: {os.environ.get('LANG')}")
    return MockApplication(app_name, package_path)

flufl.i18n.initialize = _mock_initialize
# --- End of dummy setup ---


# Example usage:
app_name = 'my_app_name'

# 1. Set up environment variables (typically done outside the script)
#    For demonstration, we set them here.

# IMPORTANT: Use os.path.dirname(__file__) to point to where your 'messages' package is
# For this quickstart, we're simulating, so package_path isn't strictly used by our mock.
package_path = os.path.join(os.path.dirname(__file__), 'messages') # Placeholder

os.environ['LANG'] = 'en' # Default language
os.environ['LOCPATH'] = package_path # Path to your translation catalogs

# Initialize the application and bind the translation function
_ = initialize(app_name, package_path).gettext()

print(f"English: {_('Hello, world!')}")
print(f"English: {_('How are you?')}")

# 2. Switch language context (e.g., for a web server or specific user session)
os.environ['LANG'] = 'xx' # A faux language for demonstration

# Re-initialize or push a new context (initialize here for simplicity in single-context example)
_ = initialize(app_name, package_path).gettext()

print(f"Faux (XX): {_('Hello, world!')}")
print(f"Faux (XX): {_('How are you?')}")

# Clean up environment variables (optional)
del os.environ['LANG']
del os.environ['LOCPATH']

view raw JSON →