# python-i18n [![Build Status](https://travis-ci.org/danhper/python-i18n.png?branch=master)](https://travis-ci.org/danhper/python-i18n) [![Coverage Status](https://coveralls.io/repos/github/danhper/python-i18n/badge.svg?branch=master)](https://coveralls.io/github/danhper/python-i18n?branch=master) [![Code Climate](https://codeclimate.com/github/danhper/python-i18n/badges/gpa.svg)](https://codeclimate.com/github/danhper/python-i18n)
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 python-i18n
If you want to use YAML to store your translations, use
pip install python-i18n[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')`
### Memoization
Setting the configuration value `enable_memoization` in the settings dir will load the files from disk the first time they
are loaded and then store their content in memory. On the next use the file content will be provided from memory and not
loaded from disk, preventing disk access. While this can be useful in some contexts, keep in mind there is no current way of
issuing a command to the reloader to re-read the files from disk, so if you are updating your translation file without restarting
the interpreter do not use this option.
### 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 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
### 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.
`python-i18n` has special configuration tha is skipping locale eg. `en` root data element from the file.
i18n.set('skip_locale_root_data', True)
Raw data
{
"_id": null,
"home_page": "https://github.com/tuvistavie/python-i18n",
"name": "python-i18n",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Daniel Perez",
"author_email": "tuvistavie@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/fe/32/d9ba976458c9503ec22db4eb677a5d919edaecd73d893effeaa92a67b84b/python-i18n-0.3.9.tar.gz",
"platform": "",
"description": "# python-i18n [![Build Status](https://travis-ci.org/danhper/python-i18n.png?branch=master)](https://travis-ci.org/danhper/python-i18n) [![Coverage Status](https://coveralls.io/repos/github/danhper/python-i18n/badge.svg?branch=master)](https://coveralls.io/github/danhper/python-i18n?branch=master) [![Code Climate](https://codeclimate.com/github/danhper/python-i18n/badges/gpa.svg)](https://codeclimate.com/github/danhper/python-i18n)\n\n\nThis library provides i18n functionality for Python 3 out of the box. The usage is mostly based on Rails i18n library.\n\n## Installation\n\nJust run\n\n pip install python-i18n\n\nIf you want to use YAML to store your translations, use\n\n pip install python-i18n[YAML]\n\n## Usage\n### Basic usage\n\nThe simplest, though not very useful usage would be\n\n import i18n\n i18n.add_translation('foo', 'bar')\n i18n.t('foo') # bar\n\n### Using translation files\n\nYAML and JSON formats are supported to store translations. With the default configuration, if you have the following `foo.en.yml` file\n\n en:\n hi: Hello world !\n\nin `/path/to/translations` folder, you simply need to add the folder to the translations path.\n\n import i18n\n i18n.load_path.append('/path/to/translations')\n i18n.t('foo.hi') # Hello world !\n\nPlease note that YAML format is used as default file format if you have `yaml` module installed.\nIf both `yaml` and `json` modules available and you want to use JSON to store translations, explicitly specify that: `i18n.set('file_format', 'json')`\n\n### Memoization\n\nSetting the configuration value `enable_memoization` in the settings dir will load the files from disk the first time they\nare loaded and then store their content in memory. On the next use the file content will be provided from memory and not\nloaded from disk, preventing disk access. While this can be useful in some contexts, keep in mind there is no current way of\nissuing a command to the reloader to re-read the files from disk, so if you are updating your translation file without restarting\nthe interpreter do not use this option.\n\n### Namespaces\n\n#### File namespaces\nIn 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.\n\nTo remove `{namespace}` from filename format please change the `filename_format` configuration.\n\n i18n.set('filename_format', '{locale}.{format}')\n\n#### Directory namespaces\nIf 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`.\n\n## Functionalities\n### Placeholder\n\nYou 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.\n\n i18n.add_translation('hi', 'Hello %{name} !')\n i18n.t('hi', name='Bob') # Hello Bob !\n\n### Pluralization\n\nPluralization 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.\n\n i18n.add_translation('mail_number', {\n 'zero': 'You do not have any mail.',\n 'one': 'You have a new mail.',\n 'few': 'You only have %{count} mails.',\n 'many': 'You have %{count} new mails.'\n })\n i18n.t('mail_number', count=0) # You do not have any mail.\n i18n.t('mail_number', count=1) # You have a new mail.\n i18n.t('mail_number', count=3) # You only have 3 new mails.\n i18n.t('mail_number', count=12) # You have 12 new mails.\n\n### Fallback\n\nYou can set a fallback which will be used when the key is not found in the default locale.\n\n i18n.set('locale', 'jp')\n i18n.set('fallback', 'en')\n i18n.add_translation('foo', 'bar', locale='en')\n i18n.t('foo') # bar\n\n### Skip locale from root\nSometimes i18n structure file came from another project or not contains root element with locale eg. `en` name.\n\n {\n \"foo\": \"FooBar\"\n }\n\nHowever we would like to use this i18n .json file in our Python sub-project or micro service as base file for translations.\n`python-i18n` has special configuration tha is skipping locale eg. `en` root data element from the file.\n\n i18n.set('skip_locale_root_data', True)\n\n\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Translation library for Python",
"version": "0.3.9",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "21ba7452f09447a0dd530c53beadb689",
"sha256": "bda5b8d889ebd51973e22e53746417bd32783c9bd6780fd27cadbb733915651d"
},
"downloads": -1,
"filename": "python_i18n-0.3.9-py3-none-any.whl",
"has_sig": true,
"md5_digest": "21ba7452f09447a0dd530c53beadb689",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 13750,
"upload_time": "2020-08-26T14:31:26",
"upload_time_iso_8601": "2020-08-26T14:31:26.266720Z",
"url": "https://files.pythonhosted.org/packages/5a/73/9a0b2974dd9a3d50788d235f10c4d73c2efcd22926036309645fc2f0db0c/python_i18n-0.3.9-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "581de49007179e536c4c47982a651cea",
"sha256": "df97f3d2364bf3a7ebfbd6cbefe8e45483468e52a9e30b909c6078f5f471e4e8"
},
"downloads": -1,
"filename": "python-i18n-0.3.9.tar.gz",
"has_sig": true,
"md5_digest": "581de49007179e536c4c47982a651cea",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11778,
"upload_time": "2020-08-26T14:31:27",
"upload_time_iso_8601": "2020-08-26T14:31:27.512205Z",
"url": "https://files.pythonhosted.org/packages/fe/32/d9ba976458c9503ec22db4eb677a5d919edaecd73d893effeaa92a67b84b/python-i18n-0.3.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-08-26 14:31:27",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "tuvistavie",
"github_project": "python-i18n",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "pyyaml",
"specs": [
[
">=",
"3.10"
]
]
},
{
"name": "coveralls",
"specs": []
}
],
"lcname": "python-i18n"
}