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:
obj (
Union[Interaction,ApplicationContext,InteractionResponse,Webhook,Guild,Member,str]) – The object to get the locale from.key (
str) – The key of the string in the language file.count (
int|None) – The count for pluralization. Defaults toNone.
- class ezcord.i18n.TEmbed(key='embed', **kwargs)[source]¶
A subclass of
discord.Embedfor localized embeds.- Parameters:
key (
str) – The key of the embed in the language file.kwargs – Parameters from
discord.Embedor 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, language_settings=None, **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
enkey is found, the values will be used for bothen-GBanden-US.fallback_locale (
str) – The locale to use if the user’s locale is not found in the localizations. Defaults toen-US.process_strings (
bool) – Whether to replace general variables when loading the language file. Defaults toTrue.prefer_user_locale (
bool) – Whether to prefer the user’s locale over the guild’s locale. Defaults toFalse.localize_numbers (
bool) – This sets the thousands separator to a period or comma, depending on the current locale. Defaults toTrue.ignore_discord_ids (
bool) – Whether to not localize numbers that could be a Discord ID. Default toTrue. This only has an effect iflocalize_numbersis set toTrue.exclude_methods (
list[str] |None) – Method names to exclude from the search of keys in the language file.disable_translations (
list[Literal['send','edit','reply','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 toDEBUGfor this to work.debug (
bool) – Whether to send debug messages and warnings. Defaults toTrue.language_settings – A function to set custom language settings. The function must return a dictionary with user/guild IDs as keys and the locale as values. Defaults to
None.variables – Additional variables to replace in the language file. This is useful for values that are the same in all languages.
- static get_locale(obj)[source]¶
Get the locale from the given object. By default, this is the guild’s locale.
This method can be called even if the I18N class has not been initialized.
- Parameters:
obj (
Union[Interaction,ApplicationContext,InteractionResponse,Webhook,Guild,Member,str]) – The object to get the locale from.- Return type:
- static get_clean_locale(obj)[source]¶
Get the clean locale from the given object. This is the locale without the region, e.g.
eninstead ofen-US.- Parameters:
obj (
Union[Interaction,ApplicationContext,InteractionResponse,Webhook,Guild,Member,str]) – The object to get the locale from.- Return type:
- 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 andreplace_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.
- ezcord.i18n.LOCALE¶
alias of
Interaction|ApplicationContext|InteractionResponse|Webhook|Guild|Member|str