Skip to main content

Class: InternationalizationHandler

@sapphire/plugin-i18next.InternationalizationHandler

A generalized class for handling i18next JSON files and their discovery.

since 1.0.0

Constructors

constructor

โ€ข new InternationalizationHandler(options?)

since 1.0.0

Parameters

NameTypeDescription
options?InternationalizationOptionsThe options that i18next, i18next-fs-backend, and InternationalizationHandler should use.

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:56

Properties

backendOptions

โ€ข Protected Readonly backendOptions: i18nextFsBackendOptions

The backend options for i18next-fs-backend used by i18next.

since 1.0.0

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:49


fetchLanguage

โ€ข fetchLanguage: (context: InternationalizationContext) => Awaitable<null | string>

Type declaration

โ–ธ (context): Awaitable<null | string>

The method to be overridden by the developer.

note In the event that fetchLanguage is not defined or returns null / undefined, the defaulting from fetchLanguage will be used.

since 2.0.0

see fetchLanguage

example

// Always use the same language (no per-guild configuration):
container.i18n.fetchLanguage = () => 'en-US';

example

// Retrieving the language from an SQL database:
container.i18n.fetchLanguage = async (context) => {
const guild = await driver.getOne('SELECT language FROM public.guild WHERE id = $1', [context.guild.id]);
return guild?.language ?? 'en-US';
};

example

// Retrieving the language from an ORM:
container.i18n.fetchLanguage = async (context) => {
const guild = await driver.getRepository(GuildEntity).findOne({ id: context.guild.id });
return guild?.language ?? 'en-US';
};

example

// Retrieving the language on a per channel basis, e.g. per user or guild channel (ORM example but same principles apply):
container.i18n.fetchLanguage = async (context) => {
const channel = await driver.getRepository(ChannelEntity).findOne({ id: context.channel.id });
return channel?.language ?? 'en-US';
};
Parameters
NameType
contextInternationalizationContext
Returns

Awaitable<null | string>

A string for the desired language or null for no match.

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:109


languages

โ€ข Readonly languages: Map<string, TFunction>

A Map of i18next language functions keyed by their language code.

since 1.0.0

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:30


languagesDirectory

โ€ข Readonly languagesDirectory: string

The director passed to i18next-fs-backend. Also used in InternationalizationHandler.walkLanguageDirectory.

since 1.2.0

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:43


languagesLoaded

โ€ข languagesLoaded: boolean = false

Describes whether InternationalizationHandler.init has been run and languages are loaded in InternationalizationHandler.languages.

since 1.0.0

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:18


namespaces

โ€ข namespaces: Set<string>

A Set of initially loaded namespaces.

since 1.2.0

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:24


options

โ€ข Readonly options: InternationalizationOptions

The options InternationalizationHandler was initialized with in the client.

since 1.0.0

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:36

Methods

format

โ–ธ format<TResult, TKeys, TInterpolationMap>(locale, key, options?): TResult

Localizes a content given one or more keys and i18next options.

since 2.0.0

see https://www.i18next.com/overview/api#t

Type parameters

NameType
TResultextends TFunctionResult = string
TKeysextends TFunctionKeys = string
TInterpolationMapextends NonNullObject = StringMap

Parameters

NameTypeDescription
localestringThe language to be used.
keyTKeys | TKeys[]The key or keys to retrieve the content from.
options?TOptions<TInterpolationMap>The interpolation options.

Returns

TResult

The localized content.

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:173


getT

โ–ธ getT(locale): TFunction

Retrieve a raw TFunction from the passed locale.

since 1.0.0

Parameters

NameTypeDescription
localestringThe language to be used.

Returns

TFunction

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:156


init

โ–ธ init(): Promise<void>

Initializes the handler by loading in the namespaces, passing the data to i18next, and filling in the {@link InternationalizationHandler#languages}.

since 1.0.0

Returns

Promise<void>

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:115


walkLanguageDirectory

โ–ธ walkLanguageDirectory(dir, namespaces?, current?): Promise<{ languages: string[] ; namespaces: string[] }>

description Skips any files that don't end with .json.

since 1.0.0

Parameters

NameTypeDefault valueDescription
dirstringundefinedThe directory that should be walked.
namespacesstring[][]The currently known namespaces.
currentstring''The directory currently being traversed.

Returns

Promise<{ languages: string[] ; namespaces: string[] }>

Defined in

projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:197