UNPKG

@jargon/platform-sdk-core

Version:

Core components of the Jargon Platform SDK for node.js

102 lines 4.28 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeLocale = exports.JargonResourceManagerFactory = void 0; const path_1 = require("path"); const _1 = require("."); const defs_1 = require("./defs"); const fs = __importStar(require("./fsPromises")); const jrm_1 = require("./jrm"); class JargonResourceManagerFactory { constructor(options) { this._intialized = false; this._localeData = new Map(); this._fallbacks = {}; this._opts = Object.assign({}, _1.DefaultResourceManagerOptions, options); this._opts.localesToPreload = this._opts.localesToPreload.map(normalizeLocale); this._initPromise = this._doInit(); } async _doInit() { // Load manifest const manifestPath = path_1.join(this._opts.resourceDirectory, defs_1.MANIFEST_FILE_NAME); const data = await fs.readFile(manifestPath, { encoding: 'utf8' }); const manifest = JSON.parse(data); if (!defs_1.isReleaseManifest(manifest)) { throw new Error(`${manifestPath} does not appear to be a valid Jargon manifest file`); } if (manifest.manifestFormat !== 1) { throw new Error(`unsupported manifest file format ${manifest.manifestFormat}`); } if (manifest.packageFormat !== 1) { throw new Error(`unsupported package format ${manifest.packageFormat}`); } if (manifest.locales.length === 0) { throw new Error('manifest must specify at least one locale'); } // Local locale data, per the manifest manifest.locales.forEach(l => { const localeDataFile = path_1.join(this._opts.resourceDirectory, `${l}.json`); const nl = normalizeLocale(l); const ld = new jrm_1.LocaleData(nl, localeDataFile); this._localeData.set(nl, ld); if (this._opts.localesToPreload.includes(nl)) { ld.loadContent(); } }); // Use fallbacks from options if any were provided, otherwise use // what was defined in the manifest let fls = manifest.fallbackLocales || {}; if (Object.keys(this._opts.fallbackLocales).length > 0) { fls = this._opts.fallbackLocales; } for (const l in fls) { this._fallbacks[normalizeLocale(l)] = fls[l].map(normalizeLocale); } this._intialized = true; } async forLocaleAndPlatform(locale, platform) { await this._initPromise; locale = normalizeLocale(locale); const fallbacks = []; const fbls = this._fallbacks[locale] || []; for (const fbl of fbls) { fallbacks.push(this._makeResourceManager(fbl, platform, [])); } return this._makeResourceManager(locale, platform, fallbacks); } _makeResourceManager(locale, platform, fallbacks) { const ld = this._localeData.get(locale); if (!ld) { throw new Error(`No data present for ${locale}`); } ld.loadContent(); return new jrm_1.JargonResourceManager(this._opts, platform, ld, fallbacks); } } exports.JargonResourceManagerFactory = JargonResourceManagerFactory; function normalizeLocale(l) { if (l.length === 5) { l = l.slice(0, 3) + l.slice(3, 5).toLocaleUpperCase(); } return l; } exports.normalizeLocale = normalizeLocale; //# sourceMappingURL=factory.js.map