Message Identifiers for internationalization

8.2 · active · verified Wed Apr 15

zope.i18nmessageid is a Python library, currently at version 8.2, that provides facilities for declaring translatable message identifiers within program source text. These message IDs encapsulate the untranslated string, a translation domain, and optional default values or substitutions. While this package handles the *declaration* of messages, their actual *translation* is the responsibility of the complementary zope.i18n package. It is actively maintained by the Zope Foundation.

Warnings

Install

Imports

Quickstart

To use zope.i18nmessageid, you typically import `MessageFactory`, create an instance bound to your application's translation domain, and then use this factory to mark strings for translation. The resulting objects are `Message` instances, which carry the original string, domain, and any substitution mappings. Actual translation into a target language requires the `zope.i18n` package.

from zope.i18nmessageid import MessageFactory

# Define a message factory for your application's domain
# This is conventionally named '_' in many i18n contexts.
_ = MessageFactory('my.application.domain')

# Create a translatable message ID
greeting_msg = _('Hello, world!')
print(f"Raw Message ID: {greeting_msg!r}")
# Expected output for MessageID: <MessageID u'Hello, world!' domain='my.application.domain'>

# Message IDs can include variables for substitution
variable_msg = _('Welcome, ${name}!', mapping={'name': 'User'})
print(f"Raw Message ID with mapping: {variable_msg!r}")
# Expected output: <MessageID u'Welcome, ${name}!' domain='my.application.domain' mapping={'name': 'User'}>

view raw JSON →