@sapphire/plugin-i18next 6.1.1-next.eccc557.0 → 7.0.0-next.1f2cccc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +39 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +428 -0
- package/dist/cjs/lib/Augmentations.d.cjs +4 -0
- package/dist/cjs/lib/Augmentations.d.cjs.map +1 -0
- package/dist/cjs/lib/InternationalizationHandler.cjs +236 -0
- package/dist/cjs/lib/InternationalizationHandler.cjs.map +1 -0
- package/dist/cjs/lib/functions.cjs +125 -0
- package/dist/cjs/lib/functions.cjs.map +1 -0
- package/dist/cjs/lib/types.cjs +4 -0
- package/dist/cjs/lib/types.cjs.map +1 -0
- package/dist/cjs/register.cjs +33 -0
- package/dist/cjs/register.cjs.map +1 -0
- package/dist/cjs/register.d.ts +10 -0
- package/dist/esm/chunk-6QB3UK4Q.mjs +11 -0
- package/dist/esm/chunk-6QB3UK4Q.mjs.map +1 -0
- package/dist/esm/index.d.mts +428 -0
- package/dist/esm/index.mjs +11 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/lib/Augmentations.d.mjs +3 -0
- package/dist/esm/lib/Augmentations.d.mjs.map +1 -0
- package/dist/esm/lib/InternationalizationHandler.mjs +224 -0
- package/dist/esm/lib/InternationalizationHandler.mjs.map +1 -0
- package/dist/esm/lib/functions.mjs +115 -0
- package/dist/esm/lib/functions.mjs.map +1 -0
- package/dist/esm/lib/types.mjs +3 -0
- package/dist/esm/lib/types.mjs.map +1 -0
- package/dist/esm/register.d.mts +10 -0
- package/dist/esm/register.mjs +30 -0
- package/dist/esm/register.mjs.map +1 -0
- package/package.json +37 -22
- package/dist/index.d.ts +0 -16
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -26
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -13
- package/dist/lib/InternationalizationHandler.d.ts +0 -127
- package/dist/lib/InternationalizationHandler.d.ts.map +0 -1
- package/dist/lib/InternationalizationHandler.js +0 -271
- package/dist/lib/InternationalizationHandler.js.map +0 -1
- package/dist/lib/functions.d.ts +0 -137
- package/dist/lib/functions.d.ts.map +0 -1
- package/dist/lib/functions.js +0 -241
- package/dist/lib/functions.js.map +0 -1
- package/dist/lib/types.d.ts +0 -151
- package/dist/lib/types.d.ts.map +0 -1
- package/dist/lib/types.js +0 -3
- package/dist/lib/types.js.map +0 -1
- package/dist/register.d.ts +0 -9
- package/dist/register.d.ts.map +0 -1
- package/dist/register.js +0 -28
- package/dist/register.js.map +0 -1
- package/dist/register.mjs +0 -4
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.InternationalizationHandler = void 0;
|
|
7
|
-
const framework_1 = require("@sapphire/framework");
|
|
8
|
-
const pieces_1 = require("@sapphire/pieces");
|
|
9
|
-
const utilities_1 = require("@sapphire/utilities");
|
|
10
|
-
const i18next_backend_1 = require("@skyra/i18next-backend");
|
|
11
|
-
const i18next_1 = __importDefault(require("i18next"));
|
|
12
|
-
const promises_1 = require("node:fs/promises");
|
|
13
|
-
const node_path_1 = require("node:path");
|
|
14
|
-
const node_url_1 = require("node:url");
|
|
15
|
-
/**
|
|
16
|
-
* A generalized class for handling `i18next` JSON files and their discovery.
|
|
17
|
-
* @since 1.0.0
|
|
18
|
-
*/
|
|
19
|
-
class InternationalizationHandler {
|
|
20
|
-
/**
|
|
21
|
-
* @param options The options that `i18next`, `@skyra/i18next-backend`, and {@link InternationalizationHandler} should use.
|
|
22
|
-
* @since 1.0.0
|
|
23
|
-
* @constructor
|
|
24
|
-
*/
|
|
25
|
-
constructor(options) {
|
|
26
|
-
/**
|
|
27
|
-
* Describes whether {@link InternationalizationHandler.init} has been run and languages are loaded in {@link InternationalizationHandler.languages}.
|
|
28
|
-
* @since 1.0.0
|
|
29
|
-
*/
|
|
30
|
-
Object.defineProperty(this, "languagesLoaded", {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
configurable: true,
|
|
33
|
-
writable: true,
|
|
34
|
-
value: false
|
|
35
|
-
});
|
|
36
|
-
/**
|
|
37
|
-
* A `Set` of initially loaded namespaces.
|
|
38
|
-
* @since 1.2.0
|
|
39
|
-
*/
|
|
40
|
-
Object.defineProperty(this, "namespaces", {
|
|
41
|
-
enumerable: true,
|
|
42
|
-
configurable: true,
|
|
43
|
-
writable: true,
|
|
44
|
-
value: new Set()
|
|
45
|
-
});
|
|
46
|
-
/**
|
|
47
|
-
* A `Map` of `i18next` language functions keyed by their language code.
|
|
48
|
-
* @since 1.0.0
|
|
49
|
-
*/
|
|
50
|
-
Object.defineProperty(this, "languages", {
|
|
51
|
-
enumerable: true,
|
|
52
|
-
configurable: true,
|
|
53
|
-
writable: true,
|
|
54
|
-
value: new Map()
|
|
55
|
-
});
|
|
56
|
-
/**
|
|
57
|
-
* The options InternationalizationHandler was initialized with in the client.
|
|
58
|
-
* @since 1.0.0
|
|
59
|
-
*/
|
|
60
|
-
Object.defineProperty(this, "options", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
configurable: true,
|
|
63
|
-
writable: true,
|
|
64
|
-
value: void 0
|
|
65
|
-
});
|
|
66
|
-
/**
|
|
67
|
-
* The director passed to `@skyra/i18next-backend`.
|
|
68
|
-
* Also used in {@link InternationalizationHandler.walkLanguageDirectory}.
|
|
69
|
-
* @since 1.2.0
|
|
70
|
-
*/
|
|
71
|
-
Object.defineProperty(this, "languagesDirectory", {
|
|
72
|
-
enumerable: true,
|
|
73
|
-
configurable: true,
|
|
74
|
-
writable: true,
|
|
75
|
-
value: void 0
|
|
76
|
-
});
|
|
77
|
-
/**
|
|
78
|
-
* The backend options for `@skyra/i18next-backend` used by `i18next`.
|
|
79
|
-
* @since 1.0.0
|
|
80
|
-
*/
|
|
81
|
-
Object.defineProperty(this, "backendOptions", {
|
|
82
|
-
enumerable: true,
|
|
83
|
-
configurable: true,
|
|
84
|
-
writable: true,
|
|
85
|
-
value: void 0
|
|
86
|
-
});
|
|
87
|
-
/**
|
|
88
|
-
* The method to be overridden by the developer.
|
|
89
|
-
*
|
|
90
|
-
* @note In the event that fetchLanguage is not defined or returns null / undefined, the defaulting from {@link fetchLanguage} will be used.
|
|
91
|
-
* @since 2.0.0
|
|
92
|
-
* @return A string for the desired language or null for no match.
|
|
93
|
-
* @see {@link fetchLanguage}
|
|
94
|
-
* @example
|
|
95
|
-
* ```typescript
|
|
96
|
-
* // Always use the same language (no per-guild configuration):
|
|
97
|
-
* container.i18n.fetchLanguage = () => 'en-US';
|
|
98
|
-
* ```
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* // Retrieving the language from an SQL database:
|
|
102
|
-
* container.i18n.fetchLanguage = async (context) => {
|
|
103
|
-
* const guild = await driver.getOne('SELECT language FROM public.guild WHERE id = $1', [context.guild.id]);
|
|
104
|
-
* return guild?.language ?? 'en-US';
|
|
105
|
-
* };
|
|
106
|
-
* ```
|
|
107
|
-
* @example
|
|
108
|
-
* ```typescript
|
|
109
|
-
* // Retrieving the language from an ORM:
|
|
110
|
-
* container.i18n.fetchLanguage = async (context) => {
|
|
111
|
-
* const guild = await driver.getRepository(GuildEntity).findOne({ id: context.guild.id });
|
|
112
|
-
* return guild?.language ?? 'en-US';
|
|
113
|
-
* };
|
|
114
|
-
* ```
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* // Retrieving the language on a per channel basis, e.g. per user or guild channel (ORM example but same principles apply):
|
|
118
|
-
* container.i18n.fetchLanguage = async (context) => {
|
|
119
|
-
* const channel = await driver.getRepository(ChannelEntity).findOne({ id: context.channel.id });
|
|
120
|
-
* return channel?.language ?? 'en-US';
|
|
121
|
-
* };
|
|
122
|
-
* ```
|
|
123
|
-
*/
|
|
124
|
-
Object.defineProperty(this, "fetchLanguage", {
|
|
125
|
-
enumerable: true,
|
|
126
|
-
configurable: true,
|
|
127
|
-
writable: true,
|
|
128
|
-
value: () => null
|
|
129
|
-
});
|
|
130
|
-
this.options = options ?? { i18next: { ignoreJSONStructure: false } };
|
|
131
|
-
const baseUserDirectory = pieces_1.container.client?.options?.baseUserDirectory instanceof node_url_1.URL
|
|
132
|
-
? (0, node_url_1.fileURLToPath)(pieces_1.container.client?.options?.baseUserDirectory)
|
|
133
|
-
: pieces_1.container.client?.options?.baseUserDirectory;
|
|
134
|
-
this.languagesDirectory = this.options.defaultLanguageDirectory ?? (0, node_path_1.join)(baseUserDirectory ?? (0, pieces_1.getRootData)().root, 'languages');
|
|
135
|
-
const languagePaths = new Set([
|
|
136
|
-
(0, node_path_1.join)(this.languagesDirectory, '{{lng}}', '{{ns}}.json'),
|
|
137
|
-
...(options?.backend?.paths ?? [])
|
|
138
|
-
]);
|
|
139
|
-
this.backendOptions = {
|
|
140
|
-
paths: [...languagePaths],
|
|
141
|
-
...this.options.backend
|
|
142
|
-
};
|
|
143
|
-
if ((0, utilities_1.isFunction)(this.options.fetchLanguage)) {
|
|
144
|
-
this.fetchLanguage = this.options.fetchLanguage;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Initializes the handler by loading in the namespaces, passing the data to i18next, and filling in the {@link InternationalizationHandler#languages}.
|
|
149
|
-
* @since 1.0.0
|
|
150
|
-
*/
|
|
151
|
-
async init() {
|
|
152
|
-
const { namespaces, languages } = await this.walkRootDirectory(this.languagesDirectory);
|
|
153
|
-
const userOptions = (0, utilities_1.isFunction)(this.options.i18next) ? this.options.i18next(namespaces, languages) : this.options.i18next;
|
|
154
|
-
const ignoreJSONStructure = userOptions?.ignoreJSONStructure ?? false;
|
|
155
|
-
const skipOnVariables = userOptions?.interpolation?.skipOnVariables ?? false;
|
|
156
|
-
i18next_1.default.use(i18next_backend_1.Backend);
|
|
157
|
-
await i18next_1.default.init({
|
|
158
|
-
backend: this.backendOptions,
|
|
159
|
-
fallbackLng: this.options.defaultName ?? 'en-US',
|
|
160
|
-
initImmediate: false,
|
|
161
|
-
interpolation: {
|
|
162
|
-
escapeValue: false,
|
|
163
|
-
...userOptions?.interpolation,
|
|
164
|
-
skipOnVariables
|
|
165
|
-
},
|
|
166
|
-
load: 'all',
|
|
167
|
-
defaultNS: 'default',
|
|
168
|
-
ns: namespaces,
|
|
169
|
-
preload: languages,
|
|
170
|
-
...userOptions,
|
|
171
|
-
ignoreJSONStructure
|
|
172
|
-
});
|
|
173
|
-
this.namespaces = new Set(namespaces);
|
|
174
|
-
for (const item of languages) {
|
|
175
|
-
this.languages.set(item, i18next_1.default.getFixedT(item));
|
|
176
|
-
}
|
|
177
|
-
this.languagesLoaded = true;
|
|
178
|
-
const formatters = this.options.formatters ?? [];
|
|
179
|
-
for (const { name, format } of formatters) {
|
|
180
|
-
i18next_1.default.services.formatter.add(name, format);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Retrieve a raw TFunction from the passed locale.
|
|
185
|
-
* @param locale The language to be used.
|
|
186
|
-
* @since 1.0.0
|
|
187
|
-
*/
|
|
188
|
-
getT(locale) {
|
|
189
|
-
if (!this.languagesLoaded)
|
|
190
|
-
throw new Error('Cannot call this method until InternationalizationHandler#init has been called');
|
|
191
|
-
const t = this.languages.get(locale);
|
|
192
|
-
if (t)
|
|
193
|
-
return t;
|
|
194
|
-
throw new ReferenceError('Invalid language provided');
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Localizes a content given one or more keys and i18next options.
|
|
198
|
-
* @since 2.0.0
|
|
199
|
-
* @param locale The language to be used.
|
|
200
|
-
* @param key The key or keys to retrieve the content from.
|
|
201
|
-
* @param options The interpolation options.
|
|
202
|
-
* @see {@link https://www.i18next.com/overview/api#t}
|
|
203
|
-
* @returns The localized content.
|
|
204
|
-
*/
|
|
205
|
-
format(locale, ...[key, defaultValueOrOptions, optionsOrUndefined]) {
|
|
206
|
-
if (!this.languagesLoaded)
|
|
207
|
-
throw new Error('Cannot call this method until InternationalizationHandler#init has been called');
|
|
208
|
-
const language = this.languages.get(locale);
|
|
209
|
-
if (!language)
|
|
210
|
-
throw new ReferenceError('Invalid language provided');
|
|
211
|
-
const defaultValue = typeof defaultValueOrOptions === 'string'
|
|
212
|
-
? defaultValueOrOptions
|
|
213
|
-
: this.options.defaultMissingKey
|
|
214
|
-
? language(this.options.defaultMissingKey, { replace: { key } })
|
|
215
|
-
: '';
|
|
216
|
-
return language(key, defaultValue, optionsOrUndefined);
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* @param directory The directory that should be walked.
|
|
220
|
-
* @since 3.0.0
|
|
221
|
-
*/
|
|
222
|
-
async walkRootDirectory(directory) {
|
|
223
|
-
const languages = new Set();
|
|
224
|
-
const namespaces = new Set();
|
|
225
|
-
const dir = await (0, promises_1.opendir)(directory);
|
|
226
|
-
for await (const entry of dir) {
|
|
227
|
-
// If the entry is not a directory, skip:
|
|
228
|
-
if (!entry.isDirectory())
|
|
229
|
-
continue;
|
|
230
|
-
// Load the directory:
|
|
231
|
-
languages.add(entry.name);
|
|
232
|
-
for await (const namespace of this.walkLocaleDirectory((0, node_path_1.join)(dir.path, entry.name), '')) {
|
|
233
|
-
namespaces.add(namespace);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return { namespaces: [...namespaces], languages: [...languages] };
|
|
237
|
-
}
|
|
238
|
-
async reloadResources() {
|
|
239
|
-
const result = await framework_1.Result.fromAsync(async () => {
|
|
240
|
-
let languages = this.options.hmr?.languages;
|
|
241
|
-
let namespaces = this.options.hmr?.namespaces;
|
|
242
|
-
if (!languages || !namespaces) {
|
|
243
|
-
const languageDirectoryResult = await this.walkRootDirectory(this.languagesDirectory);
|
|
244
|
-
languages ?? (languages = languageDirectoryResult.languages);
|
|
245
|
-
namespaces ?? (namespaces = languageDirectoryResult.namespaces);
|
|
246
|
-
}
|
|
247
|
-
await i18next_1.default.reloadResources(languages, namespaces);
|
|
248
|
-
pieces_1.container.logger.info('[i18next-Plugin] Reloaded language resources.');
|
|
249
|
-
});
|
|
250
|
-
result.inspectErr((error) => pieces_1.container.logger.error('[i18next-Plugin]: Failed to reload language resources.', error));
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* @description Skips any files that don't end with `.json`.
|
|
254
|
-
* @param directory The directory that should be walked.
|
|
255
|
-
* @param ns The current namespace.
|
|
256
|
-
* @since 3.0.0
|
|
257
|
-
*/
|
|
258
|
-
async *walkLocaleDirectory(directory, ns) {
|
|
259
|
-
const dir = await (0, promises_1.opendir)(directory);
|
|
260
|
-
for await (const entry of dir) {
|
|
261
|
-
if (entry.isDirectory()) {
|
|
262
|
-
yield* this.walkLocaleDirectory((0, node_path_1.join)(dir.path, entry.name), `${ns}${entry.name}/`);
|
|
263
|
-
}
|
|
264
|
-
else if (entry.isFile() && entry.name.endsWith('.json')) {
|
|
265
|
-
yield `${ns}${entry.name.slice(0, -5)}`;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
exports.InternationalizationHandler = InternationalizationHandler;
|
|
271
|
-
//# sourceMappingURL=InternationalizationHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"InternationalizationHandler.js","sourceRoot":"","sources":["../../src/lib/InternationalizationHandler.ts"],"names":[],"mappings":";;;;;;AAAA,mDAA6C;AAC7C,6CAA0D;AAC1D,mDAAiE;AACjE,4DAAsE;AACtE,sDASiB;AAEjB,+CAA2C;AAC3C,yCAAiC;AACjC,uCAA8C;AAG9C;;;GAGG;AACH,MAAa,2BAA2B;IAsCvC;;;;OAIG;IACH,YAAmB,OAAqC;QA1CxD;;;WAGG;QACI;;;;mBAAkB,KAAK;WAAC;QAE/B;;;WAGG;QACI;;;;mBAAa,IAAI,GAAG,EAAU;W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C,SAAS,EAAC;gBAChD,UAAU,KAAV,UAAU,GAAK,uBAAuB,CAAC,UAAU,EAAC;aAClD;YAED,MAAM,iBAAO,CAAC,eAAe,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;YACrD,kBAAS,CAAC,MAAM,CAAC,IAAI,CAAC,+CAA+C,CAAC,CAAC;QACxE,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,UAAU,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,kBAAS,CAAC,MAAM,CAAC,KAAK,CAAC,wDAAwD,EAAE,KAAK,CAAC,CAAC,CAAC;IACvH,CAAC;IAED;;;;;OAKG;IACK,KAAK,CAAC,CAAC,mBAAmB,CAAC,SAAiB,EAAE,EAAU;QAC/D,MAAM,GAAG,GAAG,MAAM,IAAA,kBAAO,EAAC,SAAS,CAAC,CAAC;QACrC,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,EAAE;YAC9B,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE;gBACxB,KAAK,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,IAAA,gBAAI,EAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,GAAG,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC;aACnF;iBAAM,IAAI,KAAK,CAAC,MAAM,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;gBAC1D,MAAM,GAAG,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACxC;SACD;IACF,CAAC;CACD;AA9PD,kEA8PC"}
|
package/dist/lib/functions.d.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { type APIApplicationCommandOptionChoice } from 'discord.js';
|
|
2
|
-
import type { AppendKeyPrefix, DefaultNamespace, InterpolationMap, Namespace, ParseKeys, TFunctionReturn, TFunctionReturnOptionalDetails, TOptions } from 'i18next';
|
|
3
|
-
import type { $Dictionary, $SpecialObject, BuilderWithDescription, BuilderWithName, BuilderWithNameAndDescription, LocalizedData, Target } from './types';
|
|
4
|
-
/**
|
|
5
|
-
* Retrieves the language name for a specific target, using {@link InternationalizationHandler.fetchLanguage}.
|
|
6
|
-
* If {@link InternationalizationHandler.fetchLanguage} is not defined or this function returns a nullish value,
|
|
7
|
-
* then there will be a series of fallback attempts in the following descending order:
|
|
8
|
-
* 1. Returns {@link Guild.preferredLocale}.
|
|
9
|
-
* 2. Returns {@link InternationalizationOptions.defaultName} if no guild was provided.
|
|
10
|
-
* 3. Returns `'en-US'` if nothing else was found.
|
|
11
|
-
* @since 2.0.0
|
|
12
|
-
* @param target The target to fetch the language from.
|
|
13
|
-
* @see {@link resolveLanguage}
|
|
14
|
-
* @returns The name of the language key.
|
|
15
|
-
*/
|
|
16
|
-
export declare function fetchLanguage(target: Target): Promise<string>;
|
|
17
|
-
/**
|
|
18
|
-
* Retrieves the language-assigned function from i18next designated to a target's preferred language code.
|
|
19
|
-
* @since 2.0.0
|
|
20
|
-
* @param target The target to fetch the language from.
|
|
21
|
-
* @returns The language function from i18next.
|
|
22
|
-
*/
|
|
23
|
-
export declare function fetchT(target: Target): Promise<import("i18next").TFunction<"translation", undefined>>;
|
|
24
|
-
/**
|
|
25
|
-
* Resolves a key and its parameters.
|
|
26
|
-
* @since 2.0.0
|
|
27
|
-
* @param target The target to fetch the language key from.
|
|
28
|
-
* @param key The i18next key.
|
|
29
|
-
* @param options The options to be passed to TFunction.
|
|
30
|
-
* @returns The data that `key` held, processed by i18next.
|
|
31
|
-
*/
|
|
32
|
-
export declare function resolveKey<const Key extends ParseKeys<Ns, TOpt, undefined>, const TOpt extends TOptions = TOptions, Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, undefined>, TOpt> = TOpt['returnObjects'] extends true ? $SpecialObject : string, Ns extends Namespace = DefaultNamespace, const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>>(target: Target, ...[key, defaultValueOrOptions, optionsOrUndefined]: [key: Key | Key[], options?: ActualOptions] | [key: string | string[], options: TOpt & $Dictionary & {
|
|
33
|
-
defaultValue: string;
|
|
34
|
-
}] | [key: string | string[], defaultValue: string, options?: TOpt & $Dictionary]): Promise<TFunctionReturnOptionalDetails<Ret, TOpt>>;
|
|
35
|
-
/**
|
|
36
|
-
* Gets the value and the localizations from a language key.
|
|
37
|
-
* @param key The key to get the localizations from.
|
|
38
|
-
* @returns The retrieved data.
|
|
39
|
-
* @remarks This should be called **strictly** after loading the locales.
|
|
40
|
-
*/
|
|
41
|
-
export declare function getLocalizedData<const TOpt extends TOptions = TOptions, Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(key: ParseKeys<Ns, TOpt, KPrefix>): LocalizedData;
|
|
42
|
-
/**
|
|
43
|
-
* Applies the localized names on the builder, calling `setName` and `setNameLocalizations`.
|
|
44
|
-
* @param builder The builder to apply the localizations to.
|
|
45
|
-
* @param key The key to get the localizations from.
|
|
46
|
-
* @returns The updated builder.
|
|
47
|
-
*/
|
|
48
|
-
export declare function applyNameLocalizedBuilder<T extends BuilderWithName, const TOpt extends TOptions = TOptions, Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(builder: T, key: ParseKeys<Ns, TOpt, KPrefix>): T;
|
|
49
|
-
/**
|
|
50
|
-
* Applies the localized descriptions on the builder, calling `setDescription` and `setDescriptionLocalizations`.
|
|
51
|
-
* @param builder The builder to apply the localizations to.
|
|
52
|
-
* @param key The key to get the localizations from.
|
|
53
|
-
* @returns The updated builder.
|
|
54
|
-
*/
|
|
55
|
-
export declare function applyDescriptionLocalizedBuilder<T extends BuilderWithDescription, const TOpt extends TOptions = TOptions, Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(builder: T, key: ParseKeys<Ns, TOpt, KPrefix>): T;
|
|
56
|
-
/**
|
|
57
|
-
* Applies the localized names and descriptions on the builder, calling {@link applyNameLocalizedBuilder} and
|
|
58
|
-
* {@link applyDescriptionLocalizedBuilder}.
|
|
59
|
-
*
|
|
60
|
-
* @param builder The builder to apply the localizations to.
|
|
61
|
-
*
|
|
62
|
-
* @param params The root key or the key for the name and description keys.
|
|
63
|
-
* This needs to be either 1 or 2 parameters.
|
|
64
|
-
* See examples below for more information.
|
|
65
|
-
*
|
|
66
|
-
* @returns The updated builder. You can chain subsequent builder methods on this.
|
|
67
|
-
*
|
|
68
|
-
* @remarks If only 2 parameters were passed, then this function will automatically append `Name` and `Description`
|
|
69
|
-
* to the root-key (wherein `root-key` is second parameter in the function, after `builder`)
|
|
70
|
-
* passed through the second parameter.
|
|
71
|
-
*
|
|
72
|
-
* For example given `applyLocalizedBuilder(builder, 'userinfo')` the localized options will use the i18next keys
|
|
73
|
-
* `userinfoName` and `userinfoDescription`.
|
|
74
|
-
*
|
|
75
|
-
* In the following example we provide all parameters and add a User Option
|
|
76
|
-
* `applyLocalizedBuilder` needs either
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* class UserInfoCommand extends Command {
|
|
80
|
-
* public registerApplicationCommands(registry: ChatInputCommand.Registry) {
|
|
81
|
-
* registry.registerChatInputCommand(
|
|
82
|
-
* (builder) =>
|
|
83
|
-
* applyLocalizedBuilder(builder, 'commands/names:userinfo', 'commands/descriptions:userinfo')
|
|
84
|
-
* .addUserOption(
|
|
85
|
-
* (input) => applyLocalizedBuilder(input, 'commands/options:userinfo-name', 'commands/options:userinfo-description').setRequired(true)
|
|
86
|
-
* )
|
|
87
|
-
* );
|
|
88
|
-
* }
|
|
89
|
-
* }
|
|
90
|
-
* ```
|
|
91
|
-
*
|
|
92
|
-
* In the following example we provide single root keys which means `Name` and `Description` get appended as mentioned above.
|
|
93
|
-
* @example
|
|
94
|
-
* ```typescript
|
|
95
|
-
* class UserInfoCommand extends Command {
|
|
96
|
-
* public registerApplicationCommands(registry: ChatInputCommand.Registry) {
|
|
97
|
-
* registry.registerChatInputCommand(
|
|
98
|
-
* (builder) =>
|
|
99
|
-
* applyLocalizedBuilder(builder, 'commands:userinfo')
|
|
100
|
-
* .addUserOption(
|
|
101
|
-
* (input) => applyLocalizedBuilder(input, 'options:userinfo').setRequired(true)
|
|
102
|
-
* )
|
|
103
|
-
* );
|
|
104
|
-
* }
|
|
105
|
-
* }
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
export declare function applyLocalizedBuilder<T extends BuilderWithNameAndDescription, const TOpt extends TOptions = TOptions, Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(builder: T, ...params: [root: string] | [name: ParseKeys<Ns, TOpt, KPrefix>, description: ParseKeys<Ns, TOpt, KPrefix>]): T;
|
|
109
|
-
/**
|
|
110
|
-
* Constructs an object that can be passed into `setChoices` for String or Number option with localized names.
|
|
111
|
-
*
|
|
112
|
-
* @param key The i18next key for the name of the select option name.
|
|
113
|
-
* @param options The additional Select Menu options. This should _at least_ include the `value` key.
|
|
114
|
-
* @returns An object with anything provided through {@link createLocalizedChoice.options} with `name` and `name_localizations` added.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```typescript
|
|
118
|
-
* export class TypeCommand extends Command {
|
|
119
|
-
* public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
|
|
120
|
-
* registry.registerChatInputCommand((builder) =>
|
|
121
|
-
* applyLocalizedBuilder(builder, 'commands/names:type').addStringOption((option) =>
|
|
122
|
-
* applyLocalizedBuilder(option, 'commands/options:type')
|
|
123
|
-
* .setRequired(true)
|
|
124
|
-
* .setChoices(
|
|
125
|
-
* createLocalizedChoice('selects/pokemon:type-grass', { value: 'grass' }),
|
|
126
|
-
* createLocalizedChoice('selects/pokemon:type-water', { value: 'water' }),
|
|
127
|
-
* createLocalizedChoice('selects/pokemon:type-fire', { value: 'fire' }),
|
|
128
|
-
* createLocalizedChoice('selects/pokemon:type-electric', { value: 'electric' })
|
|
129
|
-
* )
|
|
130
|
-
* )
|
|
131
|
-
* );
|
|
132
|
-
* }
|
|
133
|
-
* }
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
export declare function createLocalizedChoice<ValueType = string | number, const TOpt extends TOptions = TOptions, Ns extends Namespace = DefaultNamespace, KPrefix = undefined>(key: ParseKeys<Ns, TOpt, KPrefix>, options: Omit<APIApplicationCommandOptionChoice<ValueType>, 'name' | 'name_localizations'>): APIApplicationCommandOptionChoice<ValueType>;
|
|
137
|
-
//# sourceMappingURL=functions.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../../src/lib/functions.ts"],"names":[],"mappings":"AAEA,OAAO,EAAwD,KAAK,iCAAiC,EAAqB,MAAM,YAAY,CAAC;AAC7I,OAAO,KAAK,EACX,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,eAAe,EACf,8BAA8B,EAC9B,QAAQ,EACR,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EACX,WAAW,EACX,cAAc,EACd,sBAAsB,EACtB,eAAe,EACf,6BAA6B,EAE7B,aAAa,EACb,MAAM,EACN,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA6B7D;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,MAAM,EAAE,MAAM,kEAE1C;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC/B,KAAK,CAAC,GAAG,SAAS,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,EAChD,KAAK,CAAC,IAAI,SAAS,QAAQ,GAAG,QAAQ,EACtC,GAAG,SAAS,eAAe,CAAC,EAAE,EAAE,eAAe,CAAC,GAAG,EAAE,SAAS,CAAC,EAAE,IAAI,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC,SAAS,IAAI,GAAG,cAAc,GAAG,MAAM,E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C,IAAI,SAAS,QAAQ,GAAG,QAAQ,EACtC,EAAE,SAAS,SAAS,GAAG,gBAAgB,EACvC,OAAO,GAAG,SAAS,EAClB,OAAO,EAAE,CAAC,EAAE,GAAG,EAAE,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,KAG9C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,wBAAgB,qBAAqB,CACpC,CAAC,SAAS,6BAA6B,EACvC,KAAK,CAAC,IAAI,SAAS,QAAQ,GAAG,QAAQ,EACtC,EAAE,SAAS,SAAS,GAAG,gBAAgB,EACvC,OAAO,GAAG,SAAS,EAClB,OAAO,EAAE,CAAC,EAAE,GAAG,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,WAAW,EAAE,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,GAAG,CAAC,CAU5H;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,qBAAqB,CACpC,SAAS,GAAG,MAAM,GAAG,MAAM,EAC3B,KAAK,CAAC,IAAI,SAAS,QAAQ,GAAG,QAAQ,EACtC,EAAE,SAAS,SAAS,GAAG,gBAAgB,EACvC,OAAO,GAAG,SAAS,EAEnB,GAAG,EAAE,SAAS,CAAC,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,EACjC,OAAO,EAAE,IAAI,CAAC,iCAAiC,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,oBAAoB,CAAC,GACxF,iCAAiC,CAAC,SAAS,CAAC,CAQ9C"}
|
package/dist/lib/functions.js
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createLocalizedChoice = exports.applyLocalizedBuilder = exports.applyDescriptionLocalizedBuilder = exports.applyNameLocalizedBuilder = exports.getLocalizedData = exports.resolveKey = exports.fetchT = exports.fetchLanguage = void 0;
|
|
4
|
-
const pieces_1 = require("@sapphire/pieces");
|
|
5
|
-
const utilities_1 = require("@sapphire/utilities");
|
|
6
|
-
const discord_js_1 = require("discord.js");
|
|
7
|
-
/**
|
|
8
|
-
* Retrieves the language name for a specific target, using {@link InternationalizationHandler.fetchLanguage}.
|
|
9
|
-
* If {@link InternationalizationHandler.fetchLanguage} is not defined or this function returns a nullish value,
|
|
10
|
-
* then there will be a series of fallback attempts in the following descending order:
|
|
11
|
-
* 1. Returns {@link Guild.preferredLocale}.
|
|
12
|
-
* 2. Returns {@link InternationalizationOptions.defaultName} if no guild was provided.
|
|
13
|
-
* 3. Returns `'en-US'` if nothing else was found.
|
|
14
|
-
* @since 2.0.0
|
|
15
|
-
* @param target The target to fetch the language from.
|
|
16
|
-
* @see {@link resolveLanguage}
|
|
17
|
-
* @returns The name of the language key.
|
|
18
|
-
*/
|
|
19
|
-
function fetchLanguage(target) {
|
|
20
|
-
// Handle Interactions:
|
|
21
|
-
if (target instanceof discord_js_1.BaseInteraction) {
|
|
22
|
-
return resolveLanguage({
|
|
23
|
-
user: target.user,
|
|
24
|
-
channel: target.channel,
|
|
25
|
-
guild: target.guild,
|
|
26
|
-
interactionGuildLocale: target.guildLocale,
|
|
27
|
-
interactionLocale: target.locale
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
// Handle Message:
|
|
31
|
-
if (target instanceof discord_js_1.Message) {
|
|
32
|
-
return resolveLanguage({ user: target.author, channel: target.channel, guild: target.guild });
|
|
33
|
-
}
|
|
34
|
-
// Handle Guild:
|
|
35
|
-
if (target instanceof discord_js_1.Guild) {
|
|
36
|
-
return resolveLanguage({ user: null, channel: null, guild: target });
|
|
37
|
-
}
|
|
38
|
-
// Handle DMChannel:
|
|
39
|
-
if (target.type === discord_js_1.ChannelType.DM) {
|
|
40
|
-
return resolveLanguage({ user: null, channel: target, guild: null });
|
|
41
|
-
}
|
|
42
|
-
// Handle any other channel:
|
|
43
|
-
return resolveLanguage({ user: null, channel: target, guild: target.guild });
|
|
44
|
-
}
|
|
45
|
-
exports.fetchLanguage = fetchLanguage;
|
|
46
|
-
/**
|
|
47
|
-
* Retrieves the language-assigned function from i18next designated to a target's preferred language code.
|
|
48
|
-
* @since 2.0.0
|
|
49
|
-
* @param target The target to fetch the language from.
|
|
50
|
-
* @returns The language function from i18next.
|
|
51
|
-
*/
|
|
52
|
-
async function fetchT(target) {
|
|
53
|
-
return pieces_1.container.i18n.getT(await fetchLanguage(target));
|
|
54
|
-
}
|
|
55
|
-
exports.fetchT = fetchT;
|
|
56
|
-
/**
|
|
57
|
-
* Resolves a key and its parameters.
|
|
58
|
-
* @since 2.0.0
|
|
59
|
-
* @param target The target to fetch the language key from.
|
|
60
|
-
* @param key The i18next key.
|
|
61
|
-
* @param options The options to be passed to TFunction.
|
|
62
|
-
* @returns The data that `key` held, processed by i18next.
|
|
63
|
-
*/
|
|
64
|
-
async function resolveKey(target, ...[key, defaultValueOrOptions, optionsOrUndefined]) {
|
|
65
|
-
const parsedOptions = typeof defaultValueOrOptions === 'string' ? optionsOrUndefined : defaultValueOrOptions;
|
|
66
|
-
const language = typeof parsedOptions?.lng === 'string' ? parsedOptions.lng : await fetchLanguage(target);
|
|
67
|
-
if (typeof defaultValueOrOptions === 'string') {
|
|
68
|
-
return pieces_1.container.i18n.format(language, key, defaultValueOrOptions, optionsOrUndefined);
|
|
69
|
-
}
|
|
70
|
-
return pieces_1.container.i18n.format(language, key, undefined, defaultValueOrOptions);
|
|
71
|
-
}
|
|
72
|
-
exports.resolveKey = resolveKey;
|
|
73
|
-
/**
|
|
74
|
-
* @internal
|
|
75
|
-
*/
|
|
76
|
-
async function resolveLanguage(context) {
|
|
77
|
-
const lang = await pieces_1.container.i18n.fetchLanguage(context);
|
|
78
|
-
return lang ?? context.guild?.preferredLocale ?? pieces_1.container.i18n.options.defaultName ?? 'en-US';
|
|
79
|
-
}
|
|
80
|
-
const supportedLanguages = new Set(Object.values(discord_js_1.Locale));
|
|
81
|
-
function isSupportedDiscordLocale(language) {
|
|
82
|
-
return supportedLanguages.has(language);
|
|
83
|
-
}
|
|
84
|
-
const getLocales = (0, utilities_1.lazy)(() => {
|
|
85
|
-
const locales = new Map(pieces_1.container.i18n.languages);
|
|
86
|
-
for (const [locale] of locales) {
|
|
87
|
-
if (!isSupportedDiscordLocale(locale)) {
|
|
88
|
-
process.emitWarning('Unsupported Discord locale', {
|
|
89
|
-
code: 'UNSUPPORTED_LOCALE',
|
|
90
|
-
detail: `'${locale}' needs to be one of: ${[...locales.keys()]}`
|
|
91
|
-
});
|
|
92
|
-
locales.delete(locale);
|
|
93
|
-
}
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
return locales;
|
|
97
|
-
});
|
|
98
|
-
const getDefaultT = (0, utilities_1.lazy)(() => {
|
|
99
|
-
const defaultLocale = pieces_1.container.i18n.options.defaultName ?? 'en-US';
|
|
100
|
-
if (!isSupportedDiscordLocale(defaultLocale)) {
|
|
101
|
-
throw new TypeError(`Unsupported Discord locale found:\n'${defaultLocale}' is not within the list of ${[...supportedLanguages]}`);
|
|
102
|
-
}
|
|
103
|
-
const defaultT = getLocales().get(defaultLocale);
|
|
104
|
-
if (defaultT) {
|
|
105
|
-
return defaultT;
|
|
106
|
-
}
|
|
107
|
-
throw new TypeError(`Could not find ${defaultLocale}`);
|
|
108
|
-
});
|
|
109
|
-
/**
|
|
110
|
-
* Gets the value and the localizations from a language key.
|
|
111
|
-
* @param key The key to get the localizations from.
|
|
112
|
-
* @returns The retrieved data.
|
|
113
|
-
* @remarks This should be called **strictly** after loading the locales.
|
|
114
|
-
*/
|
|
115
|
-
function getLocalizedData(key) {
|
|
116
|
-
const locales = getLocales();
|
|
117
|
-
const defaultT = getDefaultT();
|
|
118
|
-
return {
|
|
119
|
-
value: defaultT(key),
|
|
120
|
-
localizations: Object.fromEntries(Array.from(locales, ([locale, t]) => [locale, t(key)]))
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
exports.getLocalizedData = getLocalizedData;
|
|
124
|
-
/**
|
|
125
|
-
* Applies the localized names on the builder, calling `setName` and `setNameLocalizations`.
|
|
126
|
-
* @param builder The builder to apply the localizations to.
|
|
127
|
-
* @param key The key to get the localizations from.
|
|
128
|
-
* @returns The updated builder.
|
|
129
|
-
*/
|
|
130
|
-
function applyNameLocalizedBuilder(builder, key) {
|
|
131
|
-
const result = getLocalizedData(key);
|
|
132
|
-
return builder.setName(result.value).setNameLocalizations(result.localizations);
|
|
133
|
-
}
|
|
134
|
-
exports.applyNameLocalizedBuilder = applyNameLocalizedBuilder;
|
|
135
|
-
/**
|
|
136
|
-
* Applies the localized descriptions on the builder, calling `setDescription` and `setDescriptionLocalizations`.
|
|
137
|
-
* @param builder The builder to apply the localizations to.
|
|
138
|
-
* @param key The key to get the localizations from.
|
|
139
|
-
* @returns The updated builder.
|
|
140
|
-
*/
|
|
141
|
-
function applyDescriptionLocalizedBuilder(builder, key) {
|
|
142
|
-
const result = getLocalizedData(key);
|
|
143
|
-
return builder.setDescription(result.value).setDescriptionLocalizations(result.localizations);
|
|
144
|
-
}
|
|
145
|
-
exports.applyDescriptionLocalizedBuilder = applyDescriptionLocalizedBuilder;
|
|
146
|
-
/**
|
|
147
|
-
* Applies the localized names and descriptions on the builder, calling {@link applyNameLocalizedBuilder} and
|
|
148
|
-
* {@link applyDescriptionLocalizedBuilder}.
|
|
149
|
-
*
|
|
150
|
-
* @param builder The builder to apply the localizations to.
|
|
151
|
-
*
|
|
152
|
-
* @param params The root key or the key for the name and description keys.
|
|
153
|
-
* This needs to be either 1 or 2 parameters.
|
|
154
|
-
* See examples below for more information.
|
|
155
|
-
*
|
|
156
|
-
* @returns The updated builder. You can chain subsequent builder methods on this.
|
|
157
|
-
*
|
|
158
|
-
* @remarks If only 2 parameters were passed, then this function will automatically append `Name` and `Description`
|
|
159
|
-
* to the root-key (wherein `root-key` is second parameter in the function, after `builder`)
|
|
160
|
-
* passed through the second parameter.
|
|
161
|
-
*
|
|
162
|
-
* For example given `applyLocalizedBuilder(builder, 'userinfo')` the localized options will use the i18next keys
|
|
163
|
-
* `userinfoName` and `userinfoDescription`.
|
|
164
|
-
*
|
|
165
|
-
* In the following example we provide all parameters and add a User Option
|
|
166
|
-
* `applyLocalizedBuilder` needs either
|
|
167
|
-
* @example
|
|
168
|
-
* ```typescript
|
|
169
|
-
* class UserInfoCommand extends Command {
|
|
170
|
-
* public registerApplicationCommands(registry: ChatInputCommand.Registry) {
|
|
171
|
-
* registry.registerChatInputCommand(
|
|
172
|
-
* (builder) =>
|
|
173
|
-
* applyLocalizedBuilder(builder, 'commands/names:userinfo', 'commands/descriptions:userinfo')
|
|
174
|
-
* .addUserOption(
|
|
175
|
-
* (input) => applyLocalizedBuilder(input, 'commands/options:userinfo-name', 'commands/options:userinfo-description').setRequired(true)
|
|
176
|
-
* )
|
|
177
|
-
* );
|
|
178
|
-
* }
|
|
179
|
-
* }
|
|
180
|
-
* ```
|
|
181
|
-
*
|
|
182
|
-
* In the following example we provide single root keys which means `Name` and `Description` get appended as mentioned above.
|
|
183
|
-
* @example
|
|
184
|
-
* ```typescript
|
|
185
|
-
* class UserInfoCommand extends Command {
|
|
186
|
-
* public registerApplicationCommands(registry: ChatInputCommand.Registry) {
|
|
187
|
-
* registry.registerChatInputCommand(
|
|
188
|
-
* (builder) =>
|
|
189
|
-
* applyLocalizedBuilder(builder, 'commands:userinfo')
|
|
190
|
-
* .addUserOption(
|
|
191
|
-
* (input) => applyLocalizedBuilder(input, 'options:userinfo').setRequired(true)
|
|
192
|
-
* )
|
|
193
|
-
* );
|
|
194
|
-
* }
|
|
195
|
-
* }
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
function applyLocalizedBuilder(builder, ...params) {
|
|
199
|
-
const [localeName, localeDescription] = params.length === 1 ? [`${params[0]}Name`, `${params[0]}Description`] : params;
|
|
200
|
-
applyNameLocalizedBuilder(builder, localeName);
|
|
201
|
-
applyDescriptionLocalizedBuilder(builder, localeDescription);
|
|
202
|
-
return builder;
|
|
203
|
-
}
|
|
204
|
-
exports.applyLocalizedBuilder = applyLocalizedBuilder;
|
|
205
|
-
/**
|
|
206
|
-
* Constructs an object that can be passed into `setChoices` for String or Number option with localized names.
|
|
207
|
-
*
|
|
208
|
-
* @param key The i18next key for the name of the select option name.
|
|
209
|
-
* @param options The additional Select Menu options. This should _at least_ include the `value` key.
|
|
210
|
-
* @returns An object with anything provided through {@link createLocalizedChoice.options} with `name` and `name_localizations` added.
|
|
211
|
-
*
|
|
212
|
-
* @example
|
|
213
|
-
* ```typescript
|
|
214
|
-
* export class TypeCommand extends Command {
|
|
215
|
-
* public override registerApplicationCommands(registry: ChatInputCommand.Registry) {
|
|
216
|
-
* registry.registerChatInputCommand((builder) =>
|
|
217
|
-
* applyLocalizedBuilder(builder, 'commands/names:type').addStringOption((option) =>
|
|
218
|
-
* applyLocalizedBuilder(option, 'commands/options:type')
|
|
219
|
-
* .setRequired(true)
|
|
220
|
-
* .setChoices(
|
|
221
|
-
* createLocalizedChoice('selects/pokemon:type-grass', { value: 'grass' }),
|
|
222
|
-
* createLocalizedChoice('selects/pokemon:type-water', { value: 'water' }),
|
|
223
|
-
* createLocalizedChoice('selects/pokemon:type-fire', { value: 'fire' }),
|
|
224
|
-
* createLocalizedChoice('selects/pokemon:type-electric', { value: 'electric' })
|
|
225
|
-
* )
|
|
226
|
-
* )
|
|
227
|
-
* );
|
|
228
|
-
* }
|
|
229
|
-
* }
|
|
230
|
-
* ```
|
|
231
|
-
*/
|
|
232
|
-
function createLocalizedChoice(key, options) {
|
|
233
|
-
const result = getLocalizedData(key);
|
|
234
|
-
return {
|
|
235
|
-
...options,
|
|
236
|
-
name: result.value,
|
|
237
|
-
name_localizations: result.localizations
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
exports.createLocalizedChoice = createLocalizedChoice;
|
|
241
|
-
//# sourceMappingURL=functions.js.map
|