sepal_ui.translator.translator.Translator#

class sepal_ui.translator.translator.Translator(json_folder, target='', default='en')[source]#

Python Box of Box representing all the nested translation key, value pairs.

It reads 2 Json files, the first one being the source language (usually English) and the second one the target language. It will replace in the source dictionary every key that exist in both json dictionaries. Following this procedure, every message that is not translated can still be accessed in the source language. To access the dictionary keys, instead of using [], you can simply use key name as in an object ex: translator.first_key.secondary_key. There are no depth limits, just respect the snake_case convention when naming your keys in the .json files. 5 internal keys are created upon initialization (there name cannot be used as keys in the translation message):

  • (str) _default : the default locale of the translator

  • (str) _targeted : the initially requested language. Use to display debug information to the user agent

  • (str) _target : the target locale of the translator

  • (bool) _match : if the target language match the one requested one by user, used to trigger information in appBar

  • (str) _folder : the path to the l10n folder

Parameters:
  • json_folder (str | Path) – The folder where the dictionaries are stored

  • target (str) – The language code (IETF BCP 47) of the target lang (it should be the same as the target dictionary). Default to either the language specified in the parameter file or the default one.

  • default (str) – The language code (IETF BCP 47) of the source lang. default to “en” (it should be the same as the source dictionary)

Methods

__init__

Python Box of Box representing all the nested translation key, value pairs.

available_locales

Return the available locales in the l10n folder.

delete_empty

Remove empty strings ("") recursively from the dictionaries.

find_target

find the target language in the available language folder.

key_use

Parse all the files in the folder and check if keys are all used at least once.

merge_dict

Gather all the .json file in the provided l10n folder as 1 single json dict.

missing_keys

Nothing.

sanitize

Identify numbered dictionaries embedded in the dict and transform them into lists.

search_key

Search a specific key in the d dictionary and raise an error if found.

Translator.__init__(json_folder, target='', default='en')[source]#

Python Box of Box representing all the nested translation key, value pairs.

It reads 2 Json files, the first one being the source language (usually English) and the second one the target language. It will replace in the source dictionary every key that exist in both json dictionaries. Following this procedure, every message that is not translated can still be accessed in the source language. To access the dictionary keys, instead of using [], you can simply use key name as in an object ex: translator.first_key.secondary_key. There are no depth limits, just respect the snake_case convention when naming your keys in the .json files. 5 internal keys are created upon initialization (there name cannot be used as keys in the translation message):

  • (str) _default : the default locale of the translator

  • (str) _targeted : the initially requested language. Use to display debug information to the user agent

  • (str) _target : the target locale of the translator

  • (bool) _match : if the target language match the one requested one by user, used to trigger information in appBar

  • (str) _folder : the path to the l10n folder

Parameters:
  • json_folder (str | Path) – The folder where the dictionaries are stored

  • target (str) – The language code (IETF BCP 47) of the target lang (it should be the same as the target dictionary). Default to either the language specified in the parameter file or the default one.

  • default (str) – The language code (IETF BCP 47) of the source lang. default to “en” (it should be the same as the source dictionary)

Return type:

None

Translator.available_locales()[source]#

Return the available locales in the l10n folder.

Returns:

the list of str codes

Return type:

List[str]

classmethod Translator.delete_empty(d)[source]#

Remove empty strings (“”) recursively from the dictionaries.

This is to prevent untranslated strings from Crowdin to be uploaded. The dictionary must only embed dictionaries and no lists.

Parameters:

d (dict) – the dictionary to sanitize

Returns:

the sanitized dictionary

Return type:

dict

static Translator.find_target(folder, target='')[source]#

find the target language in the available language folder.

given a folder and a target lang, this function returns the closest language available in the folder If nothing is found falling back to any working subvariety and return None if it doesn’t exist

Parameters:
  • folder (Path) – the folder where the languages dictionaries are stored

  • target (str) – the target lang in IETF BCP 47. If not specified, the value in the sepal-ui config file will be used

Returns:

the targeted language code, the closest lang in IETF BCP 47

Return type:

Tuple[str, str]

Translator.key_use(folder, name)[source]#

Parse all the files in the folder and check if keys are all used at least once.

Return the unused key names.

Warning

Don’t forget that there are many ways of calling Translator variables (getattr, save.cm.xxx in another variable etc…) SO don’t forget to check manually the variables suggested by this method before deleting them

Parameters:
  • folder (Path) – The application folder using this translator data

  • name (str) – the name use by the translator in this app (usually “cm”)

Returns:

the list of unused keys

Return type:

List[str]

New in version 2.10.0.

classmethod Translator.merge_dict(folder)[source]#

Gather all the .json file in the provided l10n folder as 1 single json dict.

The json dict will be sanitysed and the key will be used as if they were coming from 1 single file. be careful with duplication. empty string keys will be removed.

Parameters:

folder (Path) – the folder where all the .json files are stored

Returns:

the json dict with all the keys

Return type:

dict

Translator.missing_keys()[source]#

Nothing.

Deprecated since version 2.9.0: Not needed with automatic translators

classmethod Translator.sanitize(d)[source]#

Identify numbered dictionaries embedded in the dict and transform them into lists.

This function is an helper to prevent deprecation after the introduction of pontoon for translation. The user is now force to use keys even for numbered lists. SimpleNamespace doesn’t support integer indexing so this function will transform back this “numbered” dictionary (with integer keys) into lists.

Parameters:

d (dict | list) – the dictionary to sanitize

Returns:

the sanitized dictionary

Return type:

dict

classmethod Translator.search_key(d, key)[source]#

Search a specific key in the d dictionary and raise an error if found.

Parameters:
  • d (dict) – the dictionary to study

  • key (str) – the key to look for

Return type:

None