Message Identifiers for internationalization
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
- deprecated The `MessageID` and `MessageIDFactory` APIs from `zope.i18nmessageid.messageid` were deprecated in favor of `Message` and `MessageFactory` (imported directly from `zope.i18nmessageid` or `zope.i18nmessageid.message`).
- gotcha This package only provides facilities for *declaring* message identifiers. It does not perform the actual translation of these messages into different languages. For translation, the `zope.i18n` package must be used in conjunction with message catalogs (e.g., PO/MO files).
- gotcha When passing a `Message` object to `zope.i18n.translate` (the separate translation utility) along with an explicit `mapping` argument, the `mapping` provided directly to `translate` might override or not correctly merge with the mapping already present in the `Message` object, potentially leading to lost substitutions.
Install
-
pip install zope.i18nmessageid
Imports
- MessageFactory
from zope.i18nmessageid.messageid import MessageIDFactory
from zope.i18nmessageid import MessageFactory
- Message
from zope.i18nmessageid.messageid import MessageID
from zope.i18nmessageid.message import Message
Quickstart
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'}>