Skip to main content

i18nice tests Coverage Status

This library provides i18n functionality for Python 3 out of the box. The usage is mostly based on Rails i18n library.

Installation

Just run

pip install i18nice

If you want to use YAML to store your translations, use

pip install i18nice[YAML]

Usage

Basic usage

The simplest, though not very useful usage would be

import i18n
i18n.add_translation('foo', 'bar')
i18n.t('foo') # bar

Using translation files

YAML and JSON formats are supported to store translations. With the default configuration, if you have the following foo.en.yml file

en:
  hi: Hello world !

in /path/to/translations folder, you simply need to add the folder to the translations path.

import i18n
i18n.load_path.append('/path/to/translations')
i18n.t('foo.hi') # Hello world !

Please note that YAML format is used as default file format if you have yaml module installed. If both yaml and json modules available and you want to use JSON to store translations, explicitly specify that: i18n.set('file_format', 'json')

!WARNING! yaml.FullLoader is no longer used by default. If you need full yaml functionalities, override it with a custom loader:

class MyLoader(i18n.loaders.YamlLoader):
    loader = yaml.FullLoader

i18n.register_loader(MyLoader, ["yml", "yaml"])

Memoization

Setting the configuration value enable_memoization will disable reloading of files every time when searching for missing translation. When translations are loaded, they're always stored in memory, hence it does not affect how existing translations are accessed.

If you want to reload all translations, you can use i18n.reload_everything().

Namespaces

File namespaces

In the above example, the translation key is foo.hi and not just hi. This is because the translation filename format is by default {namespace}.{locale}.{format}, so the {namespace} part of the file is used as translation.

To remove {namespace} from filename format please change the filename_format configuration.

i18n.set('filename_format', '{locale}.{format}')

Directory namespaces

If your files are in subfolders, the foldernames are also used as namespaces, so for example if your translation root path is /path/to/translations and you have the file /path/to/translations/my/app/name/foo.en.yml, the translation namespace for the file will be my.app.name and the file keys will therefore be accessible from my.app.name.foo.my_key.

Functionalities

Placeholder

You can of course use placeholders in your translations. With the default configuration, the placeholders are used by inserting %{placeholder_name} in the translation string. Here is a sample usage.

i18n.add_translation('hi', 'Hello %{name} !')
i18n.t('hi', name='Bob') # Hello Bob !

Pluralization

Pluralization is based on Rail i18n module. By passing a count variable to your translation, it will be pluralized. The translation value should be a dictionary with at least the keys one and many. You can add a zero or few key when needed, if it is not present many will be used instead. Here is a sample usage.

i18n.add_translation('mail_number', {
    'zero': 'You do not have any mail.',
    'one': 'You have a new mail.',
    'few': 'You only have %{count} mails.',
    'many': 'You have %{count} new mails.'
})
i18n.t('mail_number', count=0) # You do not have any mail.
i18n.t('mail_number', count=1) # You have a new mail.
i18n.t('mail_number', count=3) # You only have 3 new mails.
i18n.t('mail_number', count=12) # You have 12 new mails.

Fallback

You can set a fallback which will be used when the key is not found in the default locale.

i18n.set('locale', 'jp')
i18n.set('fallback', 'en')
i18n.add_translation('foo', 'bar', locale='en')
i18n.t('foo') # bar

Skip locale from root

Sometimes i18n structure file came from another project or not contains root element with locale eg. en name.

{
    "foo": "FooBar"
}

However we would like to use this i18n .json file in our Python sub-project or micro service as base file for translations. i18nice has special configuration that is skipping locale eg. en root data element from the file.

i18n.set('skip_locale_root_data', True)

Error handling

There are three config options for handling different situations. Setting it to None disables handling (default), "error" enables error throwing. You can also set your custom handlers:

on_missing_translation(key, locale, **kwargs)

on_missing_plural(key, locale, translation, count)

on_missing_placeholder(key, locale, translation, placeholder)

Example:

import logging, i18n

def handler(key, locale, text, name):
    logging.warning(f"Missing placeholder {name!r} while translating {key!r} to {locale!r} (in {text!r})")
    return "undefined"

i18n.set("on_missing_placeholder", handler)
i18n.add_translation("am", "Amount is %{amount}")
print(i18n.t("am"))
# output:
# WARNING:root:Missing placeholder 'amount' while translating 'am' to 'en' (in 'Amount is %{amount}')
# Amount is undefined

Custom functions

Add your custom functions and choose translation variants during runtime. All arguments given to t would be passed to the function as keyword arguments. This may be an alternative for pluralization, especially if a language has more than one plural form.

Example (correct plural form of days in Ukrainian):

i18n.set("locale", "uk")
i18n.add_translation("days", "%{count} %{p(день|дні|днів)}")

def determine_plural_form(*, count):
    count = abs(count)
    if count % 10 >= 5 or count % 10 == 0 or (count % 100) in range(11, 20):
        return 2
    elif count % 10 == 1:
        return 0
    return 1

i18n.add_function("p", determine_plural_form, "uk")
i18n.t("days", count=1) # 1 день
i18n.t("days", count=2) # 2 дні
i18n.t("days", count=5) # 5 днів

Metadata

Release files for i18nice 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for i18nice 0.6.2
File Size Uploaded
i18nice-0.6.2.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for i18nice 0.6.2
File Interpreter ABI Platform
i18nice-0.6.2-py3-none-any.whl Python 3 none any Details

Total release size: 33.3 kB

Release files / i18nice-0.6.2.tar.gz

Download URL i18nice-0.6.2.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4cbbf39d918883c29bb0e36035d0b07ece27a8267b3dd95255046af4ac42e9e0
BLAKE2b-256 checksum
How to use checksums
f0f0efe46377e850ce08e81eb9bddf0947561635bac1080f7d5876c8837f3d90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / i18nice-0.6.2-py3-none-any.whl

Download URL i18nice-0.6.2-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f862ca1f98f7214297b0cefbebc8ec11e406dcae85c44d898e257b3614003ce
BLAKE2b-256 checksum
How to use checksums
371be1cc9e6b06768a8c73f3367725f205f2e7809a4c9c4dadf37d4b82adfb3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

0.16.0

2 release files

0.15.5

2 release files

0.15.4

2 release files

0.15.3

2 release files

0.15.2

2 release files

0.13.3

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

This release

0.6.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page