Name | pluralize JSON |
Version |
20240519.1
JSON |
| download |
home_page | None |
Summary | i18n + pluralization library with multi-plural form support and thread safe for web apps |
upload_time | 2024-05-19 18:33:18 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.7 |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Pluralize
Pluralize is a Python library for Internationalization (i18n) and Pluralization.
The library assumes a folder (for exaple "translations") that contains files like:
```sh
it.json
it-IT.json
fr.json
fr-FR.json
(etc)
```
Each file has the following structure, for example for Italian (it.json):
```json
{"dog": {"0": "no cane", "1": "un cane", "2": "{n} cani", "10": "tantissimi cani"}}
```
The top level keys are the expressions to be translated and the associated value/dictionary maps a number to a translation.
Different translations correspond to different plural forms of the expression,
Here is another example for the word "bed" in Czech
```json
{"bed": {"0": "no postel", "1": "postel", "2": "postele", "5": "postelĂ"}}
```
To translate and pluralize a string "dog" one simply wraps the string in the T operator as follows:
```python
>>> from pluralize import Translator
>>> T = Translator('translations')
>>> dog = T("dog")
>>> print(dog)
dog
>>> T.select('it')
>>> print(dog)
un cane
>>> print(dog.format(n=0))
no cane
>>> print(dog.format(n=1))
un cane
>>> print(dog.format(n=5))
5 cani
>>> print(dog.format(n=20))
tantissimi cani
```
The string can contain multiple placeholders but the {n} placeholder is special because
the variable called "n" is used to determine the pluralization by best match (max dict key <= n).
T(...) objects can be added together with each other and with string, like regular strings.
T.select(s) can parse a string s following the HTTP accept language format.
## Update the translation files
Find all strings wrapped in T(...) in .py, .html, and .js files:
```python
matches = T.find_matches('path/to/app/folder')
```
Add newly discovered entries in all supported languages
```python
T.update_languages(matches)
```
Add a new supported language (for example german, "de")
```python
T.languages['de'] = {}
```
Make sure all languages contain the same origin expressions
```python
known_expressions = set()
for language in T.languages.values():
for expression in language:
known_expressions.add(expression)
T.update_languages(known_expressions))
```
Finally save the changes:
```python
T.save('translations')
```
Raw data
{
"_id": null,
"home_page": null,
"name": "pluralize",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Massimo Di Pierro <massimo.dipierro@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/d1/42/cd68a0fc4d6fa2f9fed11c1830d9b0bd6a405a4ffab9cfd6fe123e217fb9/pluralize-20240519.1.tar.gz",
"platform": null,
"description": "# Pluralize\n\nPluralize is a Python library for Internationalization (i18n) and Pluralization.\n\nThe library assumes a folder (for exaple \"translations\") that contains files like:\n\n```sh\nit.json\nit-IT.json\nfr.json\nfr-FR.json\n(etc)\n```\n\nEach file has the following structure, for example for Italian (it.json):\n\n```json\n{\"dog\": {\"0\": \"no cane\", \"1\": \"un cane\", \"2\": \"{n} cani\", \"10\": \"tantissimi cani\"}}\n```\n\nThe top level keys are the expressions to be translated and the associated value/dictionary maps a number to a translation.\nDifferent translations correspond to different plural forms of the expression,\n\nHere is another example for the word \"bed\" in Czech\n\n```json\n{\"bed\": {\"0\": \"no postel\", \"1\": \"postel\", \"2\": \"postele\", \"5\": \"postel\u00ed\"}}\n```\n\nTo translate and pluralize a string \"dog\" one simply wraps the string in the T operator as follows:\n\n```python\n>>> from pluralize import Translator\n>>> T = Translator('translations')\n>>> dog = T(\"dog\")\n>>> print(dog)\ndog\n>>> T.select('it')\n>>> print(dog)\nun cane\n>>> print(dog.format(n=0))\nno cane\n>>> print(dog.format(n=1))\nun cane\n>>> print(dog.format(n=5))\n5 cani\n>>> print(dog.format(n=20))\ntantissimi cani\n```\n\nThe string can contain multiple placeholders but the {n} placeholder is special because\nthe variable called \"n\" is used to determine the pluralization by best match (max dict key <= n).\n\nT(...) objects can be added together with each other and with string, like regular strings.\n\nT.select(s) can parse a string s following the HTTP accept language format.\n\n## Update the translation files\n\nFind all strings wrapped in T(...) in .py, .html, and .js files:\n```python\nmatches = T.find_matches('path/to/app/folder')\n```\n\nAdd newly discovered entries in all supported languages\n```python\nT.update_languages(matches)\n```\n\nAdd a new supported language (for example german, \"de\")\n\n```python\nT.languages['de'] = {}\n```\n\nMake sure all languages contain the same origin expressions\n```python\nknown_expressions = set()\nfor language in T.languages.values():\n for expression in language:\n known_expressions.add(expression)\nT.update_languages(known_expressions))\n```\n\nFinally save the changes:\n\n```python\nT.save('translations')\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "i18n + pluralization library with multi-plural form support and thread safe for web apps",
"version": "20240519.1",
"project_urls": {
"Bug Tracker": "https://github.com/web2py/yatl/pluralize",
"Homepage": "https://github.com/web2py/pluralize"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4e1d9c915340fc783357f5a396ea8f1b43b68f8e703c6f47e86207ce1b4af004",
"md5": "7ed374adda6270e424eb61c38e72388b",
"sha256": "1b04501c7bd99c62b8422b6065fa1eda4f431f965ef6bc3b79a8b0f14202dbc4"
},
"downloads": -1,
"filename": "pluralize-20240519.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7ed374adda6270e424eb61c38e72388b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 4312,
"upload_time": "2024-05-19T18:33:16",
"upload_time_iso_8601": "2024-05-19T18:33:16.245584Z",
"url": "https://files.pythonhosted.org/packages/4e/1d/9c915340fc783357f5a396ea8f1b43b68f8e703c6f47e86207ce1b4af004/pluralize-20240519.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d142cd68a0fc4d6fa2f9fed11c1830d9b0bd6a405a4ffab9cfd6fe123e217fb9",
"md5": "48c812231f21894379f8fbd8c2a495c2",
"sha256": "48942635697c33acbc0db130f664778a470d65dbfc6f742d58bafd5010e8868e"
},
"downloads": -1,
"filename": "pluralize-20240519.1.tar.gz",
"has_sig": false,
"md5_digest": "48c812231f21894379f8fbd8c2a495c2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 4537,
"upload_time": "2024-05-19T18:33:18",
"upload_time_iso_8601": "2024-05-19T18:33:18.039282Z",
"url": "https://files.pythonhosted.org/packages/d1/42/cd68a0fc4d6fa2f9fed11c1830d9b0bd6a405a4ffab9cfd6fe123e217fb9/pluralize-20240519.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-19 18:33:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "web2py",
"github_project": "yatl",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "pluralize"
}