Python implementation of Unicode MessageFormat 2.0
Project description
MessageFormat2
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
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 datetime
from messageformat2 import Message
message = Message("Today's date is {$now :date}")
now = datetime.now()
message.format({"now": now}, locale='en_US')
# -> Today's date is 5/10/2024
message.format({"now": now}, locale='en_GB')
# -> Today's date is 10/05/2024
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') # -> "You have 42 notifications."
msg.format({"count": 1}, locale='en_US') # -> "You have 1 notification."
Complete documentation is available here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file messageformat2-0.1.0.tar.gz
.
File metadata
- Download URL: messageformat2-0.1.0.tar.gz
- Upload date:
- Size: 31.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7eacaad6b9f47c15d554d1cc076ac99cc0ef0c81082757fdc31591a6b84edf6 |
|
MD5 | 8716b5808de74d09f54ee26d30ab8ca1 |
|
BLAKE2b-256 | cf9580899530c3e7642e80b7e4a2c292fed996c26f924638ecea468c6e4576e7 |
File details
Details for the file messageformat2-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: messageformat2-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbedf6e8fe91330d41fa96ed9729f9f92f68cd590ded0c4ad5591a9af7f73b2e |
|
MD5 | 2da4857402d334b0f0ff394e6156a5e9 |
|
BLAKE2b-256 | cff7c14c7be926afafc52bff8a4110393b111a6cba64ca9da0074db3c9b28786 |