Skip to main content

Translation library for Python

Project description

# python-i18n [![Build Status](https://travis-ci.org/tuvistavie/python-i18n.png?branch=master)](https://travis-ci.org/tuvistavie/python-i18n) [![Coverage Status](https://coveralls.io/repos/tuvistavie/python-i18n/badge.png?branch=master)](https://coveralls.io/r/tuvistavie/python-i18n?branch=master)


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

## Installation

Simply download the package and run

python setup.py install

Make sure you are using Python 3. If you want to use YAML translation files, you will need PyYAML installed as well. If you are using `easy_install`, you can use

easy_install python-i18n[yaml]

to install both.

## 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 !

### 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.

#### 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 ntranslation 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 dictionnary 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

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

python-i18n-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python-i18n-0.1.0.linux-x86_64.exe (73.2 kB view details)

Uploaded Source

File details

Details for the file python-i18n-0.1.0.tar.gz.

File metadata

  • Download URL: python-i18n-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for python-i18n-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37558e62f9364407091d6c4f63cb8709e0e53510037bc50499d7666365091df0
MD5 5f9ceaf84598f8df24e5b0c3532d7ee4
BLAKE2b-256 cd13fb103ed19b9e5a11734efe484a032a4e94ea97398cedad938c0a4a8df829

See more details on using hashes here.

File details

Details for the file python-i18n-0.1.0.linux-x86_64.exe.

File metadata

File hashes

Hashes for python-i18n-0.1.0.linux-x86_64.exe
Algorithm Hash digest
SHA256 9c64af66b9d4f18d97c66b27bb8626268db283cfb1e12a224e454be9f005790d
MD5 a656dbabf742ece2a4d0c632b41dcea4
BLAKE2b-256 9dfe8cc479f979b9dd6da40af73c8d3bb7662bc175d828d5072101cacbe57e30

See more details on using hashes here.

Supported by

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