General functions

This section gathers general functions included in the lensepy package.

Using dictionnary

It is possible to use functions to automaticaly translate some part of a user interface for exemple in a specific language.

Translation data files

For that, you need to create a CSV file for each language. For example, a file named lang_FR.csv can contain all the definitions for the French language.

The file should have the following format:

# comment # comment key_1 ; language_word_1 key_2 ; language_word_2

Functions for translations

lensepy.load_dictionary(language_path: str) dict

Load a dictionary from a CSV file based on the specified language.

Parameters:

language (str) – The language path to specify which CSV file to load.

Returns:

dict containing ‘key_1’

Return type:

‘language_word_1’

Notes

This function reads a CSV file that contains key-value pairs separated by semicolons (‘;’) and stores them in a global dictionary variable. The CSV file may contain comments prefixed by ‘#’, which will be ignored.

The file should have the following format:

# comment # comment key_1 ; language_word_1 key_2 ; language_word_2

The function will strip any leading or trailing whitespace from the keys and values.

See also

numpy.genfromtxt

Load data from a text file, with missing values handled as specified.

lensepy.translate(key: str) str

Translate a given key to its corresponding value.

Parameters:

key (str) – The key to translate.

Returns:

The translated value corresponding to the key. If the key does not exist, it returns the value of the key itself.

Return type:

str

Import functions from lensepy

To import the previous functions, you have to include this line to your python script:

from lensepy import load_dictionary, translate, dictionary

dictionary is a global variable. It is a Python dictionary containing the translated expression for a specific key.

How to use

Load a dictionary file

To load an existing dictionary file, you can use the following instruction where file_name_dict is the path to the CSV file containing the definition of the different keys.

load_dictionary(file_name_dict)

Translate function

label = QLabel(translate("label_key"))

If the label_key is in the global dictionary, the translate function returns the associated value. Otherwise, the value label_key is returned.