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?): InternationalizationHandler

Parameters

NameTypeDescription
options?InternationalizationOptionsThe options that i18next, @skyra/i18next-backend, and InternationalizationHandler should use.

Returns

InternationalizationHandler

Since

1.0.0

Defined in

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

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


fetchLanguage

fetchLanguage: (context: InternationalizationContext) => 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';
};

Type declaration

▸ (context): Awaitable<null | string>

Parameters
NameType
contextInternationalizationContext
Returns

Awaitable<null | string>

Defined in

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


languages

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

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


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


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


namespaces

namespaces: Set<string>

A Set of initially loaded namespaces.

Since

1.2.0

Defined in

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


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

Methods

format

format<Key, TOpt, Ns, Ret, ActualOptions>(locale, ...«destructured»): TFunctionReturnOptionalDetails<Ret, TOpt>

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

Type parameters

NameType
Keyextends string
TOptextends TOptions = TOptions
Nsextends Namespace = "translation"
Retextends string | $SpecialObject = TOpt["returnObjects"] extends true ? $SpecialObject : string
ActualOptionsextends TOptionsBase & $Dictionary = TOpt & InterpolationMap<Ret>

Parameters

NameTypeDescription
localestringThe language to be used.
...«destructured»[key: Key | Key[], options?: ActualOptions] | [key: string | string[], options: TOpt & $Dictionary & Object] | [key: string | string[], defaultValue: string, options?: TOpt & $Dictionary]-

Returns

TFunctionReturnOptionalDetails<Ret, TOpt>

The localized content.

Since

2.0.0

See

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

Defined in

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


getT

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

Retrieve a raw TFunction from the passed locale.

Parameters

NameTypeDescription
localestringThe language to be used.

Returns

TFunction<"translation", undefined>

Since

1.0.0

Defined in

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


init

init(): Promise<void>

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

Returns

Promise<void>

Since

1.0.0

Defined in

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


reloadResources

reloadResources(): Promise<void>

Returns

Promise<void>

Defined in

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


walkLocaleDirectory

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

Parameters

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

Returns

AsyncGenerator<string, any, unknown>

Description

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

Since

3.0.0

Defined in

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


walkRootDirectory

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

Parameters

NameTypeDescription
directoryPathLikeThe directory that should be walked.

Returns

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

Since

3.0.0

Defined in

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