Skip to main content

i18n + pluralization library with multi-plural form support and thread safe for web apps

Project description

Pluralize

Pluralize is a Python library for Internationalization (i18n) and Pluralization.

The library assumes a folder (for exaple "translations") that contains files like:

it.json
it-IT.json
fr.json
fr-FR.json
(etc)

Each file has the following structure, for example for Italian (it.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

{"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:

>>> 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:

matches = T.find_matches('path/to/app/folder')

Add newly discovered entries in all supported languages

T.update_languages(matches)

Add a new supported language (for example german, "de")

T.languages['de'] = {}

Make sure all languages contain the same origin expressions

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:

T.save('translations')

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

pluralize-20240519.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

pluralize-20240519.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file pluralize-20240519.3.tar.gz.

File metadata

  • Download URL: pluralize-20240519.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for pluralize-20240519.3.tar.gz
Algorithm Hash digest
SHA256 efb23e2304d816d4d8a3c9b60faac33a39e0d72067a184570ef4d57d519527d0
MD5 a22f013c58bfe4232725f42511ea6d29
BLAKE2b-256 95c659290019829430eb1b17064a41f32c8614a5082691a7b812a25efe5cf543

See more details on using hashes here.

File details

Details for the file pluralize-20240519.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pluralize-20240519.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8538b1e01d0251e86389ed9969dfb620ad66f2583f7b711d4278ba34bd7781e8
MD5 2f9710b8accaef25efaa4e765e8cf82c
BLAKE2b-256 70fecfbd43621f5a9b8a4edc5ebcbef5dd27664fcfe1352877a0b4cc347891c9

See more details on using hashes here.

Supported by

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