@itwin/core-i18n 5.14.0-dev.7 → 5.14.0-dev.9

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.
@@ -22480,23 +22480,9 @@ class ITwinLocalization {
22480
22480
  this.i18next.loadNamespaces(name, (err) => {
22481
22481
  if (!err)
22482
22482
  return resolve();
22483
- // Here we got a non-null err object.
22484
- // This method is called when the system has attempted to load the resources for the namespaces for each possible locale.
22485
- // For example 'fr-ca' might be the most specific locale, in which case 'fr' and 'en' are fallback locales.
22486
- // Using Backend from i18next-http-backend, err will be an array of strings of each namespace it tried to read and its locale.
22487
- // There might be errs for some other namespaces as well as this one. We resolve the promise unless there's an error for each possible locale.
22488
- let locales = this.getLanguageList().map((thisLocale) => `/${thisLocale}/`);
22489
- try {
22490
- for (const thisError of err) {
22491
- if (typeof thisError === "string")
22492
- locales = locales.filter((thisLocale) => !thisError.includes(thisLocale));
22493
- }
22494
- }
22495
- catch {
22496
- locales = [];
22497
- }
22498
- // if we removed every locale from the array, it wasn't loaded.
22499
- if (locales.length === 0)
22483
+ // i18next can return errors from other concurrent namespace loads in this callback.
22484
+ const wasLoaded = this.getLanguageList().some((language) => this.i18next.hasResourceBundle(language, name));
22485
+ if (!wasLoaded)
22500
22486
  core_bentley_1.Logger.logError("i18n", `No resources for namespace ${name} could be loaded`);
22501
22487
  resolve();
22502
22488
  });
@@ -23636,6 +23622,7 @@ let exports = __webpack_exports__;
23636
23622
  *--------------------------------------------------------------------------------------------*/
23637
23623
  Object.defineProperty(exports, "__esModule", ({ value: true }));
23638
23624
  const chai_1 = __webpack_require__(/*! chai */ "../../common/temp/node_modules/.pnpm/chai@4.5.0/node_modules/chai/index.js");
23625
+ const core_bentley_1 = __webpack_require__(/*! @itwin/core-bentley */ "../bentley/lib/esm/core-bentley.js");
23639
23626
  const ITwinLocalization_1 = __webpack_require__(/*! ../ITwinLocalization */ "./lib/cjs/ITwinLocalization.js");
23640
23627
  describe("ITwinLocalization", () => {
23641
23628
  let localization;
@@ -24502,6 +24489,65 @@ describe("ITwinLocalization", () => {
24502
24489
  chai_1.assert.isTrue(itwinLocalization.i18next.hasLoadedNamespace("Test"));
24503
24490
  chai_1.assert.equal(itwinLocalization.getLocalizedString("Test:FirstTrivial"), "First level string (test)");
24504
24491
  });
24492
+ it("attributes errors from concurrent namespace loads to the namespace that failed", async () => {
24493
+ const pendingLoads = new Map();
24494
+ const backend = {
24495
+ type: "backend",
24496
+ init: () => { },
24497
+ read: (_language, namespace, callback) => pendingLoads.set(namespace, callback),
24498
+ };
24499
+ itwinLocalization = new ITwinLocalization_1.ITwinLocalization({
24500
+ backendPlugin: backend,
24501
+ initOptions: { lng: "en", fallbackLng: false },
24502
+ });
24503
+ await itwinLocalization.initialize([]);
24504
+ const loggedErrors = [];
24505
+ // eslint-disable-next-line @typescript-eslint/unbound-method -- Preserve the exact method reference so the test can restore it.
24506
+ const originalLogError = core_bentley_1.Logger.logError;
24507
+ core_bentley_1.Logger.logError = (_category, message) => loggedErrors.push(String(message));
24508
+ try {
24509
+ const loadedPromise = itwinLocalization.registerNamespace("Loaded");
24510
+ const missingPromise = itwinLocalization.registerNamespace("Missing");
24511
+ chai_1.assert.sameMembers([...pendingLoads.keys()], ["Loaded", "Missing"]);
24512
+ pendingLoads.get("Missing")?.(new Error("Missing namespace"), false);
24513
+ pendingLoads.get("Loaded")?.(null, { key: "value" });
24514
+ await Promise.all([missingPromise, loadedPromise]);
24515
+ chai_1.assert.deepEqual(loggedErrors, ["No resources for namespace Missing could be loaded"]);
24516
+ chai_1.assert.isTrue(itwinLocalization.i18next.hasResourceBundle("en", "Loaded"));
24517
+ }
24518
+ finally {
24519
+ core_bentley_1.Logger.logError = originalLogError;
24520
+ }
24521
+ });
24522
+ it("does not log an error when a namespace loads from a fallback language", async () => {
24523
+ const pendingLoads = new Map();
24524
+ const backend = {
24525
+ type: "backend",
24526
+ init: () => { },
24527
+ read: (language, namespace, callback) => pendingLoads.set(`${language}/${namespace}`, callback),
24528
+ };
24529
+ itwinLocalization = new ITwinLocalization_1.ITwinLocalization({
24530
+ backendPlugin: backend,
24531
+ initOptions: { lng: "fr", fallbackLng: "en" },
24532
+ });
24533
+ await itwinLocalization.initialize([]);
24534
+ const loggedErrors = [];
24535
+ // eslint-disable-next-line @typescript-eslint/unbound-method -- Preserve the exact method reference so the test can restore it.
24536
+ const originalLogError = core_bentley_1.Logger.logError;
24537
+ core_bentley_1.Logger.logError = (_category, message) => loggedErrors.push(String(message));
24538
+ try {
24539
+ const loadPromise = itwinLocalization.registerNamespace("Fallback");
24540
+ chai_1.assert.sameMembers([...pendingLoads.keys()], ["fr/Fallback", "en/Fallback"]);
24541
+ pendingLoads.get("fr/Fallback")?.(new Error("Missing French namespace"), false);
24542
+ pendingLoads.get("en/Fallback")?.(null, { key: "value" });
24543
+ await loadPromise;
24544
+ chai_1.assert.isEmpty(loggedErrors);
24545
+ chai_1.assert.isTrue(itwinLocalization.i18next.hasResourceBundle("en", "Fallback"));
24546
+ }
24547
+ finally {
24548
+ core_bentley_1.Logger.logError = originalLogError;
24549
+ }
24550
+ });
24505
24551
  });
24506
24552
  // unregisterNamespace() isn't used and basically does nothing
24507
24553
  // describe("#unregisterNamespace", () => {