Localization

A module to handle localization for all interactions. This is currently only supported for Pycord.

ezcord.i18n.t(obj, key, count=None, **variables)[source]

Get the localized string for the given key and insert all variables.

Parameters:
class ezcord.i18n.TEmbed(key='embed', **kwargs)[source]

A subclass of discord.Embed for localized embeds.

Parameters:
  • key (str) – The key of the embed in the language file.

  • kwargs – Parameters from discord.Embed or custom variables.

title
url
type
description
class ezcord.i18n.I18N(localizations, *, fallback_locale='en-US', process_strings=True, prefer_user_locale=False, localize_numbers=True, ignore_discord_ids=True, exclude_methods=None, disable_translations=None, debug=True, **variables)[source]

A simple class to handle the localization of strings.

A list of available languages is available here: https://discord.com/developers/docs/reference#locales

Note

Methods in this class are called automatically and do not need to be called manually in most cases.

Parameters:
  • localizations (dict[str, dict]) –

    A dictionary containing the localizations for all strings.

    If an en key is found, the values will be used for both en-GB and en-US.

  • fallback_locale (str) – The locale to use if the user’s locale is not found in the localizations. Defaults to en-US.

  • process_strings (bool) – Whether to replace general variables when loading the language file. Defaults to True.

  • prefer_user_locale (bool) – Whether to prefer the user’s locale over the guild’s locale. Defaults to False.

  • localize_numbers (bool) – This sets the thousands separator to a period or comma, depending on the current locale. Defaults to True.

  • ignore_discord_ids (bool) – Whether to not localize numbers that could be a Discord ID. Default to True. This only has an effect if localize_numbers is set to True.

  • exclude_methods (list[str] | None) – Method names to exclude from the search of keys in the language file.

  • disable_translations (list[Literal['send', 'edit', 'send_message', 'send_modal', 'edit_message', 'edit_original_response', 'webhook_send', 'webhook_edit_message']] | None) –

    A list of translations to disable. Defaults to None.

    The log level in ezcord.logs.set_log() must be set to DEBUG for this to work.

  • debug (bool) – Whether to send debug messages and warnings. Defaults to True.

  • variables – Additional variables to replace in the language file. This is useful for values that are the same in all languages.

localizations: dict[str, dict]
fallback_locale: str
prefer_user_locale: bool = False
localize_numbers: bool
ignore_discord_ids: bool
exclude_methods: list[str] | None
cmd_localizations: dict[str, dict] = {}
initialized: bool = False
static get_locale(obj)[source]

Get the locale from the given object. By default, this is the guild’s locale.

Parameters:

obj – The object to get the locale from.

static get_clean_locale(obj)[source]

Get the clean locale from the given object. This is the locale without the region, e.g. en instead of en-US.

Parameters:

obj (Union[Interaction, ApplicationContext, InteractionResponse, Webhook, Guild, Member]) – The object to get the locale from.

static get_location()[source]

Returns the name of the file, method and class for the current interaction.

This can only get the class if a method was executed from inside the class.

static load_text(key, locale, count=None, called_class=None, add_locations=(), **variables)[source]

A helper methods that calls get_text() to load the specified key and replace_variables() to replace the variables.

A class name can be given if the kwargs contain a view or modal. This name will be used to load strings from the init method or from decorators, as the class can only be fetched automatically if a method was executed from inside the class.

Additional locations can be added to the lookup by passing a tuple of strings. This is used to load embed keys from the location of the embed creation, instead of the location of the embed usage.

static load_embed(embed, locale)[source]

Loads an embed from the language file.

Return type:

Embed

static load_lang_keys(content, locale, count=None, add_locations=(), **variables)[source]

Iterates through the content, loads the keys from the language file and replaces all variables with their values.

Does not modify the original content.

Return type:

dict | str