Class: InternationalizationHandler
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:27
A generalized class for handling i18next JSON files and their discovery.
Since
1.0.0
Constructors
Constructor
new InternationalizationHandler(
options?:InternationalizationOptions):InternationalizationHandler
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:70
Parameters
| Parameter | Type | Description |
|---|---|---|
options? | InternationalizationOptions | The options that i18next, @skyra/i18next-backend, and InternationalizationHandler should use. |
Returns
InternationalizationHandler
Since
1.0.0
Properties
backendOptions
protectedreadonlybackendOptions:Options
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:63
The backend options for @skyra/i18next-backend used by i18next.
Since
1.0.0
fetchLanguage()
fetchLanguage: (
context:InternationalizationContext) =>Awaitable<null|string>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:132
The method to be overridden by the developer.
Parameters
| Parameter | Type |
|---|---|
context | InternationalizationContext |
Returns
Awaitable<null | string>
A string for the desired language or null for no match.
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
Examples
// Always use the same language (no per-guild configuration):
container.i18n.fetchLanguage = () => 'en-US';
// 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';
};
// 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';
};
// 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';
};
languages
readonlylanguages:Map<string,TFunction<"translation",undefined>>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:44
A Map of i18next language functions keyed by their language code.
Since
1.0.0
languagesDirectory
readonlylanguagesDirectory:string
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:57
The director passed to @skyra/i18next-backend.
Also used in InternationalizationHandler.walkLanguageDirectory.
Since
1.2.0
languagesLoaded
languagesLoaded:
boolean=false
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:32
Describes whether InternationalizationHandler.init has been run and languages are loaded in InternationalizationHandler.languages.
Since
1.0.0
namespaces
namespaces:
Set<string>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:38
A Set of initially loaded namespaces.
Since
1.2.0
options
readonlyoptions:InternationalizationOptions
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:50
The options InternationalizationHandler was initialized with in the client.
Since
1.0.0
Methods
format()
Localizes a content given one or more keys and i18next options.
Since
2.0.0
Param
The language to be used.
Remark
This function also has additional parameters for key, defaultValue, and options, however
TSDoc does not let us document those while matching proper implementation. See the overloads for this method
for the documentation on those parameters.
See
https://www.i18next.com/overview/api#t
Call Signature
format<
Key,TOpt,Ns,Ret,ActualOptions>(locale:string,key:Key|Key[],options?:ActualOptions):TFunctionReturnOptionalDetails<Ret,TOpt>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:199
Localizes a content given one or more keys and i18next options.
Type Parameters
| Type Parameter | Default type |
|---|---|
Key extends string | - |
TOpt extends TOptions | TOptions |
Ns extends Namespace | "translation" |
Ret extends string | $SpecialObject | TOpt["returnObjects"] extends true ? $SpecialObject : string |
ActualOptions extends TOptionsBase & $Dictionary | TOpt & InterpolationMap<Ret> |
Parameters
| Parameter | Type | Description |
|---|---|---|
locale | string | The language to be used. |
key | Key | Key[] | The key or keys to retrieve the content from. |
options? | ActualOptions | The interpolation options. |
Returns
TFunctionReturnOptionalDetails<Ret, TOpt>
The localized content.
Since
2.0.0
See
https://www.i18next.com/overview/api#t
Call Signature
format<
Key,TOpt,Ns,Ret,ActualOptions>(locale:string,key:string|string[],options:TOpt&$Dictionary&object):TFunctionReturnOptionalDetails<Ret,TOpt>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:216
Localizes a content given one or more keys and i18next options.
Type Parameters
| Type Parameter | Default type |
|---|---|
Key extends string | - |
TOpt extends TOptions | TOptions |
Ns extends Namespace | "translation" |
Ret extends string | $SpecialObject | TOpt["returnObjects"] extends true ? $SpecialObject : string |
ActualOptions extends TOptionsBase & $Dictionary | TOpt & InterpolationMap<Ret> |
Parameters
| Parameter | Type | Description |
|---|---|---|
locale | string | The language to be used. |
key | string | string[] | The key or keys to retrieve the content from. |
options | TOpt & $Dictionary & object | The interpolation options as well as a defaultValue for the key and any key/value pairs. |
Returns
TFunctionReturnOptionalDetails<Ret, TOpt>
The localized content.
Since
2.0.0
See
https://www.i18next.com/overview/api#t
Call Signature
format<
Key,TOpt,Ns,Ret,ActualOptions>(locale:string,key:string|string[],defaultValue:undefined|string,options?:TOpt&$Dictionary):TFunctionReturnOptionalDetails<Ret,TOpt>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:234
Localizes a content given one or more keys and i18next options.
Type Parameters
| Type Parameter | Default type |
|---|---|
Key extends string | - |
TOpt extends TOptions | TOptions |
Ns extends Namespace | "translation" |
Ret extends string | $SpecialObject | TOpt["returnObjects"] extends true ? $SpecialObject : string |
ActualOptions extends TOptionsBase & $Dictionary | TOpt & InterpolationMap<Ret> |
Parameters
| Parameter | Type | Description |
|---|---|---|
locale | string | The language to be used. |
key | string | string[] | The key or keys to retrieve the content from. |
defaultValue | undefined | string | The default value to use if the key is not found. |
options? | TOpt & $Dictionary | The interpolation options. |
Returns
TFunctionReturnOptionalDetails<Ret, TOpt>
The localized content.
Since
2.0.0
See
https://www.i18next.com/overview/api#t
getT()
getT(
locale:string):TFunction<"translation",undefined>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:182
Retrieve a raw TFunction from the passed locale.
Parameters
| Parameter | Type | Description |
|---|---|---|
locale | string | The language to be used. |
Returns
TFunction<"translation", undefined>
Since
1.0.0
init()
init():
Promise<void>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:138
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
reloadResources()
reloadResources():
Promise<void>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:315
Returns
Promise<void>
walkLocaleDirectory()
privatewalkLocaleDirectory(directory:string,ns:string):AsyncGenerator<string>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:338
Parameters
| Parameter | Type | Description |
|---|---|---|
directory | string | The directory that should be walked. |
ns | string | The current namespace. |
Returns
AsyncGenerator<string>
Description
Skips any files that don't end with .json.
Since
3.0.0
walkRootDirectory()
walkRootDirectory(
directory:PathLike):Promise<{languages:string[];namespaces:string[]; }>
Defined in: projects/plugins/packages/i18next/src/lib/InternationalizationHandler.ts:295
Parameters
| Parameter | Type | Description |
|---|---|---|
directory | PathLike | The directory that should be walked. |
Returns
Promise<{ languages: string[]; namespaces: string[]; }>
Since
3.0.0