Skip to content

Documentation

Pearoo edited this page Aug 14, 2024 · 2 revisions

Localization (discord.ext.localization.Localization)

Attributes

Name Type Description
localizations Union[str, dict] If str: relative path to the localization JSON or YAML file. If dict: dictionary containing the localizations.
default_locale Optional[str] The default locale to use if a given locale is not found in the localization.
error Optional[bool] Whether to raise an error if the localization is not found. If set to False, it will return the key itself. Defaults to False.

Properties

Name Type Description
default_locale Optional[str] The default locale that was defined in the default_locale attribute.
error Optional[str] Whether the library should raise errors (True) or log them with the logging module and return the key False)

Operations

Operation Description
x == y Returns whether two objects have the same localizations stored.
x != y Returns whether two objects don't have the same localizations stored.
dict(x) Returns the localization data.
x Calling the object will automatically call the localize method.

Methods

@staticmethod format_strings ( data: Any, **kwargs: Any )

Formats the strings in a dictionary. This is used internally, to format strings in the localize method.

Parameters

data ( Any ) : The data to format.

**kwargs ( Any ) : The arguments to pass to the string formatter.

Returns

Any : The formatted data.


localize ( text: str, locale: Union[str, discord.Locale, discord.Guild, discord.Interaction, discord.ext.commands.Context], **kwargs: Any )

Gets the localization of a string like it's done in i18n.

Parameters

text ( str ) : The key to find in the localization file.

locale ( Union[str, Locale, Guild, Interaction, Context] ) : The locale to find the localization with, or an object that has an attribute that returns discord.Locale.

**kwargs ( Any ) : The arguments to pass to the string formatter.

Returns

Union[str, List[str], dict] : The localized data.

Raises

TypeError: The locale parameter received an incorrect type.

InvalidLocale: The locale parameter is not found in the localization file.

LocalizationNotFound: The localization key is not found in the localization file.

Aliases

_, t, translate, localise


one ( text: str, number: Union[int, float], locale: Union[str, discord.Locale, discord.Guild, discord.Interaction, discord.ext.commands.Context], **kwargs: Any )

Gets the singular and plural form of a string like it's done in i18n.

For this, you need to have the key in the JSON be a list of two strings, the first being the singular form, the second being the plural form.

This method can raise any errors that the localize method can raise.

Parameters

text ( str ) : The key to find in the localization file. The number to determine whether to use the singular (list[0]) or plural (list[-1]) form.

number ( Union[int, float] ) :

locale ( Union[str, Locale, Guild, Interaction, Context] ) : The locale to find the localization with, or an object that has an attribute that returns discord.Locale.

**kwargs ( Any ) : The arguments to pass to the string formatter.

Returns

str : If num is 1, returns the first item of the list. Otherwise, it returns the last item of the list.

Raises

WrongLocalizationFormat: The localization key is not a list.

Aliases

_o, o


Errors (discord.ext.localization.errors)

InvalidJSONFormat ( path: str ) ( ValueError )

Raised when there is incorrect formatting in the localization file.

LocalizationFileNotFound ( path: str ) ( FileNotFoundError )

Raised when the localization file is not found.

LocalizationNotFound ( text: str, locale: str ) ( KeyError )

Raised when the localization is not found.

InvalidLocale ( locale: str ) ( KeyError )

Raised when the locale is not found.

WrongLocalizationFormat ( locale: str, _type: type ) ( TypeError )

Raised when the localization is not a list.