Skip to content

Commit 2e39517

Browse files
committed
refactor: update i18next to v23
typings: `getT` now returns `TFunction` with the correct default namespace typings: `LocalePrefixKey`'s type now doesn't force a `commands/` prefix typings: `LocalePrefixKey` now uses i18next's `nsSeparator` type instead of a hardcoded `:` typings: All functions using `TypedT` now requires `LocalePrefixKey` BREAKING CHANGE: refactor: Removed `resolveKey`, use `getSupportedLanguageT(interaction)(...args)` instead BREAKING CHANGE: refactor: Removed `resolveUserKey`, use `getSupportedUserLanguageT(interaction)(...args)` instead BREAKING CHANGE: typings: Removed `T` type utility function BREAKING CHANGE: typings: Removed `FT` type utility function BREAKING CHANGE: typings: Removed `TypedT` type utility BREAKING CHANGE: typings: Removed `TypedFT` type utility BREAKING CHANGE: typings: Removed `Value` type utility BREAKING CHANGE: typings: Removed `Values` type utility BREAKING CHANGE: typings: Removed `Difference` type utility BREAKING CHANGE: typings: `i18next.TFunction` is not longer augmented, `lng` and `ns` is not longer assumed to exist
1 parent e54a14a commit 2e39517

File tree

6 files changed

+12
-96
lines changed

6 files changed

+12
-96
lines changed

