Package: i18next
i18nexti18nextis a library facilitating internationalization and localization (i18n) of your Python applicationsRationaleThe idea of the project came from the experiences we encountered during the internationalization of projects created
as part oflemon.markets.  We were not entirely satisfied with the i18n tools available on the market.
It was mainly about the simplicity of generating translation files, their format, the ease of generating a translation based on a given context or
integration with tools and processes supported by external companies such aslocaliseorphrase.
We figured out we would write our own module that would improve the life of a Python developer in this area.Please note that this project is in the early development phase.We would really appreciate if you could help us to improve it. Fell free to report any issue or suggest a new feature.InstallationYou can install library usingpip:pipinstalli18nextGlossaryThe table below describes the terms used in this documentation.TermDescriptiontranslation keyunique key identifying translation stringtranslation stringstring to be translatedtranslation contextdata used to interpolate translation stringtranslation filefile containing mapping of translation keys and translation stringstranslationinterpolated translation stringLocale filesi18nextassumes the existence of the locale directory containing translation files. The locale directory path can be
configured (see sectionConfiguration).Translation files are JSON files containing mapping between translation keys and translation strings.
The translation file should be named as<language>.jsonwhere<language>is a language code.An example of translation file content is presented below:{"translation-key-1":"This is a text without interpolation","translation-key-2":"This is a text with interpolation: { interpolated-value }"}An example directory structure of your locale directory should be similar to:locale/
  en.json
  fr.json
  es.json
  pt.json
  ...Configurationi18nextconfiguration is being done by modifying globalconfigobject fromi18nextpackage.
It should be done before first usage oftransfunction:fromi18nextimportconfigconfig.fallback_lang='en'config.locale_path='/path/to/your/locale/directory'config.strict=FalseThe table below describes the configuration parameters:ParameterDescriptionDefault valuefallback_langFallback language to search for translation key if one cannot be found for requested languageenlocale_pathLocale directory path./localesstrictFlag determining the behavior oftransfunction. If set to False -keyitself will be returned in case one has not been found or interpolation has failed , otherwise - exception will be raisedFalseUsagetransfunction is used to get translation based on translation key and requested language.Non-strict modefromi18nextimporttrans,config# './locale/en.json':# {#    "key-1": "Welcome {firstname} {lastname}!",#    "key-2": "Good morning!",#    "key-3": "Bye!"# }# './locale/pl.json':# {#    "key-1": "Witaj {firstname} {lastname}!",#    "key-2": "Dzień dobry!"# }config.fallback_lang='en'config.strict=Falseen_trans1=trans('key-1',params={'firstname':'John','lastname':'Doe'})# 'Welcome John Doe!'en_trans2=trans('key-2')# 'Good morning!'en_trans3=trans('key-3')# 'Bye!'pl_trans1=trans('key-1',params={'firstname':'John','lastname':'Doe'},lang='pl')# 'Witaj John Doe!'pl_trans2=trans('key-2',lang='pl')# 'Dzień dobry!'pl_trans3=trans('key-3',lang='pl')# 'Bye!' - fallback case because of missing 'key-3' in pl translation# config.strict=False so key itself is returned when translation key or file is missingmissing_en_trans=trans('missing-key1')# 'missing-key1'missing_pl_trans=trans('missing-key2',lang='pl')# 'missing-key2'missing_fr_trans=trans('missing-key3',lang='fr')# 'missing-key3' because translation file is missingStrict modetransfunction is raising exceptions in strict mode (config.strict=True) when:translation file is missingtranslation key does not existinterpolation failsfromi18nextimporttrans,config,errors# './locale/en.json':# {#    "key-1": "Welcome {firstname} {lastname}!",#    "key-2": "Good morning!"# }config.strict=Trueen_trans1=trans('key-1',params={'firstname':'John','lastname':'Doe'})# 'Welcome John Doe!'en_trans2=trans('key-2')# 'Good morning!'# interpolation errortry:trans('key-1',params={'firstname':'John'})# missing lastnameexcepterrors.TranslationFormatError:...# missing translation keytry:trans('missing-key')excepterrors.TranslationNotFoundError:...# missing translation filetry:trans('key-2',lang='pl')excepterrors.TranslationFileNotFoundError:...Error handlingError hierarchy available in strict mode is presented below:I18nError- base class for all exceptions thrown withini18nextlibraryTranslationFileNotFoundError- missing translation file errorTranslationNotFoundError- missing translation key errorTranslationFormatError- interpolation errorCLI to extract translation keys from source codei18nextlibrary provides a command line interface to extract translation keys from Python source code.bash$i18next--help

usage:i18next[-h][--locale[LOCALE]][--lang[LANG]][--debug][search_paths...]positionalarguments:search_pathsPathstosearchforpythonfiles

options:-h,--helpshowthishelpmessageandexit--locale[LOCALE]Localedirectorypath--lang[LANG]Languagecode--debug,-dEnabledebugloggingBasic usage is presented below:i18next--locale./locale--langen--search_paths./your_package/./your/module.pyHow it works?i18nextCLI:scans search paths for python filesextracts translation keys from each file by analysingtransfunction callsupdates translation file with new translation keys containing empty translationsTranslation file to be updated is selected based on language provided in cli call (--lang).
Please note that currentlyi18nextis not removing any translation keys from translation file.Which statements are being detected by the CLI?As mentioned above,i18nextCLI is detecting translation keys by analysingtransfunction calls.
Currently, we support following statements:#1fromi18nextimporttranstrans("1")#2fromi18nextimporttransas__("2")#3importi18nexti18next.trans("3")#4importi18nextas__.trans("4")#5classA:defa(self):fromi18nextimporttranstrans("1")defb(self):fromi18nextimporttransas__("2")defc(self):importi18nexti18next.trans("3")defd(self):importi18nextas__.trans("4")