Skip to content

Localization

There are two ways of localizing your code. First, the Translator and a simple language switch. Whichever option you choose, the localization happens on start up. If you change the language, the program has to be restarted.

formify.localization

Translator

By the default, the system language is used (i.e. "en", "de", "fr", ...)

language = language instance-attribute

Set the current language (usually a language code)

add(id, **langauges)

Add translations for an id. You can use any string as `id.

translator = Translator()
translator.add("button_open", en="Open", de="Öffnen"})

translator.language = "de"
print(translator("button_open")) # returns "Öffnen"

__call__(id)

Grab the translation for id based on the current language. Always returns a string. If no translation for the current language translator.language is provided, the id is returned.

translator("button_open")

load(file_name)

Reads all translations into a JSON file.

save(file_name)

Dumps all translations into a JSON file.

default_translator(*args, **kwargs)

Returns a Translator, populated with translations for default menu items (Open, Close, ...) for german and english. args and kwargs are passed to the Translator(args, **kwargs).

make_language_switch(translator, language_order)

Makes a function, that selects the argument corresponding to the current language. Best read the example below:

translator = Translator()
switch = make_language_switch(translator, ["en", "de"])

translator.language = "en"
print(switch("open", "öffnen")) # prints "open"

translator.language = "de"
print(switch("open", "öffnen")) # prints "öffnen"

# set the text of a button
ControlButton(switch("open", "öffnen"))

Parameters:

Name Type Description Default
translator Translator

used to determine the current language

required
language_order list

language order of the language switch arguments

required

Returns:

Name Type Description
language_switch callable

language switch function