Skip to content

Commit

Permalink
fix(i18next): change how fetchLanguage is implemented (#107)
Browse files Browse the repository at this point in the history
This is honestly better than a second property on `ClientOptions`.
Another option on `ClientOptions` gets really confusing when you already
have an `i18n` property. Not to mention that other than the type
it wasn't really documented anywhere properly because it was only
being used in `register`.

BREAKING CHANGE: `fetchLanguage` has been moved from `ClientOptions` to `ClientOptions.i18n`
  • Loading branch information
favna committed Aug 9, 2021
1 parent 7c17a92 commit 30c8f7f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 66 deletions.
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -26,16 +26,16 @@
"@sapphire/prettier-config": "^1.1.6",
"@sapphire/ts-config": "^3.0.0",
"@sapphire/utilities": "^2.0.1",
"@types/jest": "^26.0.23",
"@types/jest": "^26.0.24",
"@types/node": "^16.4.13",
"@types/node-fetch": "^2.5.12",
"@types/ws": "^7.4.7",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.5",
"@typescript-eslint/eslint-plugin": "^4.29.1",
"@typescript-eslint/parser": "^4.29.1",
"cz-conventional-changelog": "^3.3.0",
"discord-api-types": "^0.20.2",
"discord.js": "dev",
"eslint": "^7.31.0",
"discord-api-types": "^0.22.0",
"discord.js": "^13.0.1",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"gen-esm-wrapper": "^1.1.2",
Expand All @@ -48,7 +48,7 @@
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1",
"ts-jest": "^27.0.4",
"ts-node": "^10.1.0",
"ts-node": "^10.2.0",
"typedoc": "0.19.2",
"typedoc-plugin-lerna-packages": "^0.3.1",
"typedoc-plugin-nojekyll": "^1.0.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/i18next/src/lib/InternationalizationHandler.ts
Expand Up @@ -62,6 +62,10 @@ export class InternationalizationHandler {
addPath: this.languagesDirectory,
...this.options.backend
};

if (isFunction(this.options.fetchLanguage)) {
this.fetchLanguage = this.options.fetchLanguage;
}
}

/**
Expand Down
19 changes: 12 additions & 7 deletions packages/i18next/src/lib/types.ts
@@ -1,3 +1,4 @@
import type { Awaited } from '@sapphire/utilities';
import type { Guild, Message, StageChannel, StoreChannel, User, VoiceChannel } from 'discord.js';
import type { InitOptions } from 'i18next';
import type { i18nextFsBackend } from 'i18next-fs-backend';
Expand Down Expand Up @@ -53,6 +54,17 @@ export interface InternationalizationOptions {
* @since 1.0.0
*/
defaultNS?: string;
/**
* A function that is to be used to retrieve the language for the current context.
* Context exists of a {@link Guild `guild`}, a {@link DiscordChannel `channel`} and a {@link User `user`}.
*
* If this is not set, then the language will always be the default language.
*
* This will be inserted for {@link InternationalizationHandler.fetchLanguage}.
* @since 2.0.0
* @default () => InternationalizationOptions.defaultName
*/
fetchLanguage?: (context: InternationalizationContext) => Awaited<string | null>;
}

export type TextBasedDiscordChannel = Message['channel'];
Expand All @@ -70,11 +82,4 @@ export interface InternationalizationContext {

export interface InternationalizationClientOptions {
i18n?: InternationalizationOptions;

/**
* Hook that returns the name of a language. Should correspond to {@link InternationalizationHandler.fetchLanguage}.
* @since 1.0.0
* @default () => {@link I18nOptions.defaultName}
*/
fetchLanguage?: (context: InternationalizationContext) => Promise<string | null> | string | null;
}
1 change: 0 additions & 1 deletion packages/i18next/src/register.ts
Expand Up @@ -5,7 +5,6 @@ import { InternationalizationClientOptions, InternationalizationHandler } from '
export class I18nextPlugin extends Plugin {
public static [preGenericsInitialization](this: SapphireClient, options: ClientOptions): void {
container.i18n = new InternationalizationHandler(options.i18n);
if (typeof options.fetchLanguage === 'function') container.i18n.fetchLanguage = options.fetchLanguage;
}

public static async [preLogin](this: SapphireClient): Promise<void> {
Expand Down

0 comments on commit 30c8f7f

Please sign in to comment.