tuxemon.locale module

class tuxemon.locale.LocaleInfo(locale, category, domain, path)[source]

Bases: object

Information about a locale.

Parameters:
  • locale (str) –

  • category (str) –

  • domain (str) –

  • path (str) –

category: str
domain: str
locale: str
path: str
class tuxemon.locale.TranslatorPo[source]

Bases: object

gettext-based translator class.

po files are read and compiled into mo files by gettext. the mo files are saved in ~/.tuxemon/cache/l18n.

build_translations(recompile_translations=False)[source]

Create MO files for existing PO translation files.

Parameters:

recompile_translations (bool) – True if the translations should be recompiled (useful for testing local changes to the translations).

Return type:

None

collect_languages(recompile_translations=False)[source]

Collect languages/locales with available translation files.

Parameters:

recompile_translations (bool) – True if the translations should be recompiled (useful for testing local changes to the translations).

Return type:

None

static compile_gettext(po_path, mo_path)[source]

Compile po file into mo file.

Parameters:
  • po_path (str) – Path of the po file.

  • mo_path (str) – Path of the mo file.

Return type:

None

format(text, parameters=None)[source]

Replaces variables in a translation string with the given parameters.

Parameters:
  • text (str) – String to format.

  • parameters (Mapping[str, Any] | None) – Parameters to format into the string.

Returns:

The formatted string.

Return type:

str

load_translator(locale_name='en_US', domain='base')[source]

Load a selected locale for translation.

Parameters:
  • locale_name (str) – Name of the locale.

  • domain (str) – Name of the domain.

Return type:

None

maybe_translate(text)[source]

Try to translate the text. If None, return empty string.

Parameters:

text (str | None) – String to translate.

Returns:

Translated string.

Return type:

str

static search_locales()[source]

Search local folder and return LocaleInfo objects.

Yields:

The information of each locale.

Return type:

Generator[LocaleInfo, None, None]

tuxemon.locale.process_translate_text(session, text_slug, parameters)[source]

Translate a dialog to a sequence of pages of text.

Parameters:
  • session (Session) – Session containing the information to fill the variables.

  • text_slug (str) – Text to translate.

  • parameters (Iterable[str]) – A sequence of parameters in the format "key=value" used to format the string.

Return type:

Sequence[str]

tuxemon.locale.replace_text(session, text)[source]

Replaces ${{var}} tiled variables with their in-session value.

Parameters:
  • session (Session) – Session containing the information to fill the variables.

  • text (str) – Text whose references to variables should be substituted.

Return type:

str

Examples

>>> replace_text(session, "${{name}} is running away!")
'Red is running away!'