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
]) – 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.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 bothen-GB
anden-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_numbers
is 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'
,'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 toDEBUG
for this to work.debug (
bool
) – Whether to send debug messages and warnings. Defaults toTrue
.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.
- 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 ofen-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 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.