Zope Internationalization Support

6.0 · active · verified Thu Apr 16

zope.i18n implements several APIs related to internationalization and localization, offering features such as locale objects based on ICU, Gettext-based message catalogs, and locale discovery for web requests. Version 6.0, released recently, continues its active development with regular updates to support new Python versions and address architectural improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a translatable message using `MessageFactory` (commonly aliased as `_`) and then translate it using the `translate` function. It includes a minimal mock for the request context, which is typically provided by the Zope application server for language negotiation. It also hints at the environment variable for automatic MO file compilation.

import os
from zope.i18n import translate
from zope.i18nmessageid import MessageFactory

# Simulate a request object for translation context
class MockRequest:
    def __init__(self, lang):
        self.LANGUAGE_NEGOTIATED = lang
        self.locale = MockLocale(lang)

class MockLocale:
    def __init__(self, lang):
        self.getLocaleID = lambda: lang

# Define a message factory for your domain
_ = MessageFactory('my.application')

# A translatable message
msg = _('hello_world_id', default='Hello, World!', mapping={'name': 'User'})

# Example of a simple translation service (in a real Zope app, this would be set up)
def get_translation_service(request):
    # In a real Zope setup, this would resolve the utility
    # For this example, we just return the 'translate' function itself
    return lambda message, target_language=None, default=None, mapping=None, context=None, domain=None:
        if target_language == 'de':
            return f"Hallo, {mapping.get('name', '')}!"
        elif target_language == 'fr':
            return f"Bonjour, {mapping.get('name', '')}!"
        return default if default else str(message) # Fallback

# Translate the message
# Often, the request context implicitly provides the target_language

mock_request_en = MockRequest('en')
mock_request_de = MockRequest('de')
mock_request_fr = MockRequest('fr')

# Using translate with a mocked context and explicit target language
translated_en = translate(msg, target_language='en', context=mock_request_en, mapping={'name': 'Alice'})
translated_de = translate(msg, target_language='de', context=mock_request_de, mapping={'name': 'Bob'})
translated_fr = translate(msg, target_language='fr', context=mock_request_fr, mapping={'name': 'Charlie'})

print(f"English: {translated_en}")
print(f"German: {translated_de}")
print(f"French: {translated_fr}")

# Example for automatic MO file compilation (if 'compile' extra is installed)
os.environ['ZOPE_I18N_COMPILE_MO_FILES'] = 'true'
# In a real application, MO files would now be compiled on startup if .po files exist
print(f"\nZOPE_I18N_COMPILE_MO_FILES env var set: {os.environ.get('ZOPE_I18N_COMPILE_MO_FILES')}")

view raw JSON →