@hua-labs/i18n-core 2.2.0 → 2.2.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/LICENSE +21 -0
- package/README.md +4 -4
- package/dist/{chunk-7ZYOSEMW.mjs → chunk-4IYWT7MS.mjs} +143 -45
- package/dist/chunk-4IYWT7MS.mjs.map +1 -0
- package/dist/index.cjs +500 -288
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +21 -21
- package/dist/index.d.ts +21 -21
- package/dist/index.mjs +361 -247
- package/dist/index.mjs.map +1 -1
- package/dist/{server-DgpyR0RE.d.mts → server-CQztOmd-.d.mts} +21 -7
- package/dist/{server-DgpyR0RE.d.ts → server-CQztOmd-.d.ts} +21 -7
- package/dist/server.cjs +141 -43
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.mts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.mjs +1 -1
- package/package.json +9 -9
- package/src/__tests__/default-value.test.ts +149 -0
- package/src/core/translator.tsx +385 -188
- package/src/hooks/useI18n.tsx +490 -337
- package/src/types/index.ts +291 -163
- package/dist/chunk-7ZYOSEMW.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { validateI18nConfig, webPlatformAdapter, Translator } from './chunk-
|
|
2
|
-
export { Translator, headlessPlatformAdapter, serverTranslate, ssrTranslate, webPlatformAdapter } from './chunk-
|
|
1
|
+
import { validateI18nConfig, webPlatformAdapter, Translator } from './chunk-4IYWT7MS.mjs';
|
|
2
|
+
export { Translator, headlessPlatformAdapter, serverTranslate, ssrTranslate, webPlatformAdapter } from './chunk-4IYWT7MS.mjs';
|
|
3
3
|
import React, { createContext, useState, useEffect, useMemo, useCallback, useContext } from 'react';
|
|
4
4
|
import { jsx } from 'react/jsx-runtime';
|
|
5
5
|
|
|
@@ -222,7 +222,9 @@ function I18nProvider({
|
|
|
222
222
|
config,
|
|
223
223
|
children
|
|
224
224
|
}) {
|
|
225
|
-
const [currentLanguage, setCurrentLanguageState] = useState(
|
|
225
|
+
const [currentLanguage, setCurrentLanguageState] = useState(
|
|
226
|
+
() => resolveInitialLanguage(config)
|
|
227
|
+
);
|
|
226
228
|
const [isLoading, setIsLoading] = useState(true);
|
|
227
229
|
const [isInitialized, setIsInitialized] = useState(false);
|
|
228
230
|
const [error, setError] = useState(null);
|
|
@@ -243,7 +245,9 @@ function I18nProvider({
|
|
|
243
245
|
const translatorLang = translator.getCurrentLanguage();
|
|
244
246
|
if (translatorLang !== currentLanguage) {
|
|
245
247
|
if (config.debug) {
|
|
246
|
-
console.log(
|
|
248
|
+
console.log(
|
|
249
|
+
`\u{1F504} [USEI18N] Syncing translator language: ${translatorLang} -> ${currentLanguage} (already initialized)`
|
|
250
|
+
);
|
|
247
251
|
}
|
|
248
252
|
translator.setLanguage(currentLanguage);
|
|
249
253
|
}
|
|
@@ -268,13 +272,18 @@ function I18nProvider({
|
|
|
268
272
|
await translator.initialize();
|
|
269
273
|
setIsInitialized(true);
|
|
270
274
|
if (config.debug) {
|
|
271
|
-
console.log(
|
|
275
|
+
console.log(
|
|
276
|
+
"\u2705 [USEI18N] Translator initialization completed successfully"
|
|
277
|
+
);
|
|
272
278
|
}
|
|
273
279
|
} catch (err) {
|
|
274
280
|
const initError = err;
|
|
275
281
|
setError(initError);
|
|
276
282
|
if (config.debug) {
|
|
277
|
-
console.error(
|
|
283
|
+
console.error(
|
|
284
|
+
"\u274C [USEI18N] Failed to initialize translator:",
|
|
285
|
+
initError
|
|
286
|
+
);
|
|
278
287
|
}
|
|
279
288
|
setIsInitialized(true);
|
|
280
289
|
} finally {
|
|
@@ -302,7 +311,9 @@ function I18nProvider({
|
|
|
302
311
|
const unsubscribe = translator.onLanguageChanged((newLanguage) => {
|
|
303
312
|
if (newLanguage !== currentLanguage) {
|
|
304
313
|
if (config.debug) {
|
|
305
|
-
console.log(
|
|
314
|
+
console.log(
|
|
315
|
+
`\u{1F504} [USEI18N] Language changed event: ${currentLanguage} -> ${newLanguage}`
|
|
316
|
+
);
|
|
306
317
|
}
|
|
307
318
|
setCurrentLanguageState(newLanguage);
|
|
308
319
|
setTranslationVersion((prev) => prev + 1);
|
|
@@ -324,38 +335,46 @@ function I18nProvider({
|
|
|
324
335
|
}
|
|
325
336
|
});
|
|
326
337
|
}, [config.autoLanguageSync, config.platformAdapter, currentLanguage]);
|
|
327
|
-
const setLanguage = useCallback(
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const currentLang = translator.getCurrentLanguage();
|
|
332
|
-
if (currentLang === language) {
|
|
333
|
-
if (config.debug) {
|
|
334
|
-
console.log(`\u23ED\uFE0F [USEI18N] Language unchanged, skipping: ${language}`);
|
|
338
|
+
const setLanguage = useCallback(
|
|
339
|
+
async (language) => {
|
|
340
|
+
if (!translator) {
|
|
341
|
+
return;
|
|
335
342
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
343
|
+
const currentLang = translator.getCurrentLanguage();
|
|
344
|
+
if (currentLang === language) {
|
|
345
|
+
if (config.debug) {
|
|
346
|
+
console.log(`\u23ED\uFE0F [USEI18N] Language unchanged, skipping: ${language}`);
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
341
349
|
}
|
|
342
|
-
}
|
|
343
|
-
setIsLoading(true);
|
|
344
|
-
try {
|
|
345
|
-
translator.setLanguage(language);
|
|
346
|
-
setCurrentLanguageState(language);
|
|
347
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
348
350
|
if (config.debug) {
|
|
349
|
-
|
|
351
|
+
if (config.debug) {
|
|
352
|
+
console.log(
|
|
353
|
+
`\u{1F504} [USEI18N] setLanguage called: ${currentLang} -> ${language}`
|
|
354
|
+
);
|
|
355
|
+
}
|
|
350
356
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
357
|
+
setIsLoading(true);
|
|
358
|
+
try {
|
|
359
|
+
translator.setLanguage(language);
|
|
360
|
+
setCurrentLanguageState(language);
|
|
361
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
362
|
+
if (config.debug) {
|
|
363
|
+
console.log(`\u2705 [USEI18N] Language changed to ${language}`);
|
|
364
|
+
}
|
|
365
|
+
} catch (error2) {
|
|
366
|
+
if (config.debug) {
|
|
367
|
+
console.error(
|
|
368
|
+
`\u274C [USEI18N] Failed to change language to ${language}:`,
|
|
369
|
+
error2
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
} finally {
|
|
373
|
+
setIsLoading(false);
|
|
354
374
|
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
}, [translator, config.debug]);
|
|
375
|
+
},
|
|
376
|
+
[translator, config.debug]
|
|
377
|
+
);
|
|
359
378
|
const parseKey = useCallback((key) => {
|
|
360
379
|
const parts = key.split(":");
|
|
361
380
|
if (parts.length >= 2) {
|
|
@@ -363,246 +382,336 @@ function I18nProvider({
|
|
|
363
382
|
}
|
|
364
383
|
return { namespace: "common", key };
|
|
365
384
|
}, []);
|
|
366
|
-
const resolveNestedKey = useCallback(
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
current
|
|
375
|
-
|
|
376
|
-
|
|
385
|
+
const resolveNestedKey = useCallback(
|
|
386
|
+
(obj, key) => {
|
|
387
|
+
if (key in obj && typeof obj[key] === "string") {
|
|
388
|
+
return obj[key];
|
|
389
|
+
}
|
|
390
|
+
const parts = key.split(".");
|
|
391
|
+
let current = obj;
|
|
392
|
+
for (const part of parts) {
|
|
393
|
+
if (current && typeof current === "object" && current !== null && part in current) {
|
|
394
|
+
current = current[part];
|
|
395
|
+
} else {
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
377
398
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
const ssrTranslations = config.initialTranslations[targetLang]?.[namespace];
|
|
387
|
-
if (ssrTranslations) {
|
|
388
|
-
const value2 = resolveNestedKey(ssrTranslations, actualKey);
|
|
389
|
-
if (value2 !== null) {
|
|
390
|
-
return value2;
|
|
399
|
+
return typeof current === "string" ? current : null;
|
|
400
|
+
},
|
|
401
|
+
[]
|
|
402
|
+
);
|
|
403
|
+
const findInSSRTranslations = useCallback(
|
|
404
|
+
(key, targetLang) => {
|
|
405
|
+
if (!config.initialTranslations) {
|
|
406
|
+
return null;
|
|
391
407
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
408
|
+
const { namespace, key: actualKey } = parseKey(key);
|
|
409
|
+
const ssrTranslations = config.initialTranslations[targetLang]?.[namespace];
|
|
410
|
+
if (ssrTranslations) {
|
|
411
|
+
const value2 = resolveNestedKey(
|
|
412
|
+
ssrTranslations,
|
|
413
|
+
actualKey
|
|
414
|
+
);
|
|
398
415
|
if (value2 !== null) {
|
|
399
416
|
return value2;
|
|
400
417
|
}
|
|
401
418
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
}
|
|
415
|
-
let params;
|
|
416
|
-
let lang;
|
|
417
|
-
if (typeof paramsOrLang === "string") {
|
|
418
|
-
lang = paramsOrLang;
|
|
419
|
-
} else if (typeof paramsOrLang === "object" && paramsOrLang !== null) {
|
|
420
|
-
params = paramsOrLang;
|
|
421
|
-
lang = language;
|
|
422
|
-
}
|
|
423
|
-
const targetLang = lang || currentLanguage;
|
|
424
|
-
try {
|
|
425
|
-
const result = translator.translate(key, params || lang, params ? lang : void 0);
|
|
426
|
-
if (result && result !== key && result !== "") {
|
|
427
|
-
return result;
|
|
419
|
+
const fallbackLang = config.fallbackLanguage || "en";
|
|
420
|
+
if (targetLang !== fallbackLang) {
|
|
421
|
+
const fallbackTranslations = config.initialTranslations[fallbackLang]?.[namespace];
|
|
422
|
+
if (fallbackTranslations) {
|
|
423
|
+
const value2 = resolveNestedKey(
|
|
424
|
+
fallbackTranslations,
|
|
425
|
+
actualKey
|
|
426
|
+
);
|
|
427
|
+
if (value2 !== null) {
|
|
428
|
+
return value2;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
428
431
|
}
|
|
429
|
-
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
432
|
+
return null;
|
|
433
|
+
},
|
|
434
|
+
[
|
|
435
|
+
config.initialTranslations,
|
|
436
|
+
config.fallbackLanguage,
|
|
437
|
+
parseKey,
|
|
438
|
+
resolveNestedKey
|
|
439
|
+
]
|
|
440
|
+
);
|
|
441
|
+
const findInDefaultTranslations = useCallback(
|
|
442
|
+
(key, targetLang) => {
|
|
443
|
+
const { namespace, key: actualKey } = parseKey(key);
|
|
444
|
+
const defaultTranslations = getDefaultTranslations(targetLang, namespace);
|
|
445
|
+
const fallbackTranslations = getDefaultTranslations(
|
|
446
|
+
config.fallbackLanguage || "en",
|
|
447
|
+
namespace
|
|
448
|
+
);
|
|
449
|
+
return resolveNestedKey(
|
|
450
|
+
defaultTranslations,
|
|
451
|
+
actualKey
|
|
452
|
+
) || resolveNestedKey(
|
|
453
|
+
fallbackTranslations,
|
|
454
|
+
actualKey
|
|
455
|
+
) || null;
|
|
456
|
+
},
|
|
457
|
+
[config.fallbackLanguage, parseKey, resolveNestedKey]
|
|
458
|
+
);
|
|
459
|
+
const t = useCallback(
|
|
460
|
+
(key, paramsOrLang, language) => {
|
|
461
|
+
if (!translator) {
|
|
462
|
+
return key;
|
|
463
|
+
}
|
|
464
|
+
let params;
|
|
465
|
+
let lang;
|
|
466
|
+
if (typeof paramsOrLang === "string") {
|
|
467
|
+
lang = paramsOrLang;
|
|
468
|
+
} else if (typeof paramsOrLang === "object" && paramsOrLang !== null) {
|
|
469
|
+
params = paramsOrLang;
|
|
470
|
+
lang = language;
|
|
471
|
+
}
|
|
472
|
+
const targetLang = lang || currentLanguage;
|
|
473
|
+
try {
|
|
474
|
+
const result = translator.translate(
|
|
475
|
+
key,
|
|
476
|
+
params || lang,
|
|
477
|
+
params ? lang : void 0
|
|
478
|
+
);
|
|
479
|
+
if (result && result !== key && result !== "") {
|
|
480
|
+
return result;
|
|
481
|
+
}
|
|
482
|
+
} catch (error2) {
|
|
455
483
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
484
|
+
const interpolate = (text) => {
|
|
485
|
+
if (!params) return text;
|
|
486
|
+
return text.replace(/\{\{(\w+)\}\}/g, (match, k) => {
|
|
487
|
+
const value2 = params[k];
|
|
488
|
+
return value2 !== void 0 ? String(value2) : match;
|
|
489
|
+
});
|
|
490
|
+
};
|
|
491
|
+
const ssrResult = findInSSRTranslations(key, targetLang);
|
|
492
|
+
if (ssrResult) {
|
|
493
|
+
return interpolate(ssrResult);
|
|
465
494
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}
|
|
470
|
-
}, [translator, config.debug]);
|
|
471
|
-
const tSync = useCallback((key, namespace, params) => {
|
|
472
|
-
if (!translator) {
|
|
473
|
-
if (config.debug) {
|
|
474
|
-
console.warn("Translator not initialized");
|
|
495
|
+
const defaultResult = findInDefaultTranslations(key, targetLang);
|
|
496
|
+
if (defaultResult) {
|
|
497
|
+
return interpolate(defaultResult);
|
|
475
498
|
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
return translator.translateSync(key, params);
|
|
479
|
-
}, [translator, config.debug]);
|
|
480
|
-
const getRawValue = useCallback((key, language) => {
|
|
481
|
-
if (!translator || !isInitialized) {
|
|
482
|
-
return void 0;
|
|
483
|
-
}
|
|
484
|
-
return translator.getRawValue(key, language);
|
|
485
|
-
}, [translator, isInitialized]);
|
|
486
|
-
const tArray = useCallback((key, language) => {
|
|
487
|
-
if (!translator || !isInitialized) {
|
|
488
|
-
return [];
|
|
489
|
-
}
|
|
490
|
-
return translator.tArray(key, language);
|
|
491
|
-
}, [translator, isInitialized, translationVersion, currentLanguage]);
|
|
492
|
-
const tPlural = useCallback((key, count, params, language) => {
|
|
493
|
-
if (!translator || !isInitialized) {
|
|
494
|
-
return key;
|
|
495
|
-
}
|
|
496
|
-
return translator.tPlural(key, count, params, language);
|
|
497
|
-
}, [translator, isInitialized, translationVersion, currentLanguage]);
|
|
498
|
-
const debug = useMemo(() => ({
|
|
499
|
-
getCurrentLanguage: () => {
|
|
500
|
-
try {
|
|
501
|
-
return translator?.getCurrentLanguage() || currentLanguage;
|
|
502
|
-
} catch {
|
|
503
|
-
return currentLanguage;
|
|
499
|
+
if (typeof paramsOrLang === "object" && paramsOrLang !== null && "defaultValue" in paramsOrLang && typeof paramsOrLang.defaultValue === "string") {
|
|
500
|
+
return interpolate(paramsOrLang.defaultValue);
|
|
504
501
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
try {
|
|
508
|
-
return translator?.getSupportedLanguages() || config.supportedLanguages?.map((l) => l.code) || [];
|
|
509
|
-
} catch {
|
|
510
|
-
return config.supportedLanguages?.map((l) => l.code) || [];
|
|
502
|
+
if (config.debug) {
|
|
503
|
+
return interpolate(key);
|
|
511
504
|
}
|
|
505
|
+
return "";
|
|
512
506
|
},
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
return Array.from(namespaces);
|
|
507
|
+
[
|
|
508
|
+
translator,
|
|
509
|
+
config.debug,
|
|
510
|
+
currentLanguage,
|
|
511
|
+
config.fallbackLanguage,
|
|
512
|
+
translationVersion,
|
|
513
|
+
findInSSRTranslations,
|
|
514
|
+
findInDefaultTranslations
|
|
515
|
+
]
|
|
516
|
+
);
|
|
517
|
+
const tAsync = useCallback(
|
|
518
|
+
async (key, params) => {
|
|
519
|
+
if (!translator) {
|
|
520
|
+
if (config.debug) {
|
|
521
|
+
console.warn("Translator not initialized");
|
|
529
522
|
}
|
|
530
|
-
return
|
|
531
|
-
} catch (error2) {
|
|
532
|
-
return [];
|
|
523
|
+
return key;
|
|
533
524
|
}
|
|
534
|
-
|
|
535
|
-
getAllTranslations: () => {
|
|
525
|
+
setIsLoading(true);
|
|
536
526
|
try {
|
|
537
|
-
|
|
527
|
+
const result = await translator.translateAsync(key, params);
|
|
528
|
+
return result;
|
|
538
529
|
} catch (error2) {
|
|
539
|
-
|
|
530
|
+
if (config.debug) {
|
|
531
|
+
console.error("Translation error:", error2);
|
|
532
|
+
}
|
|
533
|
+
return key;
|
|
534
|
+
} finally {
|
|
535
|
+
setIsLoading(false);
|
|
540
536
|
}
|
|
541
537
|
},
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
538
|
+
[translator, config.debug]
|
|
539
|
+
);
|
|
540
|
+
const tSync = useCallback(
|
|
541
|
+
(key, namespace, params) => {
|
|
542
|
+
if (!translator) {
|
|
543
|
+
if (config.debug) {
|
|
544
|
+
console.warn("Translator not initialized");
|
|
545
|
+
}
|
|
546
|
+
return key;
|
|
547
547
|
}
|
|
548
|
+
return translator.translateSync(key, params);
|
|
548
549
|
},
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
550
|
+
[translator, config.debug]
|
|
551
|
+
);
|
|
552
|
+
const getRawValue = useCallback(
|
|
553
|
+
(key, language) => {
|
|
554
|
+
if (!translator || !isInitialized) {
|
|
555
|
+
return void 0;
|
|
556
|
+
}
|
|
557
|
+
return translator.getRawValue(key, language);
|
|
555
558
|
},
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
559
|
+
[translator, isInitialized]
|
|
560
|
+
);
|
|
561
|
+
const tArray = useCallback(
|
|
562
|
+
(key, language) => {
|
|
563
|
+
if (!translator || !isInitialized) {
|
|
564
|
+
return [];
|
|
560
565
|
}
|
|
566
|
+
return translator.tArray(key, language);
|
|
561
567
|
},
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
};
|
|
571
|
-
}
|
|
572
|
-
return { size: 0, hits: 0, misses: 0 };
|
|
573
|
-
} catch (error2) {
|
|
574
|
-
return { size: 0, hits: 0, misses: 0 };
|
|
575
|
-
}
|
|
568
|
+
[translator, isInitialized, translationVersion, currentLanguage]
|
|
569
|
+
);
|
|
570
|
+
const tPlural = useCallback(
|
|
571
|
+
(key, count, params, language) => {
|
|
572
|
+
if (!translator || !isInitialized) {
|
|
573
|
+
return key;
|
|
574
|
+
}
|
|
575
|
+
return translator.tPlural(key, count, params, language);
|
|
576
576
|
},
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
577
|
+
[translator, isInitialized, translationVersion, currentLanguage]
|
|
578
|
+
);
|
|
579
|
+
const debug = useMemo(
|
|
580
|
+
() => ({
|
|
581
|
+
getCurrentLanguage: () => {
|
|
581
582
|
try {
|
|
582
|
-
|
|
583
|
-
} catch
|
|
584
|
-
|
|
585
|
-
}
|
|
586
|
-
|
|
583
|
+
return translator?.getCurrentLanguage() || currentLanguage;
|
|
584
|
+
} catch {
|
|
585
|
+
return currentLanguage;
|
|
586
|
+
}
|
|
587
|
+
},
|
|
588
|
+
getSupportedLanguages: () => {
|
|
589
|
+
try {
|
|
590
|
+
return translator?.getSupportedLanguages() || config.supportedLanguages?.map((l) => l.code) || [];
|
|
591
|
+
} catch {
|
|
592
|
+
return config.supportedLanguages?.map((l) => l.code) || [];
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
getLoadedNamespaces: () => {
|
|
596
|
+
try {
|
|
597
|
+
const debugInfo = translator?.debug();
|
|
598
|
+
if (debugInfo && debugInfo.loadedNamespaces) {
|
|
599
|
+
return Array.from(debugInfo.loadedNamespaces);
|
|
600
|
+
}
|
|
601
|
+
if (debugInfo && debugInfo.allTranslations) {
|
|
602
|
+
const namespaces = /* @__PURE__ */ new Set();
|
|
603
|
+
Object.values(debugInfo.allTranslations).forEach(
|
|
604
|
+
(langData) => {
|
|
605
|
+
if (langData && typeof langData === "object") {
|
|
606
|
+
Object.keys(langData).forEach((namespace) => {
|
|
607
|
+
namespaces.add(namespace);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
);
|
|
612
|
+
return Array.from(namespaces);
|
|
613
|
+
}
|
|
614
|
+
return [];
|
|
615
|
+
} catch (error2) {
|
|
616
|
+
return [];
|
|
617
|
+
}
|
|
618
|
+
},
|
|
619
|
+
getAllTranslations: () => {
|
|
620
|
+
try {
|
|
621
|
+
return translator?.debug()?.allTranslations || {};
|
|
622
|
+
} catch (error2) {
|
|
623
|
+
return {};
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
isReady: () => {
|
|
627
|
+
try {
|
|
628
|
+
return translator?.isReady() || isInitialized;
|
|
629
|
+
} catch {
|
|
630
|
+
return isInitialized;
|
|
631
|
+
}
|
|
632
|
+
},
|
|
633
|
+
getInitializationError: () => {
|
|
634
|
+
try {
|
|
635
|
+
return translator?.getInitializationError() || error;
|
|
636
|
+
} catch {
|
|
637
|
+
return error;
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
clearCache: () => {
|
|
641
|
+
try {
|
|
642
|
+
translator?.clearCache();
|
|
643
|
+
} catch {
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
getCacheStats: () => {
|
|
647
|
+
try {
|
|
648
|
+
const debugInfo = translator?.debug();
|
|
649
|
+
if (debugInfo && debugInfo.cacheStats) {
|
|
650
|
+
return {
|
|
651
|
+
size: debugInfo.cacheSize || 0,
|
|
652
|
+
hits: debugInfo.cacheStats.hits || 0,
|
|
653
|
+
misses: debugInfo.cacheStats.misses || 0
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
return { size: 0, hits: 0, misses: 0 };
|
|
657
|
+
} catch (error2) {
|
|
658
|
+
return { size: 0, hits: 0, misses: 0 };
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
reloadTranslations: async () => {
|
|
662
|
+
if (translator) {
|
|
663
|
+
setIsLoading(true);
|
|
664
|
+
setError(null);
|
|
665
|
+
try {
|
|
666
|
+
await translator.initialize();
|
|
667
|
+
} catch (err) {
|
|
668
|
+
setError(err);
|
|
669
|
+
} finally {
|
|
670
|
+
setIsLoading(false);
|
|
671
|
+
}
|
|
587
672
|
}
|
|
588
673
|
}
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
674
|
+
}),
|
|
675
|
+
[
|
|
676
|
+
translator,
|
|
677
|
+
currentLanguage,
|
|
678
|
+
error,
|
|
679
|
+
isInitialized,
|
|
680
|
+
config.supportedLanguages
|
|
681
|
+
]
|
|
682
|
+
);
|
|
683
|
+
const value = useMemo(
|
|
684
|
+
() => ({
|
|
685
|
+
currentLanguage,
|
|
686
|
+
setLanguage,
|
|
687
|
+
t,
|
|
688
|
+
tPlural,
|
|
689
|
+
tArray,
|
|
690
|
+
tAsync,
|
|
691
|
+
tSync,
|
|
692
|
+
getRawValue,
|
|
693
|
+
isLoading,
|
|
694
|
+
error,
|
|
695
|
+
supportedLanguages: config.supportedLanguages,
|
|
696
|
+
debug,
|
|
697
|
+
isInitialized
|
|
698
|
+
}),
|
|
699
|
+
[
|
|
700
|
+
currentLanguage,
|
|
701
|
+
setLanguage,
|
|
702
|
+
t,
|
|
703
|
+
tPlural,
|
|
704
|
+
tArray,
|
|
705
|
+
tAsync,
|
|
706
|
+
tSync,
|
|
707
|
+
getRawValue,
|
|
708
|
+
isLoading,
|
|
709
|
+
error,
|
|
710
|
+
config.supportedLanguages,
|
|
711
|
+
debug,
|
|
712
|
+
isInitialized
|
|
713
|
+
]
|
|
714
|
+
);
|
|
606
715
|
return /* @__PURE__ */ jsx(I18nContext.Provider, { value, children });
|
|
607
716
|
}
|
|
608
717
|
function useI18n() {
|
|
@@ -612,7 +721,12 @@ function useI18n() {
|
|
|
612
721
|
currentLanguage: "ko",
|
|
613
722
|
setLanguage: () => {
|
|
614
723
|
},
|
|
615
|
-
t: (key) =>
|
|
724
|
+
t: (key, paramsOrLang) => {
|
|
725
|
+
if (typeof paramsOrLang === "object" && paramsOrLang !== null && "defaultValue" in paramsOrLang && typeof paramsOrLang.defaultValue === "string") {
|
|
726
|
+
return paramsOrLang.defaultValue;
|
|
727
|
+
}
|
|
728
|
+
return key;
|
|
729
|
+
},
|
|
616
730
|
tPlural: (key) => key,
|
|
617
731
|
tAsync: async (key) => key,
|
|
618
732
|
tSync: (key) => key,
|