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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file messageformat2-0.1.1.tar.gz.
File metadata
- Download URL: messageformat2-0.1.1.tar.gz
- Upload date:
- Size: 62.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
531e34d9133477673a5416372e9b0f95d77066bf0ea2eb74ae760488fb8188e9
|
|
| MD5 |
b25e826db43a0bdedd12fecf67076eec
|
|
| BLAKE2b-256 |
bb38035a3be3df2c8edda4ec2b03384cd9c832afe2612ed088dce569659bcdd4
|
Provenance
The following attestation bundles were made for messageformat2-0.1.1.tar.gz:
Publisher:
release.yml on tomasr8/messageformat2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
messageformat2-0.1.1.tar.gz -
Subject digest:
531e34d9133477673a5416372e9b0f95d77066bf0ea2eb74ae760488fb8188e9 - Sigstore transparency entry: 738784809
- Sigstore integration time:
-
Permalink:
tomasr8/messageformat2@a221107ec7d1e85890613c2615ea345fcaab1a38 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/tomasr8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a221107ec7d1e85890613c2615ea345fcaab1a38 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file messageformat2-0.1.1-py3-none-any.whl.
File metadata
- Download URL: messageformat2-0.1.1-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fd88485fc2fee071a64e25d0759a036faa6a87df8540ff91f7d60dadfae5acf
|
|
| MD5 |
6ee24f8436ec503e0a893c02d4f439cf
|
|
| BLAKE2b-256 |
10161c4262cdbd85c0f71db6a527f85a2245ef5222d31a98ee09ac680bd97813
|
Provenance
The following attestation bundles were made for messageformat2-0.1.1-py3-none-any.whl:
Publisher:
release.yml on tomasr8/messageformat2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
messageformat2-0.1.1-py3-none-any.whl -
Subject digest:
5fd88485fc2fee071a64e25d0759a036faa6a87df8540ff91f7d60dadfae5acf - Sigstore transparency entry: 738784814
- Sigstore integration time:
-
Permalink:
tomasr8/messageformat2@a221107ec7d1e85890613c2615ea345fcaab1a38 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/tomasr8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a221107ec7d1e85890613c2615ea345fcaab1a38 -
Trigger Event:
workflow_dispatch
-
Statement type: