/ node_modules / @jet / engine / lib / dependencies / localized-strings-bundle.js
localized-strings-bundle.js
 1  "use strict";
 2  Object.defineProperty(exports, "__esModule", { value: true });
 3  exports.LocalizedStringsBundle = void 0;
 4  const environment_1 = require("@jet/environment");
 5  const localized_strings_json_object_1 = require("./localized-strings-json-object");
 6  /**
 7   * A localized string data source which loads strings from the application bundle.
 8   *
 9   * The bundle used by this data source can be a web app webpack bundle
10   * or a native app bundle bridged over to JS code.
11   */
12  class LocalizedStringsBundle {
13      // MARK: - Initialization
14      /**
15       * Create localized strings bundle with all required attributes.
16       *
17       * @param bundle - The app bundle object.
18       */
19      constructor(bundle) {
20          this.bundle = bundle;
21      }
22      // MARK: - LocalizedStringsDataSource
23      async fetchStrings(language) {
24          var _a;
25          // Load the strings from bundle and cache them.
26          const localizations = this.bundle.localizationsProperty;
27          if (environment_1.isNothing(localizations)) {
28              throw new Error("Localized strings bundle index file is missing 'localizations' property");
29          }
30          let strings;
31          const format = (_a = localizations.format) !== null && _a !== void 0 ? _a : "json/inline" /* jsonInline */;
32          if (format === "json/inline" /* jsonInline */) {
33              const inlineLocalizations = localizations;
34              strings = inlineLocalizations[language];
35          }
36          else {
37              const externalLocalizations = localizations;
38              switch (externalLocalizations.format) {
39                  case "json/multi-file" /* jsonMultiFile */:
40                      {
41                          // The path points to directory where JSON files are located.
42                          // We don't even have to list a directory, just construct a final path.
43                          // The path is also not an OS path but a bundle (e.g. JetPack) path.
44                          // Bundle APIs always use "/" in the path, same as the paths used in the
45                          // index.json (manifest) files.
46                          const jsonPath = `${externalLocalizations.path}/${language}.json`;
47                          strings = (await this.bundle.loadResource(jsonPath));
48                      }
49                      break;
50                  case "json/single-file" /* jsonSingleFile */:
51                      // The bundle contains single JSON file with all strings dictionary in it.
52                      strings = (await this.bundle.loadResource(externalLocalizations.path))[language];
53                      break;
54                  case "loctable" /* loctable */:
55                      throw new Error("Loctable format not supported in JS implementation");
56                  case "js" /* js */:
57                      throw new Error("Not yet implemented");
58                  default:
59                      throw new Error(`Unknown localization format: ${JSON.stringify(format)}`);
60              }
61          }
62          if (environment_1.isNothing(strings)) {
63              throw new Error(`Missing strings for ${language}`);
64          }
65          return new localized_strings_json_object_1.LocalizedStringsJSONObject(strings);
66      }
67  }
68  exports.LocalizedStringsBundle = LocalizedStringsBundle;