i18nUtils module

i18nUtils

Module for handling internationalization in Python programs

Copyright (c) 2020 Tom Arn, www.tanapro.ch

For suggestions and questions: <sw@tanapro.ch>

This file is distributed under the terms of the MIT license

i18nUtils.version = '0.3.2'
i18nUtils.version_date = '2020-07-03 - 2023-11-08'

I18nUtils class

class tatogalib.util.i18nUtils.I18nUtils(translation_dir, fallback_lang, lang=None)

Initializes the class and loads the translation files

Parameters:
  • translation_dir (str) – The path to the directory containing the translation files in the format xx.yml where xx is the language, e.g. en.yml

  • fallback_lang (str) – The language to be used if the chosen language is not available

  • lang (str) – The language to use. Defaults to self.get_default_app_language()

get_app_languages()

Returns a list of languages supported by the app

Returns:

The languages for which there are translation files

Return type:

list[str]

get_default_app_language()

Returns the default language of the app.

Returns:

get_default_system_language() if it is in get_app_languages(). Otherwise, it will return self.fallbackLang

Return type:

str

static get_default_system_language()

Returns the default language of the system or ‘en’ when default language cannot be determined

Returns:

The default language of the system or ‘en’

Return type:

str

get_error_translation(text)

Returns the translation of the error text or the original error text To translate error texts, remove all ‘.’ in the message, prefix it with ‘python.error.’ and use this as the key, e.g. python.error.factorial() not defined for negative values: factorial() ist nicht definiert für negative Werte

Parameters:

text (str) – The error text

Returns:

The translated error text if found. Otherwise the original error text

Return type:

str

load_i18n(dir_name='')

Loads the translation files from the passed directory or (when not passed) from the default translation directory which is in __init__ The translation files must be named xx.yml where xx is the language code, e.g. en.yml

Parameters:

dir_name (str) – The path to the directory with the translation files

static t(key, **kwargs)

Gets the translation for the passed text key If the key cannot be found in the set language, the key itself will be returned

Parameters:
  • key (str) – The key for text

  • kwargs (int) – locale chooses a specifc locale, count is used for pluralization of the text: 0 will choose the ‘zero’ element, 1 the ‘one’ element, 2 or greater will choose the ‘many’ element of the translated text. Pass -1 if you want to get the complete dictionary back

Returns:

Returns the translated text or the key itself when no translation was found

Return type:

str