Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomasr8/messageformat2
Python implementation of the Unicode Message Format 2.0 specification
https://github.com/tomasr8/messageformat2
i18n icu internationalization localization messageformat translation
Last synced: 18 days ago
JSON representation
Python implementation of the Unicode Message Format 2.0 specification
- Host: GitHub
- URL: https://github.com/tomasr8/messageformat2
- Owner: tomasr8
- License: mit
- Created: 2024-05-06T22:00:16.000Z (6 months ago)
- Default Branch: master
- Last Pushed: 2024-05-26T08:45:12.000Z (6 months ago)
- Last Synced: 2024-09-21T10:28:21.861Z (about 2 months ago)
- Topics: i18n, icu, internationalization, localization, messageformat, translation
- Language: Python
- Homepage:
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# MessageFormat2 [![Documentation Status](https://readthedocs.org/projects/messageformat2/badge/?version=latest)](https://messageformat2.readthedocs.io/en/latest/?badge=latest) [![CI](https://github.com/tomasr8/messageformat2/workflows/CI/badge.svg)](https://github.com/tomasr8/messageformat2/workflows/CI/badge.svg) [![Checked with pyright](https://microsoft.github.io/pyright/img/pyright_badge.svg)](https://microsoft.github.io/pyright/)
This is a Python implementation of the [Unicode Message Format 2.0
specification](https://www.unicode.org/reports/tr35/tr35-72/tr35-messageFormat.html).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](https://www.unicode.org/reports/tr35/tr35-72/tr35-messageFormat.html).## Features
The library includes:
- A message parser and a formatter
- Several builtin formatters and selectors described by the
[spec](https://www.unicode.org/reports/tr35/tr35-72/tr35-messageFormat.html#function-registry)
and the possibility to write custom formatters/selectors
- Helper classes to inspect and transform the message [data
model](https://www.unicode.org/reports/tr35/tr35-72/tr35-messageFormat.html#interchange-data-model)## [Documentation](https://messageformat2.readthedocs.io/en/latest/)
## Installation
Via pip for Python `>= 3.12`
```sh
pip install messageformat2
```## Examples
### Simple messages
```python
from messageformat2 import format_messageformat_message("Hello, {$name}!", {"name": "Alice"}) # -> "Hello, Alice!"
```Messages can be reused with different inputs.
```python
from messageformat2 import Messagemessage = 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.
```python
from datetime import datetime
from messageformat2 import Messagemessage = 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.
```python
from messageformat2 import Messagemsg = 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](https://messageformat2.readthedocs.io/en/latest/).