MessageFormat2
![Checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)
This is a Python implementation of the Unicode Message Format 2.0
specification.
Message Format 2.0 (MF2) is a new iteration of Message Format which aims to be
more expressive and flexible than both Message Format 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
- Several builtin formatters and selectors described by the
spec
and the possibility to write custom formatters/selectors
- Helper classes to inspect and transform the message data
model
Installation
Via pip for Python >= 3.12
pip install messageformat2
Examples
Simple messages
from messageformat2 import format_message
format_message("Hello, {$name}!", {"name": "Alice"})
Messages can be reused with different inputs.
from messageformat2 import Message
message = Message("Hello, {$name}!")
message.format({"name": "Alice"})
message.format({"name": "Bob"})
Built-in formatters
There are several builtin locale-aware formatters available.
from datetime import datetime
from messageformat2 import Message
message = Message("Today's date is {$now :date}")
now = datetime.now()
message.format({"now": now}, locale='en_US')
message.format({"now": now}, locale='en_GB')
Plural support
MF2 supports pluralization using built-in or custom selectors.
from messageformat2 import Message
msg = Message ("""\
.match {$count :number}
one {{You have {$count} notification.}}
* {{You have {$count} notifications.}}
""")
msg.format({"count": 42}, locale='en_US')
msg.format({"count": 1}, locale='en_US')
Complete documentation is available here.