Skip to main content

MessageFormat2 Documentation Status CI

This is a Python implementation of the Unicode MessageFormat specification (LDML 49).

Unicode MessageFormat (MessageFormat 2.0, MF2) is a new iteration of MessageFormat which aims to be more expressive and flexible than both MessageFormat 1.0 and, in the context of Python, gettext.

If you would like to learn more about MF2 itself head over to the Unicode technical specification.

Features

The library includes:

  • A message parser and a formatter, passing the official test suite
  • All default functions described by the spec (:string, :number, :integer, :offset, :percent, :currency, :date, :time, :datetime) and the possibility to write custom formatters/selectors
  • Formatting with fallback values and error reporting, optional bidi isolation of placeholders
  • Helper classes to inspect and transform the message data model

Documentation

Installation

Via pip for Python >= 3.12

pip install messageformat2

Examples

Simple messages

from messageformat2 import format_message

format_message("Hello, {$name}!", {"name": "Alice"})  # -> "Hello, Alice!"

Messages can be reused with different inputs.

from messageformat2 import Message

message = Message("Hello, {$name}!")
message.format({"name": "Alice"})  # -> "Hello, Alice!"
message.format({"name": "Bob"})  # -> "Hello, Bob!"

Built-in formatters

There are several builtin locale-aware formatters available.

from datetime import date
from messageformat2 import Message

message = Message("Today's date is {$today :date}")
today = date(2024, 5, 10)
message.format({"today": today}, locale="en_US")
# -> "Today's date is May 10, 2024"
message.format({"today": today}, locale="en_GB")
# -> "Today's date is 10 May 2024"

Message("{$price :currency currency=EUR}").format({"price": 42}, locale="de")
# -> "42,00\xa0€"

Plural support

MF2 supports pluralization using built-in or custom selectors.

from messageformat2 import Message

msg = Message ("""\
.input {$count :number}
.match $count
one {{You have {$count} notification.}}
*   {{You have {$count} notifications.}}
""")

msg.format({"count": 42}, locale='en_US') # -> "You have 42 notifications."
msg.format({"count": 1}, locale='en_US') # -> "You have 1 notification."

Error handling

By default the first error is raised. To get the spec's fallback behaviour instead ({$name} for a missing variable, etc.) and collect the errors, pass an on_error callback:

errors = []
Message("Hello, {$name}!").format({}, on_error=errors.append)  # -> "Hello, {$name}!"
errors  # -> [UnresolvedVariable('Unresolved variable: name')]

Bidirectional text

The spec's Default Bidi Strategy (wrapping placeholders in Unicode isolate controls so that mixed-direction text displays correctly) is available with bidi_isolation=True:

Message("Hello, {$name}!").format({"name": "Alice"}, bidi_isolation=True)
# -> "Hello, \u2068Alice\u2069!"

Complete documentation is available here.

Metadata

Release files for messageformat2 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for messageformat2 1.1.0
File Size Uploaded Provenance Actions
messageformat2-1.1.0.tar.gz 114.6 kB With provenance Details for messageformat2-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for messageformat2 1.1.0
File Interpreter ABI Platform Actions
messageformat2-1.1.0-py3-none-any.whl Python 3 none any Details for messageformat2-1.1.0-py3-none-any.whl

Total release size: 145.4 kB

Release files / messageformat2-1.1.0.tar.gz

Download URL messageformat2-1.1.0.tar.gz
Size 114.6 kB
Tags Source
SHA-256 checksum
How to use checksums
81750770e7dac7264e74a4c83a0aecd44c1600f13b8f3bed3e2600714a4c5e43
BLAKE2b-256 checksum
How to use checksums
168d1cde8a5135ee3a124acdfaac69461c52716a5aabcb6600a1f4c559d7c11a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

Release files / messageformat2-1.1.0-py3-none-any.whl

Download URL messageformat2-1.1.0-py3-none-any.whl
Size 30.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
879abeeabfa31656de6e67278f6d362751b5c9f798f963e78fe7866719154e74
BLAKE2b-256 checksum
How to use checksums
4a7b329be5198b6c591f091fc78b1c395a409e2ea1161070ac360d1aa853ae99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

Security for messageformat2 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for messageformat2-1.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page