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

Defined in

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

Properties

backendOptions

Protected Readonly backendOptions: Options<object>

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


languages

Readonly languages: Map<string, TFunction<"translation", undefined, "translation">>

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 InternationalizationHandler.walkLanguageDirectory.

Since

1.2.0

Defined in

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


languagesLoaded

languagesLoaded: boolean = false

Describes whether init has been run and languages are loaded in 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 DefaultTFuncReturnWithObject = string
TKeysextends string = string
TInterpolationMapextends object = 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:181


getT

getT(locale): TFunction<"translation", undefined, "translation">

Retrieve a raw TFunction from the passed locale.

Since

1.0.0

Parameters

NameTypeDescription
localestringThe language to be used.

Returns

TFunction<"translation", undefined, "translation">

Defined in

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


init

init(): Promise<void>

Initializes the handler by loading in the namespaces, passing the data to i18next, and filling in the languages.

Since

1.0.0

Returns

Promise<void>

Defined in

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


reloadResources

reloadResources(): Promise<void>

Returns

Promise<void>

Defined in

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


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