ae.kivy.i18n

ae.kivy.i18n module

this module is adding translatable f-strings to the python and kv code of your app, via the helper function get_txt() and the _GetTextBinder class.

Module Attributes

get_txt(text[, count, language, loc_vars])

instantiate global i18n translation callable and language switcher helper

class _GetTextBinder[source]

Bases: Observable

redirect ae.i18n.get_f_string() to an instance of this class.

kivy currently only support a single one automatic binding in kv files for all function names ending with _ (see watched_keys extension in kivy/lang/parser.py line 201; e.g. f_ would get recognized by the lang_tr re pattern, but kivy will only add the _ symbol to watched_keys and therefore f_ not gets bound.) to allow both - f-strings and simple get_text messages - this module binds ae.i18n.get_f_string() to the get_txt symbol (instead of ae.i18n.get_text()).

get_txt can be used as translation callable, but also to switch the current default language. additionally get_txt is implemented as an observer that automatically updates any translations messages of all active/visible kv rules on switch of the language at app run-time.

inspired by (see also discussion at https://github.com/kivy/kivy/issues/1664):

observers: List[Tuple[Callable, tuple, dict]] = []

list of bound observer tuples (func, args, kwargs)

_bound_uid = -1
fbind(name, func, *args, **kwargs)[source]

override fbind (fast bind) from Observable to collect and separate _ bindings.

Parameters:
  • name (str) – attribute name to be bound.

  • func (Callable) – observer notification function (to be called if attribute changes).

  • args – args to be passed to the observer.

  • kwargs – kwargs to be passed to the observer.

Return type:

int

Returns:

unique id of this binding.

funbind(name, func, *args, **kwargs)[source]

override fast unbind.

Parameters:
  • name (str) – bound attribute name.

  • func (Callable) – observer notification function (called if attribute changed).

  • args – args to be passed to the observer.

  • kwargs – kwargs to be passed to the observer.

switch_lang(lang_code)[source]

change language and update kv rules properties.

Parameters:

lang_code (str) – language code to switch this app to.

__call__(text, count=None, language='', loc_vars=None, **kwargs)[source]

translate text into the current-default or the passed language.

Parameters:
  • text (str) – text to translate.

  • count (Optional[int]) – optional count for pluralization.

  • language (str) – language code to translate the passed text to (def=current default language).

  • loc_vars (Optional[Dict[str, Any]]) – local variables used in the conversion of the f-string expression to a string. the count item of this dict will be overwritten by the value of the count parameter (if this argument got specified).

  • kwargs – extra kwargs (e.g. glo_vars or key_suffix - see get_f_string()).

Return type:

str

Returns:

translated text.

get_txt(text, count=None, language='', loc_vars=None, **kwargs) = <ae.kivy.i18n._GetTextBinder object>

instantiate global i18n translation callable and language switcher helper