I18N Translations

discord_tools provides localization tools. This section outlines everything about it.

Translator

class discord_tools.app_commands.i18n.Translator[source]

Represents a I18N translator.

This is a subclass of discord.app_commands.Translator, this means it can be set by using discord.app_commands.CommandTree.set_translator() for commands translations.

These translations are all merged, so unique translation keys are recommended.

Examples can be found on the repository.

Changed in version 1.0: This is now a subclass of Translator and does not require the googletrans module.

clear_translations()[source]

Clears all the translations. :rtype: None

Added in version 1.0.

update_translation(locale, data)[source]

Updates a locale’s translation strings.

Added in version 1.0.

Parameters:
  • locale (discord.Locale) – The locale to update the translations.

  • data (Dict[str, str]) – The data to update the translations with.

Raises:

KeyError – The locale has no translations strings.

Returns:

The updated translation strings.

Return type:

Dict[discord.Locale, Dict[str, str]]

delete_translation(locale)[source]

Deletes a locale’s translation strings. :rtype: None

Added in version 1.0.

Note

Note that this removes it from the cache and does not clear the strings. If you want to clear it you can use clear_translation().

Raises:

KeyError – The locale has no translation strings.

clear_translation(locale)[source]

Clears a locale’s translation strings. :rtype: None

Added in version 1.0.

Note

This does not remove it from the cache but instead removes the translation strings.

This is a shortcut of calling update_translation() like translator.update_translations(locale, {})

Raises:

KeyError – The locale has no translation strings.

add_translation(locale, data)[source]

Adds a locale’s translation strings.

For updating translations use update_translation(), for deleting translation use delete_translation(), and for clearing them use clear_translations(). :rtype: None

Added in version 1.0.

Raises:

KeyError – The locale already has translation strings.

load_translations(path, *, strategy=..., locale=...)[source]

Loads the translations from a file.

As this function could take a lot of time and block the event loop, it is recommended to call this function once and before starting any Async I/O operations.

Added in version 1.0.

Parameters:
  • path (Union[int, str, bytes, os.PathLike]) – The path to the file to read.

  • strategy (str) – The strategy to use to load the translations, defaults to the file extension.

  • locale (discord.Locale) – The locale this file represents, required for po or mo file-based translations.

Returns:

The loaded translation data.

Return type:

Dict[discord.Locale, Dict[str, str]]