Skip to main content

Python implementation of Unicode MessageFormat 2.0

Project description

MessageFormat2 Documentation Status CI

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

messageformat2-0.1.1.tar.gz (62.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

messageformat2-0.1.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

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

Hashes for messageformat2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 531e34d9133477673a5416372e9b0f95d77066bf0ea2eb74ae760488fb8188e9
MD5 b25e826db43a0bdedd12fecf67076eec
BLAKE2b-256 bb38035a3be3df2c8edda4ec2b03384cd9c832afe2612ed088dce569659bcdd4

See more details on using hashes here.

Provenance

The following attestation bundles were made for messageformat2-0.1.1.tar.gz:

Publisher: release.yml on tomasr8/messageformat2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for messageformat2-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5fd88485fc2fee071a64e25d0759a036faa6a87df8540ff91f7d60dadfae5acf
MD5 6ee24f8436ec503e0a893c02d4f439cf
BLAKE2b-256 10161c4262cdbd85c0f71db6a527f85a2245ef5222d31a98ee09ac680bd97813

See more details on using hashes here.

Provenance

The following attestation bundles were made for messageformat2-0.1.1-py3-none-any.whl:

Publisher: release.yml on tomasr8/messageformat2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page