@guren/server 2.10.0 → 2.10.1
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/DocsViewerServiceProvider-qFCaeiUv.js +77 -0
- package/dist/Hash-CzrwKeDp.js +133 -0
- package/dist/HttpException-BkS3euNG.js +161 -0
- package/dist/Job-BVT813DO.js +441 -0
- package/dist/McpServiceProvider-BoAN-UcD.js +367 -0
- package/dist/MessageSigner-Bd25ZJAV.js +93 -0
- package/dist/Random-DbIJ22KQ.js +191 -0
- package/dist/address-D0Iq-XIH.js +20 -0
- package/dist/auth/index.js +2 -122
- package/dist/auth-C7sVmYgc.js +1954 -0
- package/dist/authorization/index.js +2 -38
- package/dist/authorization-Dbcuc5ZK.js +610 -0
- package/dist/broadcasting/index.js +2 -26
- package/dist/broadcasting-Dw-FhyjN.js +1055 -0
- package/dist/cache/index.js +2 -17
- package/dist/cache/types.d.ts +4 -4
- package/dist/cache-BYnZf2FR.js +812 -0
- package/dist/client-Dd7vLcqp.js +27 -0
- package/dist/console/Output.d.ts +18 -18
- package/dist/encryption/index.js +5 -96
- package/dist/encryption-fOWQaKqX.js +250 -0
- package/dist/endpoint-D-20PiHY.js +46 -0
- package/dist/endpoint-D6GIqOpe.js +31 -0
- package/dist/errors-quJOkkCn.js +81 -0
- package/dist/events/index.js +2 -28
- package/dist/events-aIH_bWRx.js +410 -0
- package/dist/health/index.js +2 -24
- package/dist/health-VWdWXRwh.js +345 -0
- package/dist/hot-disposables-WsTX2ySN.js +281 -0
- package/dist/hot-reload/testing.d.ts +1 -1
- package/dist/i18n/index.js +2 -30
- package/dist/i18n-mcWi7LCd.js +617 -0
- package/dist/index.js +7495 -7276
- package/dist/lambda/index.js +114 -71
- package/dist/logging/index.js +2 -24
- package/dist/logging-CPaRMuPP.js +518 -0
- package/dist/loopback-guard-D0Kc7-xc.js +224 -0
- package/dist/mail/index.js +2 -27
- package/dist/mail-DfEfSQEr.js +699 -0
- package/dist/mcp/index.js +2 -10
- package/dist/notifications/index.js +2 -33
- package/dist/notifications-BGqmUNZ8.js +759 -0
- package/dist/queue/index.js +3 -42
- package/dist/queue-CXrRK85G.js +948 -0
- package/dist/redis/index.js +672 -642
- package/dist/rolldown-runtime-8H4AJuhK.js +14 -0
- package/dist/runtime/index.js +472 -616
- package/dist/scheduling/index.js +2 -29
- package/dist/scheduling-CPiEdtQd.js +839 -0
- package/dist/storage/index.js +2 -15
- package/dist/storage-tWHMv2H_.js +841 -0
- package/dist/support/expiry.js +69 -12
- package/dist/trim-slashes-ZJ_VsY5s.js +25 -0
- package/dist/vite/index.js +118 -181
- package/dist/vite/plugin.d.ts +1 -1
- package/dist/vite-dev-server-CpCUqmiC.js +131 -0
- package/package.json +6 -5
- package/dist/DocsViewerServiceProvider-RUYDFBMW.js +0 -75
- package/dist/McpServiceProvider-SL7DUZ4G.js +0 -8
- package/dist/chunk-2SC566CD.js +0 -376
- package/dist/chunk-2V3YID4H.js +0 -20
- package/dist/chunk-3KV5X2FL.js +0 -31
- package/dist/chunk-63SKW5PS.js +0 -457
- package/dist/chunk-6J6B5KOB.js +0 -425
- package/dist/chunk-A5JJ7XTH.js +0 -24
- package/dist/chunk-AZOYI43J.js +0 -18
- package/dist/chunk-B5E5VBDI.js +0 -115
- package/dist/chunk-DAQKYKLH.js +0 -182
- package/dist/chunk-DRNPVBAA.js +0 -1002
- package/dist/chunk-EI5GYMAT.js +0 -853
- package/dist/chunk-H3B23O6T.js +0 -271
- package/dist/chunk-HPET6XY5.js +0 -853
- package/dist/chunk-HR2KZ3WL.js +0 -616
- package/dist/chunk-IOTWFHZU.js +0 -558
- package/dist/chunk-K6CUBJX6.js +0 -642
- package/dist/chunk-KTNJG7R7.js +0 -911
- package/dist/chunk-LAJQCSSF.js +0 -355
- package/dist/chunk-LHR6OHI7.js +0 -189
- package/dist/chunk-N7PLD7MB.js +0 -729
- package/dist/chunk-Q2P6YLRR.js +0 -24
- package/dist/chunk-QQKTH5KX.js +0 -114
- package/dist/chunk-SFQP6O52.js +0 -126
- package/dist/chunk-SQR7277M.js +0 -911
- package/dist/chunk-T2FFUU6J.js +0 -484
- package/dist/chunk-TVV5YNEC.js +0 -1842
- package/dist/chunk-UY3AZSYL.js +0 -14
- package/dist/chunk-V4DNC2ER.js +0 -114
- package/dist/chunk-VSA3QY5Q.js +0 -274
- package/dist/chunk-YSOJKAAU.js +0 -427
- package/dist/errors-PUNHAFL2.js +0 -34
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
2
|
+
import { basename, extname, join } from "node:path";
|
|
3
|
+
//#region src/i18n/pluralization.ts
|
|
4
|
+
/**
|
|
5
|
+
* Default pluralization rules for common languages.
|
|
6
|
+
*
|
|
7
|
+
* Returns the index of the plural form to use.
|
|
8
|
+
* Most languages use: 0 = one, 1 = other
|
|
9
|
+
* Some languages have more complex rules.
|
|
10
|
+
*/
|
|
11
|
+
const pluralizationRules = {
|
|
12
|
+
/**
|
|
13
|
+
* English, German, Spanish, Italian, Portuguese, etc.
|
|
14
|
+
* 1 = singular, everything else = plural
|
|
15
|
+
*/
|
|
16
|
+
en: (count) => count === 1 ? 0 : 1,
|
|
17
|
+
de: (count) => count === 1 ? 0 : 1,
|
|
18
|
+
es: (count) => count === 1 ? 0 : 1,
|
|
19
|
+
it: (count) => count === 1 ? 0 : 1,
|
|
20
|
+
pt: (count) => count === 1 ? 0 : 1,
|
|
21
|
+
nl: (count) => count === 1 ? 0 : 1,
|
|
22
|
+
/**
|
|
23
|
+
* French, Brazilian Portuguese
|
|
24
|
+
* 0 and 1 = singular, everything else = plural
|
|
25
|
+
*/
|
|
26
|
+
fr: (count) => count <= 1 ? 0 : 1,
|
|
27
|
+
"pt-BR": (count) => count <= 1 ? 0 : 1,
|
|
28
|
+
/**
|
|
29
|
+
* Japanese, Chinese, Korean, Vietnamese, Thai
|
|
30
|
+
* No plural forms
|
|
31
|
+
*/
|
|
32
|
+
ja: () => 0,
|
|
33
|
+
zh: () => 0,
|
|
34
|
+
ko: () => 0,
|
|
35
|
+
vi: () => 0,
|
|
36
|
+
th: () => 0,
|
|
37
|
+
/**
|
|
38
|
+
* Russian, Ukrainian, Serbian, Croatian, Bosnian
|
|
39
|
+
* Three forms: one, few (2-4), many (5+)
|
|
40
|
+
*/
|
|
41
|
+
ru: (count) => {
|
|
42
|
+
const mod10 = count % 10;
|
|
43
|
+
const mod100 = count % 100;
|
|
44
|
+
if (mod10 === 1 && mod100 !== 11) return 0;
|
|
45
|
+
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) return 1;
|
|
46
|
+
return 2;
|
|
47
|
+
},
|
|
48
|
+
uk: (count) => {
|
|
49
|
+
const mod10 = count % 10;
|
|
50
|
+
const mod100 = count % 100;
|
|
51
|
+
if (mod10 === 1 && mod100 !== 11) return 0;
|
|
52
|
+
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) return 1;
|
|
53
|
+
return 2;
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* Polish
|
|
57
|
+
* Three forms with different rules
|
|
58
|
+
*/
|
|
59
|
+
pl: (count) => {
|
|
60
|
+
if (count === 1) return 0;
|
|
61
|
+
const mod10 = count % 10;
|
|
62
|
+
const mod100 = count % 100;
|
|
63
|
+
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) return 1;
|
|
64
|
+
return 2;
|
|
65
|
+
},
|
|
66
|
+
/**
|
|
67
|
+
* Czech, Slovak
|
|
68
|
+
* Three forms: 1, 2-4, 5+
|
|
69
|
+
*/
|
|
70
|
+
cs: (count) => {
|
|
71
|
+
if (count === 1) return 0;
|
|
72
|
+
if (count >= 2 && count <= 4) return 1;
|
|
73
|
+
return 2;
|
|
74
|
+
},
|
|
75
|
+
sk: (count) => {
|
|
76
|
+
if (count === 1) return 0;
|
|
77
|
+
if (count >= 2 && count <= 4) return 1;
|
|
78
|
+
return 2;
|
|
79
|
+
},
|
|
80
|
+
/**
|
|
81
|
+
* Arabic
|
|
82
|
+
* Six forms
|
|
83
|
+
*/
|
|
84
|
+
ar: (count) => {
|
|
85
|
+
if (count === 0) return 0;
|
|
86
|
+
if (count === 1) return 1;
|
|
87
|
+
if (count === 2) return 2;
|
|
88
|
+
const mod100 = count % 100;
|
|
89
|
+
if (mod100 >= 3 && mod100 <= 10) return 3;
|
|
90
|
+
if (mod100 >= 11) return 4;
|
|
91
|
+
return 5;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Get pluralization rule for a locale.
|
|
96
|
+
*/
|
|
97
|
+
function getPluralizationRule(locale) {
|
|
98
|
+
if (pluralizationRules[locale]) return pluralizationRules[locale];
|
|
99
|
+
const langCode = locale.split("-")[0];
|
|
100
|
+
if (pluralizationRules[langCode]) return pluralizationRules[langCode];
|
|
101
|
+
return pluralizationRules.en;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Select the appropriate plural form from a translation.
|
|
105
|
+
*
|
|
106
|
+
* Translation format: "singular|plural" or "one|few|many"
|
|
107
|
+
*/
|
|
108
|
+
function selectPluralForm(translation, count, rule) {
|
|
109
|
+
const forms = translation.split("|");
|
|
110
|
+
return forms[rule(Math.abs(count))] ?? forms[forms.length - 1];
|
|
111
|
+
}
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/i18n/Translator.ts
|
|
114
|
+
const REGEXP_SPECIALS = /[.*+?^${}()|[\]\\]/g;
|
|
115
|
+
/**
|
|
116
|
+
* Translator class for handling translations.
|
|
117
|
+
*/
|
|
118
|
+
var Translator = class {
|
|
119
|
+
locale;
|
|
120
|
+
fallbackLocale;
|
|
121
|
+
messages;
|
|
122
|
+
pluralizationRules;
|
|
123
|
+
onMissingKey;
|
|
124
|
+
constructor(options) {
|
|
125
|
+
this.locale = options.locale;
|
|
126
|
+
this.fallbackLocale = options.fallbackLocale;
|
|
127
|
+
this.messages = options.messages ?? {};
|
|
128
|
+
this.pluralizationRules = options.pluralizationRules ?? {};
|
|
129
|
+
this.onMissingKey = options.onMissingKey;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Translate a key.
|
|
133
|
+
*/
|
|
134
|
+
t(key, replacements) {
|
|
135
|
+
return this.translate(key, replacements);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Translate a key with count for pluralization.
|
|
139
|
+
*/
|
|
140
|
+
tc(key, count, replacements) {
|
|
141
|
+
const translation = this.getRawTranslation(key, this.locale) ?? this.getRawTranslation(key, this.fallbackLocale);
|
|
142
|
+
if (!translation) return this.handleMissingKey(key);
|
|
143
|
+
const pluralized = selectPluralForm(translation, count, this.pluralizationRules[this.locale] ?? getPluralizationRule(this.locale));
|
|
144
|
+
return this.applyReplacements(pluralized, {
|
|
145
|
+
count,
|
|
146
|
+
...replacements
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Check if a translation exists.
|
|
151
|
+
*/
|
|
152
|
+
has(key, locale) {
|
|
153
|
+
const targetLocale = locale ?? this.locale;
|
|
154
|
+
return this.getRawTranslation(key, targetLocale) !== void 0;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Get the current locale.
|
|
158
|
+
*/
|
|
159
|
+
getLocale() {
|
|
160
|
+
return this.locale;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Set the current locale.
|
|
164
|
+
*/
|
|
165
|
+
setLocale(locale) {
|
|
166
|
+
this.locale = locale;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Get the fallback locale.
|
|
170
|
+
*/
|
|
171
|
+
getFallbackLocale() {
|
|
172
|
+
return this.fallbackLocale;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Set the fallback locale.
|
|
176
|
+
*/
|
|
177
|
+
setFallbackLocale(locale) {
|
|
178
|
+
this.fallbackLocale = locale;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Add messages for a locale.
|
|
182
|
+
*/
|
|
183
|
+
addMessages(locale, messages) {
|
|
184
|
+
this.messages[locale] = this.mergeMessages(this.messages[locale] ?? {}, messages);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Set messages for a locale (replaces existing).
|
|
188
|
+
*/
|
|
189
|
+
setMessages(locale, messages) {
|
|
190
|
+
this.messages[locale] = messages;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get all messages for a locale.
|
|
194
|
+
*/
|
|
195
|
+
getMessages(locale) {
|
|
196
|
+
return this.messages[locale ?? this.locale] ?? {};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Get available locales.
|
|
200
|
+
*/
|
|
201
|
+
getAvailableLocales() {
|
|
202
|
+
return Object.keys(this.messages);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Set a custom pluralization rule.
|
|
206
|
+
*/
|
|
207
|
+
setPluralizationRule(locale, rule) {
|
|
208
|
+
this.pluralizationRules[locale] = rule;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Internal translate method.
|
|
212
|
+
*/
|
|
213
|
+
translate(key, replacements) {
|
|
214
|
+
let translation = this.getRawTranslation(key, this.locale);
|
|
215
|
+
if (translation === void 0 && this.fallbackLocale) translation = this.getRawTranslation(key, this.fallbackLocale);
|
|
216
|
+
if (translation === void 0) return this.handleMissingKey(key);
|
|
217
|
+
return this.applyReplacements(translation, replacements);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Get raw translation without replacements.
|
|
221
|
+
*/
|
|
222
|
+
getRawTranslation(key, locale) {
|
|
223
|
+
if (!locale) return void 0;
|
|
224
|
+
const messages = this.messages[locale];
|
|
225
|
+
if (!messages) return void 0;
|
|
226
|
+
const parts = key.split(".");
|
|
227
|
+
let result = messages;
|
|
228
|
+
for (const part of parts) {
|
|
229
|
+
if (result === void 0 || typeof result === "string") return;
|
|
230
|
+
result = result[part];
|
|
231
|
+
}
|
|
232
|
+
return typeof result === "string" ? result : void 0;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Apply replacements to a translation string.
|
|
236
|
+
*/
|
|
237
|
+
applyReplacements(translation, replacements) {
|
|
238
|
+
if (!replacements) return translation;
|
|
239
|
+
let result = translation;
|
|
240
|
+
for (const [key, value] of Object.entries(replacements)) {
|
|
241
|
+
const escaped = key.replace(REGEXP_SPECIALS, "\\$&");
|
|
242
|
+
const replacement = () => String(value);
|
|
243
|
+
result = result.replace(new RegExp(`:${escaped}`, "g"), replacement).replace(new RegExp(`\\{${escaped}\\}`, "g"), replacement);
|
|
244
|
+
}
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Handle missing translation key.
|
|
249
|
+
*/
|
|
250
|
+
handleMissingKey(key) {
|
|
251
|
+
if (this.onMissingKey) {
|
|
252
|
+
const result = this.onMissingKey(key, this.locale);
|
|
253
|
+
if (result !== void 0) return result;
|
|
254
|
+
}
|
|
255
|
+
return key;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Deep merge messages.
|
|
259
|
+
*/
|
|
260
|
+
mergeMessages(target, source) {
|
|
261
|
+
const result = { ...target };
|
|
262
|
+
for (const [key, value] of Object.entries(source)) if (typeof value === "object" && value !== null && typeof result[key] === "object" && result[key] !== null) result[key] = this.mergeMessages(result[key], value);
|
|
263
|
+
else result[key] = value;
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
//#endregion
|
|
268
|
+
//#region src/i18n/loaders/JsonLoader.ts
|
|
269
|
+
/**
|
|
270
|
+
* JSON file-based translation loader.
|
|
271
|
+
*
|
|
272
|
+
* Expected directory structure:
|
|
273
|
+
* path/
|
|
274
|
+
* en/
|
|
275
|
+
* messages.json
|
|
276
|
+
* validation.json
|
|
277
|
+
* ja/
|
|
278
|
+
* messages.json
|
|
279
|
+
* validation.json
|
|
280
|
+
*
|
|
281
|
+
* This layout (locale subdirectories, namespace = file name) is a contract
|
|
282
|
+
* shared with the CLI: `guren codegen` (packages/cli/src/i18n-types.ts) and
|
|
283
|
+
* `guren check --i18n` (packages/cli/src/i18n-check.ts) re-implement the
|
|
284
|
+
* same walk statically. Changing the layout or parse tolerance here strands
|
|
285
|
+
* them on the old contract — update both together.
|
|
286
|
+
*/
|
|
287
|
+
var JsonLoader = class {
|
|
288
|
+
basePath;
|
|
289
|
+
cache = /* @__PURE__ */ new Map();
|
|
290
|
+
useCache;
|
|
291
|
+
constructor(basePath, options = {}) {
|
|
292
|
+
this.basePath = basePath;
|
|
293
|
+
this.useCache = options.cache ?? true;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Load all translations for a locale.
|
|
297
|
+
*/
|
|
298
|
+
async load(locale) {
|
|
299
|
+
const cacheKey = locale;
|
|
300
|
+
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
301
|
+
const localePath = join(this.basePath, locale);
|
|
302
|
+
const messages = {};
|
|
303
|
+
try {
|
|
304
|
+
const files = await readdir(localePath);
|
|
305
|
+
for (const file of files) {
|
|
306
|
+
if (extname(file) !== ".json") continue;
|
|
307
|
+
const namespace = basename(file, ".json");
|
|
308
|
+
const filePath = join(localePath, file);
|
|
309
|
+
const content = await readFile(filePath, "utf-8");
|
|
310
|
+
try {
|
|
311
|
+
messages[namespace] = JSON.parse(content);
|
|
312
|
+
} catch {
|
|
313
|
+
console.warn(`Failed to parse translation file: ${filePath}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} catch {}
|
|
317
|
+
if (this.useCache) this.cache.set(cacheKey, messages);
|
|
318
|
+
return messages;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Load translations for a specific namespace.
|
|
322
|
+
*/
|
|
323
|
+
async loadNamespace(locale, namespace) {
|
|
324
|
+
const cacheKey = `${locale}:${namespace}`;
|
|
325
|
+
if (this.useCache && this.cache.has(cacheKey)) return this.cache.get(cacheKey);
|
|
326
|
+
const filePath = join(this.basePath, locale, `${namespace}.json`);
|
|
327
|
+
try {
|
|
328
|
+
const content = await readFile(filePath, "utf-8");
|
|
329
|
+
const messages = JSON.parse(content);
|
|
330
|
+
if (this.useCache) this.cache.set(cacheKey, messages);
|
|
331
|
+
return messages;
|
|
332
|
+
} catch {
|
|
333
|
+
return {};
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Get available locales.
|
|
338
|
+
*/
|
|
339
|
+
async getAvailableLocales() {
|
|
340
|
+
try {
|
|
341
|
+
return (await readdir(this.basePath, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
342
|
+
} catch {
|
|
343
|
+
return [];
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Clear the cache.
|
|
348
|
+
*/
|
|
349
|
+
clearCache() {
|
|
350
|
+
this.cache.clear();
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Enable or disable caching.
|
|
354
|
+
*/
|
|
355
|
+
setCaching(enabled) {
|
|
356
|
+
this.useCache = enabled;
|
|
357
|
+
if (!enabled) this.clearCache();
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
//#endregion
|
|
361
|
+
//#region src/i18n/I18nManager.ts
|
|
362
|
+
/**
|
|
363
|
+
* I18n manager for handling multiple locales and translators.
|
|
364
|
+
*/
|
|
365
|
+
var I18nManager = class {
|
|
366
|
+
config;
|
|
367
|
+
translator;
|
|
368
|
+
loader;
|
|
369
|
+
loadedLocales = /* @__PURE__ */ new Set();
|
|
370
|
+
constructor(config) {
|
|
371
|
+
this.config = config;
|
|
372
|
+
this.loader = config.loader ?? (config.path ? new JsonLoader(config.path) : null);
|
|
373
|
+
this.translator = new Translator({
|
|
374
|
+
locale: config.locale,
|
|
375
|
+
fallbackLocale: config.fallbackLocale,
|
|
376
|
+
messages: config.messages ?? {}
|
|
377
|
+
});
|
|
378
|
+
if (config.messages) for (const locale of Object.keys(config.messages)) this.loadedLocales.add(locale);
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Translate a key.
|
|
382
|
+
*/
|
|
383
|
+
t(key, replacements) {
|
|
384
|
+
return this.translator.t(key, replacements);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Translate a key with count for pluralization.
|
|
388
|
+
*/
|
|
389
|
+
tc(key, count, replacements) {
|
|
390
|
+
return this.translator.tc(key, count, replacements);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Check if a translation exists.
|
|
394
|
+
*/
|
|
395
|
+
has(key, locale) {
|
|
396
|
+
return this.translator.has(key, locale);
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Get the current locale.
|
|
400
|
+
*/
|
|
401
|
+
getLocale() {
|
|
402
|
+
return this.translator.getLocale();
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Set the current locale.
|
|
406
|
+
*/
|
|
407
|
+
setLocale(locale) {
|
|
408
|
+
this.translator.setLocale(locale);
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Get the fallback locale.
|
|
412
|
+
*/
|
|
413
|
+
getFallbackLocale() {
|
|
414
|
+
return this.translator.getFallbackLocale();
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Set the fallback locale.
|
|
418
|
+
*/
|
|
419
|
+
setFallbackLocale(locale) {
|
|
420
|
+
this.translator.setFallbackLocale(locale);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Load translations for a locale.
|
|
424
|
+
*/
|
|
425
|
+
async loadLocale(locale) {
|
|
426
|
+
if (this.loadedLocales.has(locale)) return;
|
|
427
|
+
if (!this.loader) return;
|
|
428
|
+
const messages = await this.loader.load(locale);
|
|
429
|
+
this.translator.addMessages(locale, messages);
|
|
430
|
+
this.loadedLocales.add(locale);
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Load translations for multiple locales.
|
|
434
|
+
*/
|
|
435
|
+
async loadLocales(locales) {
|
|
436
|
+
await Promise.all(locales.map((locale) => this.loadLocale(locale)));
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Load a specific namespace for a locale.
|
|
440
|
+
*/
|
|
441
|
+
async loadNamespace(locale, namespace) {
|
|
442
|
+
if (!this.loader?.loadNamespace) return;
|
|
443
|
+
const messages = await this.loader.loadNamespace(locale, namespace);
|
|
444
|
+
const currentMessages = this.translator.getMessages(locale);
|
|
445
|
+
this.translator.setMessages(locale, {
|
|
446
|
+
...currentMessages,
|
|
447
|
+
[namespace]: messages
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Add messages for a locale.
|
|
452
|
+
*/
|
|
453
|
+
addMessages(locale, messages) {
|
|
454
|
+
this.translator.addMessages(locale, messages);
|
|
455
|
+
this.loadedLocales.add(locale);
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Get all messages for a locale.
|
|
459
|
+
*/
|
|
460
|
+
getMessages(locale) {
|
|
461
|
+
return this.translator.getMessages(locale);
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Get available locales.
|
|
465
|
+
*/
|
|
466
|
+
getAvailableLocales() {
|
|
467
|
+
return this.translator.getAvailableLocales();
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Get available locales from loader.
|
|
471
|
+
*/
|
|
472
|
+
async getLoaderLocales() {
|
|
473
|
+
if (!this.loader?.getAvailableLocales) return [];
|
|
474
|
+
return this.loader.getAvailableLocales();
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Check if a locale is loaded.
|
|
478
|
+
*/
|
|
479
|
+
isLocaleLoaded(locale) {
|
|
480
|
+
return this.loadedLocales.has(locale);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Get the underlying translator.
|
|
484
|
+
*/
|
|
485
|
+
getTranslator() {
|
|
486
|
+
return this.translator;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Set a custom loader.
|
|
490
|
+
*/
|
|
491
|
+
setLoader(loader) {
|
|
492
|
+
this.loader = loader;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Create a scoped translator for a specific locale.
|
|
496
|
+
*/
|
|
497
|
+
forLocale(locale) {
|
|
498
|
+
return new Translator({
|
|
499
|
+
locale,
|
|
500
|
+
fallbackLocale: this.config.fallbackLocale,
|
|
501
|
+
messages: this.messagesForLocale(locale)
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* The message catalogs that accompany a locale: the fallback locale's
|
|
506
|
+
* (when it differs) plus the locale's own, in that order — so consumers
|
|
507
|
+
* that flatten the record let the active locale win on key collisions.
|
|
508
|
+
*/
|
|
509
|
+
messagesForLocale(locale) {
|
|
510
|
+
const fallback = this.config.fallbackLocale;
|
|
511
|
+
const messages = {};
|
|
512
|
+
if (fallback && fallback !== locale) messages[fallback] = this.translator.getMessages(fallback);
|
|
513
|
+
messages[locale] = this.translator.getMessages(locale);
|
|
514
|
+
return messages;
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
let globalI18n = null;
|
|
518
|
+
/**
|
|
519
|
+
* Create a new I18n manager.
|
|
520
|
+
*/
|
|
521
|
+
function createI18n(config) {
|
|
522
|
+
return new I18nManager(config);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Set the global I18n manager.
|
|
526
|
+
*/
|
|
527
|
+
function setI18n(i18n) {
|
|
528
|
+
globalI18n = i18n;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Get the global I18n manager.
|
|
532
|
+
*/
|
|
533
|
+
function getI18n() {
|
|
534
|
+
if (!globalI18n) throw new Error("I18n manager not initialized. Call setI18n() first.");
|
|
535
|
+
return globalI18n;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Get the global I18n manager, or `undefined` when none was registered.
|
|
539
|
+
*/
|
|
540
|
+
function tryGetI18n() {
|
|
541
|
+
return globalI18n ?? void 0;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Translate a key using the global I18n manager.
|
|
545
|
+
*/
|
|
546
|
+
function t(key, replacements) {
|
|
547
|
+
return getI18n().t(key, replacements);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Translate a key with count using the global I18n manager.
|
|
551
|
+
*/
|
|
552
|
+
function tc(key, count, replacements) {
|
|
553
|
+
return getI18n().tc(key, count, replacements);
|
|
554
|
+
}
|
|
555
|
+
//#endregion
|
|
556
|
+
//#region src/i18n/loaders/MemoryLoader.ts
|
|
557
|
+
/**
|
|
558
|
+
* In-memory translation loader.
|
|
559
|
+
* Useful for testing or when translations are bundled with the application.
|
|
560
|
+
*/
|
|
561
|
+
var MemoryLoader = class {
|
|
562
|
+
messages;
|
|
563
|
+
constructor(messages = {}) {
|
|
564
|
+
this.messages = messages;
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Load all translations for a locale.
|
|
568
|
+
*/
|
|
569
|
+
async load(locale) {
|
|
570
|
+
return this.messages[locale] ?? {};
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Load translations for a specific namespace.
|
|
574
|
+
*/
|
|
575
|
+
async loadNamespace(locale, namespace) {
|
|
576
|
+
const localeMessages = this.messages[locale];
|
|
577
|
+
if (!localeMessages) return {};
|
|
578
|
+
const namespaceMessages = localeMessages[namespace];
|
|
579
|
+
if (typeof namespaceMessages === "object" && namespaceMessages !== null) return namespaceMessages;
|
|
580
|
+
return {};
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Get available locales.
|
|
584
|
+
*/
|
|
585
|
+
async getAvailableLocales() {
|
|
586
|
+
return Object.keys(this.messages);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Add messages for a locale.
|
|
590
|
+
*/
|
|
591
|
+
addMessages(locale, messages) {
|
|
592
|
+
this.messages[locale] = {
|
|
593
|
+
...this.messages[locale],
|
|
594
|
+
...messages
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Set messages for a locale (replaces existing).
|
|
599
|
+
*/
|
|
600
|
+
setMessages(locale, messages) {
|
|
601
|
+
this.messages[locale] = messages;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Remove messages for a locale.
|
|
605
|
+
*/
|
|
606
|
+
removeLocale(locale) {
|
|
607
|
+
delete this.messages[locale];
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Clear all messages.
|
|
611
|
+
*/
|
|
612
|
+
clear() {
|
|
613
|
+
this.messages = {};
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
//#endregion
|
|
617
|
+
export { setI18n as a, tryGetI18n as c, getPluralizationRule as d, pluralizationRules as f, getI18n as i, JsonLoader as l, I18nManager as n, t as o, selectPluralForm as p, createI18n as r, tc as s, MemoryLoader as t, Translator as u };
|