Package: python-i18n
python-i18nThis library provides i18n functionality for Python 3 out of the box. The usage is mostly based on Rails i18n library.InstallationJust runpip install python-i18nIf you want to use YAML to store your translations, usepip install python-i18n[YAML]UsageBasic usageThe simplest, though not very useful usage would beimport i18n
i18n.add_translation('foo', 'bar')
i18n.t('foo') # barUsing translation filesYAML and JSON formats are supported to store translations. With the default configuration, if you have the followingfoo.en.ymlfileen:
  hi: Hello world !in/path/to/translationsfolder, 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 haveyamlmodule installed.
If bothyamlandjsonmodules available and you want to use JSON to store translations, explicitly specify that:i18n.set('file_format', 'json')MemoizationSetting the configuration valueenable_memoizationin 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.NamespacesFile namespacesIn the above example, the translation key isfoo.hiand not justhi. 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 thefilename_formatconfiguration.i18n.set('filename_format', '{locale}.{format}')Directory namespacesIf your files are in subfolders, the foldernames are also used as namespaces, so for example if your translation root path is/path/to/translationsand you have the file/path/to/translations/my/app/name/foo.en.yml, the translation namespace for the file will bemy.app.nameand the file keys will therefore be accessible frommy.app.name.foo.my_key.FunctionalitiesPlaceholderYou 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 !PluralizationPluralization is based on Rail i18n module. By passing acountvariable to your translation, it will be pluralized. The translation value should be a dictionnary with at least the keysoneandmany. You can add azeroorfewkey when needed, if it is not presentmanywill 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.FallbackYou 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') # barSkip locale from rootSometimes i18n structure file came from another project or not contains root element with locale eg.enname.{
    "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-i18nhas special configuration tha is skipping locale eg.enroot data element from the file.i18n.set('skip_locale_root_data', True)