Skip to main content

Class: InternationalizationHandler

i18next/src.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, @skyra/i18next-backend, and InternationalizationHandler should use.

Defined in

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

Properties

backendOptions

Protected Readonly backendOptions: Options

The backend options for @skyra/i18next-backend used by i18next.

since 1.0.0

Defined in

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


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:115


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:32


languagesDirectory

Readonly languagesDirectory: string

The director passed to @skyra/i18next-backend. Also used in {@link InternationalizationHandler.walkLanguageDirectory}.

since 1.2.0

Defined in

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


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:20


namespaces

namespaces: Set<string>

A Set of initially loaded namespaces.

since 1.2.0

Defined in

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


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:38

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:179


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:162


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:121


reloadResources

reloadResources(): Promise<void>

Returns

Promise<void>

Defined in

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


walkLocaleDirectory

Private walkLocaleDirectory(directory, ns): AsyncGenerator<string, any, unknown>

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

since 3.0.0

Parameters

NameTypeDescription
directorystringThe directory that should be walked.
nsstringThe current namespace.

Returns

AsyncGenerator<string, any, unknown>

Defined in

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


walkRootDirectory

walkRootDirectory(directory): Promise<{ languages: string[] ; namespaces: string[] }>

since 3.0.0

Parameters

NameTypeDescription
directoryPathLikeThe directory that should be walked.

Returns

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

Defined in

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