@svelstack/translator 0.9.5 → 0.9.6
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/index.js +31 -3
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -310,11 +310,21 @@ var Translator = class {
|
|
|
310
310
|
* @type {Record<string, Record<string, string>> | undefined}
|
|
311
311
|
*/
|
|
312
312
|
translations = void 0;
|
|
313
|
+
/**
|
|
314
|
+
* @private
|
|
315
|
+
* @type {Record<string, Record<string, string>> | undefined}
|
|
316
|
+
*/
|
|
317
|
+
fallbackTranslations = void 0;
|
|
313
318
|
/**
|
|
314
319
|
* @private
|
|
315
320
|
* @type {Promise<any> | undefined}
|
|
316
321
|
*/
|
|
317
322
|
_promise = void 0;
|
|
323
|
+
/**
|
|
324
|
+
* @private
|
|
325
|
+
* @type {Promise<any> | undefined}
|
|
326
|
+
*/
|
|
327
|
+
_fallbackPromise = void 0;
|
|
318
328
|
/**
|
|
319
329
|
* @private
|
|
320
330
|
* @type {string}
|
|
@@ -351,6 +361,9 @@ var Translator = class {
|
|
|
351
361
|
]);
|
|
352
362
|
}
|
|
353
363
|
this.load(this._language);
|
|
364
|
+
if (this._language !== this.options.fallbackLanguage) {
|
|
365
|
+
this.loadFallback();
|
|
366
|
+
}
|
|
354
367
|
}
|
|
355
368
|
/**
|
|
356
369
|
* Changes the current language and reloads translations.
|
|
@@ -377,7 +390,7 @@ var Translator = class {
|
|
|
377
390
|
* @returns {Promise<void>} Resolves when loading is complete.
|
|
378
391
|
*/
|
|
379
392
|
async wait() {
|
|
380
|
-
await this._promise;
|
|
393
|
+
await Promise.all([this._promise, this._fallbackPromise]);
|
|
381
394
|
}
|
|
382
395
|
/**
|
|
383
396
|
* Translates a key within a given domain using optional parameters.
|
|
@@ -387,11 +400,11 @@ var Translator = class {
|
|
|
387
400
|
* @returns {string} The translated string, or a fallback if not found.
|
|
388
401
|
*/
|
|
389
402
|
trans(domain, key, parameters) {
|
|
390
|
-
var _a;
|
|
403
|
+
var _a, _b, _c;
|
|
391
404
|
if (this.translations === void 0) {
|
|
392
405
|
return "";
|
|
393
406
|
}
|
|
394
|
-
const translation = (_a = this.translations[domain]) == null ? void 0 : _a[key];
|
|
407
|
+
const translation = ((_a = this.translations[domain]) == null ? void 0 : _a[key]) ?? ((_c = (_b = this.fallbackTranslations) == null ? void 0 : _b[domain]) == null ? void 0 : _c[key]);
|
|
395
408
|
if (translation === void 0) {
|
|
396
409
|
return `${domain}.${key}`;
|
|
397
410
|
}
|
|
@@ -422,6 +435,21 @@ var Translator = class {
|
|
|
422
435
|
this.translations = dictionary;
|
|
423
436
|
}
|
|
424
437
|
}
|
|
438
|
+
/**
|
|
439
|
+
* Loads fallback translations.
|
|
440
|
+
* @private
|
|
441
|
+
* @returns {Promise<void>} Resolves when the fallback translations are loaded.
|
|
442
|
+
*/
|
|
443
|
+
async loadFallback() {
|
|
444
|
+
const fallbackDictionary = this.options.dictionaries[this.options.fallbackLanguage];
|
|
445
|
+
if (typeof fallbackDictionary === "function") {
|
|
446
|
+
const promise = this._fallbackPromise = fallbackDictionary();
|
|
447
|
+
this.fallbackTranslations = await promise;
|
|
448
|
+
this._fallbackPromise = void 0;
|
|
449
|
+
} else {
|
|
450
|
+
this.fallbackTranslations = fallbackDictionary;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
425
453
|
/**
|
|
426
454
|
* Determines the appropriate language to use.
|
|
427
455
|
* @private
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svelstack/translator",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.6",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"build": "tsup && npm run prepack",
|
|
6
6
|
"prepack": "publint",
|
|
7
7
|
"check": "tsc --noEmit",
|
|
8
|
-
"test": "vitest"
|
|
8
|
+
"test": "vitest run",
|
|
9
|
+
"test:watch": "vitest"
|
|
9
10
|
},
|
|
10
11
|
"type": "module",
|
|
11
12
|
"main": "./dist/index.js",
|