packages/http-framework-i18n/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ addFormatters(
3434
await init();
3535
```
3636

37+
> **Note**: If you want to customize the options, please check [i18next's TypeScript guide](https://www.i18next.com/overview/typescript) to improve the experience.
38+
3739
### Definition
3840

3941
```typescript
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
11
export { default as i18next, type InitOptions, type TFunction, type TOptions, type TOptionsBase } from 'i18next';
2-
export * from './lib/functions.js';
32
export * from './lib/registry.js';
4-
export type * from './lib/types.js';
53
export * from './lib/utils.js';
6-
7-
import type { NonNullObject } from '@sapphire/utilities';
8-
import type { TypedFT, TypedT } from './lib/types.js';
9-
10-
declare module 'i18next' {
11-
export interface TFunction {
12-
lng: string;
13-
ns?: string;
14-
15-
<TReturn>(key: TypedT<TReturn>, options?: TOptionsBase | string): TReturn;
16-
<TReturn>(key: TypedT<TReturn>, defaultValue: TReturn, options?: TOptionsBase | string): TReturn;
17-
<TArgs extends NonNullObject, TReturn>(key: TypedFT<TArgs, TReturn>, options?: TOptions<TArgs>): TReturn;
18-
<TArgs extends NonNullObject, TReturn>(key: TypedFT<TArgs, TReturn>, defaultValue: TReturn, options?: TOptions<TArgs>): TReturn;
19-
}
20-
}

packages/http-framework-i18n/src/lib/functions.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/http-framework-i18n/src/lib/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async function loadLocale(directory: string, ns: string) {
8383
}
8484

8585
const fixedCache = new Collection<LocaleString, TFunction>();
86-
export function getT(locale: LocaleString): TFunction<'translation', undefined, 'translation'> {
86+
export function getT(locale: LocaleString): TFunction {
8787
if (!loadedLocales.has(locale)) throw new ReferenceError(`Invalid language (${locale})`);
8888
return fixedCache.ensure(locale, () => getFixedT(locale));
8989
}

packages/http-framework-i18n/src/lib/types.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/http-framework-i18n/src/lib/utils.ts

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { Collection } from '@discordjs/collection';
22
import type { NonNullObject } from '@sapphire/utilities';
33
import { lazy } from '@sapphire/utilities';
44
import type { APIInteraction, APIPingInteraction, LocaleString, LocalizationMap } from 'discord-api-types/v10';
5-
import type { TFunction, TOptions, TOptionsBase } from 'i18next';
5+
import type { TFunction, TypeOptions } from 'i18next';
66
import { getT, loadedLocales } from './registry.js';
7-
import type { LocalePrefixKey, TypedFT, TypedT } from './types.js';
87

98
export type Interaction = Pick<Exclude<APIInteraction, APIPingInteraction>, 'locale' | 'guild_locale' | 'guild_id'>;
9+
export type LocaleSeparator = TypeOptions['nsSeparator'];
10+
export type LocalePrefixKey = `${string}${LocaleSeparator}${string}`;
1011

1112
export function getSupportedUserLanguageName(interaction: Interaction): LocaleString {
1213
if (loadedLocales.has(interaction.locale)) return interaction.locale;
@@ -31,45 +32,6 @@ export function getSupportedLanguageT(interaction: Interaction): TFunction {
3132
return getT(getSupportedLanguageName(interaction));
3233
}
3334

34-
export function resolveUserKey<TReturn>(interaction: Interaction, key: TypedT<TReturn>, options?: TOptionsBase | string): TReturn;
35-
export function resolveUserKey<TReturn>(
36-
interaction: Interaction,
37-
key: TypedT<TReturn>,
38-
defaultValue: TReturn,
39-
options?: TOptionsBase | string
40-
): TReturn;
41-
export function resolveUserKey<TArgs extends NonNullObject, TReturn>(
42-
interaction: Interaction,
43-
key: TypedFT<TArgs, TReturn>,
44-
options?: TOptions<TArgs>
45-
): TReturn;
46-
export function resolveUserKey<TArgs extends NonNullObject, TReturn>(
47-
interaction: Interaction,
48-
key: TypedFT<TArgs, TReturn>,
49-
defaultValue: TReturn,
50-
options?: TOptions<TArgs>
51-
): TReturn;
52-
export function resolveUserKey(interaction: Interaction, ...args: [any, any, any?]) {
53-
return getSupportedUserLanguageT(interaction)(...args);
54-
}
55-
56-
export function resolveKey<TReturn>(interaction: Interaction, key: TypedT<TReturn>, options?: TOptionsBase | string): TReturn;
57-
export function resolveKey<TReturn>(interaction: Interaction, key: TypedT<TReturn>, defaultValue: TReturn, options?: TOptionsBase | string): TReturn;
58-
export function resolveKey<TArgs extends NonNullObject, TReturn>(
59-
interaction: Interaction,
60-
key: TypedFT<TArgs, TReturn>,
61-
options?: TOptions<TArgs>
62-
): TReturn;
63-
export function resolveKey<TArgs extends NonNullObject, TReturn>(
64-
interaction: Interaction,
65-
key: TypedFT<TArgs, TReturn>,
66-
defaultValue: TReturn,
67-
options?: TOptions<TArgs>
68-
): TReturn;
69-
export function resolveKey(interaction: Interaction, ...args: [any, any, any?]) {
70-
return getSupportedLanguageT(interaction)(...args);
71-
}
72-
7335
const getLocales = lazy(() => new Collection([...loadedLocales].map((locale) => [locale, getT(locale)])));
7436
const getDefaultT = lazy(() => {
7537
const defaultT = getLocales().get('en-US');
@@ -83,7 +45,7 @@ const getDefaultT = lazy(() => {
8345
* @returns The retrieved data.
8446
* @remarks This should be called **strictly** after loading the locales.
8547
*/
86-
export function getLocalizedData(key: TypedT): LocalizedData {
48+
export function getLocalizedData(key: LocalePrefixKey): LocalizedData {
8749
const locales = getLocales();
8850
const defaultT = getDefaultT();
8951

@@ -99,7 +61,7 @@ export function getLocalizedData(key: TypedT): LocalizedData {
9961
* @param key The key to get the localizations from.
10062
* @returns The updated builder.
10163
*/
102-
export function applyNameLocalizedBuilder<T extends BuilderWithName>(builder: T, key: TypedT) {
64+
export function applyNameLocalizedBuilder<T extends BuilderWithName>(builder: T, key: LocalePrefixKey) {
10365
const result = getLocalizedData(key);
10466
return builder.setName(result.value).setNameLocalizations(result.localizations);
10567
}
@@ -110,7 +72,7 @@ export function applyNameLocalizedBuilder<T extends BuilderWithName>(builder: T,
11072
* @param key The key to get the localizations from.
11173
* @returns The updated builder.
11274
*/
113-
export function applyDescriptionLocalizedBuilder<T extends BuilderWithDescription>(builder: T, key: TypedT) {
75+
export function applyDescriptionLocalizedBuilder<T extends BuilderWithDescription>(builder: T, key: LocalePrefixKey) {
11476
const result = getLocalizedData(key);
11577
return builder.setDescription(result.value).setDescriptionLocalizations(result.localizations);
11678
}
@@ -126,16 +88,16 @@ export function applyDescriptionLocalizedBuilder<T extends BuilderWithDescriptio
12688
*/
12789
export function applyLocalizedBuilder<T extends BuilderWithNameAndDescription>(
12890
builder: T,
129-
...params: [root: LocalePrefixKey] | [name: TypedT, description: TypedT]
91+
...params: [root: LocalePrefixKey] | [name: LocalePrefixKey, description: LocalePrefixKey]
13092
): T {
131-
const [localeName, localeDescription] = params.length === 1 ? [`${params[0]}Name` as TypedT, `${params[0]}Description` as TypedT] : params;
93+
const [localeName, localeDescription] = params.length === 1 ? [`${params[0]}Name` as const, `${params[0]}Description` as const] : params;
13294

13395
applyNameLocalizedBuilder(builder, localeName);
13496
applyDescriptionLocalizedBuilder(builder, localeDescription);
13597
return builder;
13698
}
13799

138-
export function createSelectMenuChoiceName<V extends NonNullObject>(key: TypedT, value?: V): createSelectMenuChoiceName.Result<V> {
100+
export function createSelectMenuChoiceName<V extends NonNullObject>(key: LocalePrefixKey, value?: V): createSelectMenuChoiceName.Result<V> {
139101
const result = getLocalizedData(key);
140102
return {
141103
...value,

0 commit comments

Comments
 (0)