# I18n for Python
[![pypi version](https://badge.fury.io/py/i18nx.svg)](https://pypi.org/project/i18nx/)
[![Build status](https://gitlab.com/demsking/i18nx/badges/main/pipeline.svg)](https://gitlab.com/demsking/i18nx/pipelines)
[![Test Coverage](https://gitlab.com/demsking/i18nx/badges/main/coverage.svg)](https://gitlab.com/demsking/i18nx/-/jobs)
[![Buy me a beer](https://img.shields.io/badge/Buy%20me-a%20beer-1f425f.svg)](https://www.buymeacoffee.com/demsking)
Lightweight i18n for Python.
## Install
```sh
pip install i18nx
```
## Usage
```python
from i18nx import I18n
i18n = I18n(
locale = 'en',
fallback = 'fr',
translations = {
'en': { 'message': { 'hello': 'Hello World!' } },
'fr': { 'message': { 'hello': 'Bonjour le monde !' } },
},
)
print(i18n.tr("message.hello")) # Hello World!
```
## Message Format Syntax
**Interpolation**
`i18nx` use the Mustache like placeholders `{}` syntax for interpolation.
```python
from i18nx import I18n
i18n = I18n(
locale = 'en',
fallback = 'fr',
translations = {
'en': { 'message': { 'hello': 'Hello {name}!' } },
'fr': { 'message': { 'hello': 'Bonjour {name} !' } },
},
)
print(i18n.tr("message.hello", name = 'Mario')) # Hello Mario!
```
**Pluralization**
Use a pipe `|` separator in combination with the param `count` to define
plurals on the locale translations.
```python
from i18nx import I18n
i18n = I18n(
locale = 'en',
fallback = 'fr',
translations = {
'en': {
'car': 'car | cars',
'apple': 'no apples | one apple | {count} apples',
},
},
)
print(i18n.tr("car")) # 'car'
print(i18n.tr("car", count = 0)) # 'car'
print(i18n.tr("car", count = 1)) # 'car'
print(i18n.tr("car", count = 2)) # 'cars'
print(i18n.tr("apple", count = 0)) # 'no apples'
print(i18n.tr("apple", count = 1)) # 'one apple'
print(i18n.tr("apple", count = 15)) # '15 apples'
```
**List of Messages**
```python
from i18nx import I18n
i18n = I18n(
locale = 'en',
fallback_locale = 'fr',
translations = {
'en': {
"greetings": [
"Hey {firtname}!",
"Hi {firtname}!",
],
},
},
)
print(i18n.tr("greetings.0", firtname = 'Mario')) # 'Hey Mario!'
print(i18n.tr("greetings.1", firtname = 'Mario')) # 'Hi Mario!'
```
## I18n API
```coffee
interface class I18n:
constructor(locale: str, fallback_locale: str, translations: Dict[str, Dict[str, Any]], show_warning = False)
# Active locale
@property locale: str
# Fallback locale
@property fallback_locale: str
# Available locales
@getter available_locales: List[str]
# Raw translations object for the active locale
@getter raw: dict
# Get translated text for the given dot path
@method tr(path: str, **params) -> str
# Get the raw message for the given dot path
@method get_raw_message(path: str) -> Union[str, None]
# Format the given raw message with the given params
@method format_raw_message(message: str, **params) -> str
```
## Development Setup
1. [Install Nix Package Manager](https://nixos.org/manual/nix/stable/installation/installing-binary.html)
2. [Install `direnv` with your OS package manager](https://direnv.net/docs/installation.html#from-system-packages)
3. [Hook it `direnv` into your shell](https://direnv.net/docs/hook.html)
4. At the top-level of your project run:
```sh
direnv allow
```
The next time your launch your terminal and enter the top-level of your
project, `direnv` will check for changes.
**Scripts**
```sh
# run tests
make test
# run tests with coverage
make coverage
# run linter
make lint
# run build process
make dist
# publish
make publish
```
## License
Under the MIT license.
See [LICENSE](https://gitlab.com/demsking/i18nx/blob/main/LICENSE)
file for more details.
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/demsking/i18nx",
"name": "i18nx",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": "",
"keywords": "i18n internationalization translate",
"author": "S\u00e9bastien Demanou",
"author_email": "demsking@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/4f/49/f49c6e39a51fcfaa200f21195ad94b70c6ff691ecdaae02aa6027c710a76/i18nx-1.3.0.tar.gz",
"platform": null,
"description": "# I18n for Python\n\n[![pypi version](https://badge.fury.io/py/i18nx.svg)](https://pypi.org/project/i18nx/)\n[![Build status](https://gitlab.com/demsking/i18nx/badges/main/pipeline.svg)](https://gitlab.com/demsking/i18nx/pipelines)\n[![Test Coverage](https://gitlab.com/demsking/i18nx/badges/main/coverage.svg)](https://gitlab.com/demsking/i18nx/-/jobs)\n[![Buy me a beer](https://img.shields.io/badge/Buy%20me-a%20beer-1f425f.svg)](https://www.buymeacoffee.com/demsking)\n\nLightweight i18n for Python.\n\n## Install\n\n```sh\npip install i18nx\n```\n\n## Usage\n\n```python\nfrom i18nx import I18n\n\ni18n = I18n(\n locale = 'en',\n fallback = 'fr',\n translations = {\n 'en': { 'message': { 'hello': 'Hello World!' } },\n 'fr': { 'message': { 'hello': 'Bonjour le monde !' } },\n },\n)\n\nprint(i18n.tr(\"message.hello\")) # Hello World!\n```\n\n## Message Format Syntax\n\n**Interpolation**\n\n`i18nx` use the Mustache like placeholders `{}` syntax for interpolation.\n\n```python\nfrom i18nx import I18n\n\ni18n = I18n(\n locale = 'en',\n fallback = 'fr',\n translations = {\n 'en': { 'message': { 'hello': 'Hello {name}!' } },\n 'fr': { 'message': { 'hello': 'Bonjour {name} !' } },\n },\n)\n\nprint(i18n.tr(\"message.hello\", name = 'Mario')) # Hello Mario!\n```\n\n**Pluralization**\n\nUse a pipe `|` separator in combination with the param `count` to define\nplurals on the locale translations.\n\n```python\nfrom i18nx import I18n\n\ni18n = I18n(\n locale = 'en',\n fallback = 'fr',\n translations = {\n 'en': {\n 'car': 'car | cars',\n 'apple': 'no apples | one apple | {count} apples',\n },\n },\n)\n\nprint(i18n.tr(\"car\")) # 'car'\nprint(i18n.tr(\"car\", count = 0)) # 'car'\nprint(i18n.tr(\"car\", count = 1)) # 'car'\nprint(i18n.tr(\"car\", count = 2)) # 'cars'\nprint(i18n.tr(\"apple\", count = 0)) # 'no apples'\nprint(i18n.tr(\"apple\", count = 1)) # 'one apple'\nprint(i18n.tr(\"apple\", count = 15)) # '15 apples'\n```\n\n**List of Messages**\n\n```python\nfrom i18nx import I18n\n\ni18n = I18n(\n locale = 'en',\n fallback_locale = 'fr',\n translations = {\n 'en': {\n \"greetings\": [\n \"Hey {firtname}!\",\n \"Hi {firtname}!\",\n ],\n },\n },\n)\n\nprint(i18n.tr(\"greetings.0\", firtname = 'Mario')) # 'Hey Mario!'\nprint(i18n.tr(\"greetings.1\", firtname = 'Mario')) # 'Hi Mario!'\n```\n\n## I18n API\n\n```coffee\ninterface class I18n:\n constructor(locale: str, fallback_locale: str, translations: Dict[str, Dict[str, Any]], show_warning = False)\n\n # Active locale\n @property locale: str\n\n # Fallback locale\n @property fallback_locale: str\n\n # Available locales\n @getter available_locales: List[str]\n\n # Raw translations object for the active locale\n @getter raw: dict\n\n # Get translated text for the given dot path\n @method tr(path: str, **params) -> str\n\n # Get the raw message for the given dot path\n @method get_raw_message(path: str) -> Union[str, None]\n\n # Format the given raw message with the given params\n @method format_raw_message(message: str, **params) -> str\n```\n\n## Development Setup\n\n1. [Install Nix Package Manager](https://nixos.org/manual/nix/stable/installation/installing-binary.html)\n\n2. [Install `direnv` with your OS package manager](https://direnv.net/docs/installation.html#from-system-packages)\n\n3. [Hook it `direnv` into your shell](https://direnv.net/docs/hook.html)\n\n4. At the top-level of your project run:\n ```sh\n direnv allow\n ```\n\n The next time your launch your terminal and enter the top-level of your\n project, `direnv` will check for changes.\n\n**Scripts**\n\n```sh\n# run tests\nmake test\n\n# run tests with coverage\nmake coverage\n\n# run linter\nmake lint\n\n# run build process\nmake dist\n\n# publish\nmake publish\n```\n\n## License\n\nUnder the MIT license.\nSee [LICENSE](https://gitlab.com/demsking/i18nx/blob/main/LICENSE)\nfile for more details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Lightweight i18n for Python",
"version": "1.3.0",
"project_urls": {
"Documentation": "https://gitlab.com/demsking/i18nx/-/blob/main/README.md",
"Homepage": "https://gitlab.com/demsking/i18nx",
"Say Thanks!": "https://www.buymeacoffee.com/demsking",
"Source": "https://gitlab.com/demsking/i18nx",
"Tracker": "https://gitlab.com/demsking/i18nx/-/issues"
},
"split_keywords": [
"i18n",
"internationalization",
"translate"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1577cb8d78a845d48fdd624bc9741337c2e59a95af8d5e8e5efbb937ae0bf261",
"md5": "248255a8674cc1db62e6cc2df65b35af",
"sha256": "a5afe786f5ff36a5a75a0ca6edc404b670bdacc071b7a3adf63cb917fb9b2b9f"
},
"downloads": -1,
"filename": "i18nx-1.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "248255a8674cc1db62e6cc2df65b35af",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 4940,
"upload_time": "2023-05-24T17:18:49",
"upload_time_iso_8601": "2023-05-24T17:18:49.705552Z",
"url": "https://files.pythonhosted.org/packages/15/77/cb8d78a845d48fdd624bc9741337c2e59a95af8d5e8e5efbb937ae0bf261/i18nx-1.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4f49f49c6e39a51fcfaa200f21195ad94b70c6ff691ecdaae02aa6027c710a76",
"md5": "3d4484c4af8c6c1311ac43cc2daf7b16",
"sha256": "c1b40379773cb48f4ad57aa35aae42783f8001f866287dfbbe0357abc8b284da"
},
"downloads": -1,
"filename": "i18nx-1.3.0.tar.gz",
"has_sig": false,
"md5_digest": "3d4484c4af8c6c1311ac43cc2daf7b16",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 4812,
"upload_time": "2023-05-24T17:18:50",
"upload_time_iso_8601": "2023-05-24T17:18:50.995616Z",
"url": "https://files.pythonhosted.org/packages/4f/49/f49c6e39a51fcfaa200f21195ad94b70c6ff691ecdaae02aa6027c710a76/i18nx-1.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-24 17:18:50",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "demsking",
"gitlab_project": "i18nx",
"lcname": "i18nx"
}