A class to help with the localization of your bot. This handles string translation based on the intl-messageformat package. Strings are defined in ICU format and dynamic values inside. It is recommended to have a data provider set in the client for the locale settings to be saved persistently for each guild.

Hierarchy

  • Localizer

Constructors

Properties

The client that this localizer will be used by.

Memberof

Localizer

defaultLocale: string

The default locale to be used in case a guild does not have its locale set in the client's data provider. You should set a data provider before setting this up so the guild's locale can be saved persistently.

Memberof

Localizer

guildLocalizers: Collection<string, GuildLocalizer>

The GuildLocalizers for each guild.

Memberof

Localizer

localeStrings: Record<string, Record<string, string>>

An object that maps the name of the locale to another object that contains all the available messages mapped by their keys. The messages should follow a ICU standard, for more information on how to structure these messages, please visit the following link.

The following is an example of how to structure this object:

en: {
'message.test.hello': 'Hello',
'message.test.bye': 'Bye',
'message.test.with_value': 'Hello {name}!'
},
es: {
'message.test.hello': 'Hola',
'message.test.bye': 'Adios',
'message.test.with_value': 'Hola {name}!'
},
fr: {
'message.test.hello': 'Bonjour',
'message.test.bye': 'Au revoir',
'message.test.with_value': 'Bonjour {name}!'
}

Memberof

Localizer

The options for this localizer.

Memberof

Localizer

Methods

  • Get a list of all the available locales retrieved based on the keys of localeStrings.

    Returns string[]

    An array containing all the available locales.

  • Get the formatter object to format the message of the given key and locale. If no message for the given locale exists, then the message for the default locale will be used.

    Parameters

    • key: string

      The key of the message to translate.

    • locale: string

      The locale to translate the message to.

    Returns IntlMessageFormat

    The formatter object for translating the message.

    Throws

    Throws if the key does not resolve to any message.

    Throws

    Throws if the given locale is not supported.

  • Private

    Handles the creation and initialization of a newly joined guild's localizer.

    Parameters

    • guild: Guild

      The guild that was joined.

    Returns Promise<string>

    A promise that resolves once the guild localizer is ready.

    Throws

    Rejects if the guild localizer was being initialized with an unsupported locale retrieved from the data provider. This may happen if the locale saved in the data provider was updated manually.

  • Private

    Handles the deletion of the localizer of the guild that has been left.

    Parameters

    • guild: Guild

      The guild that was left.

    Returns Promise<void>

    A promise that resolves once the guild localizer has been removed from both this and the data provider (if any).

  • Initializes all the GuildLocalizers for each guild the client is connected to. You should call this after initializing the client's data provider since this method will read the locale set for each guild and will initialize their localizer with that value. In case the client does not have a data provider, you should still call this method, but the localizers will be initialized with the default locale. This also registers event handlers for client#guildCreate and client#guildDelete events to automatically create or delete localizers if the bot joins or leaves another guild. You should use the GUILDS intent.

    Returns Promise<string[]>

    A promise that resolves once all guild localizers are ready.

    Throws

    Rejects if a guild localizer was being initialized with an unsupported locale retrieved from the data provider. This may happen if the locale saved in the data provider was updated manually.

  • Check whether the given locale is supported.

    Parameters

    • locale: string

      The locale to test.

    Returns boolean

    true if the locale is supported.

  • Alias for translate().

    Parameters

    • key: string

      The key of the message to translate.

    • Optional locale: null | string

      The locale to translate the message to.

    • values: {} = {}

      The dynamic values to be replaced in the message.

      Returns string

      The translated message.

      Throws

      Throws if the key does not resolve to any message.

      Throws

      Throws if the given locale is not supported.

    • Get the message for the given key translated into the given locale. You can also supply an object containing the dynamic values to be used.

      Parameters

      • key: string

        The key of the message to translate.

      • Optional locale: null | string

        The locale to translate the message to.

      • values: {} = {}

        The dynamic values to be replaced in the message.

        Returns string

        The translated message.

        Throws

        Throws if the key does not resolve to any message.

        Throws

        Throws if the given locale is not supported.

      Generated using TypeDoc