UNPKG

@omnia/tooling-composers

Version:

Provide tooling to work with manifest things.

113 lines (112 loc) 4.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalizationNamespaceComposer = exports.Localization = exports.getLocalizationPath = exports.getLocalizations = void 0; const tslib_1 = require("tslib"); const Utils_1 = require("./Utils"); const ManifestRegistry_1 = require("./ManifestRegistry"); const deep_extend_1 = tslib_1.__importDefault(require("deep-extend")); const models_1 = require("../../fx-models"); var localizations = {}; function getLocalizations() { return localizations; } exports.getLocalizations = getLocalizations; function getLocalizationPath(exp, ns) { const propertyPaths = Utils_1.Utils.getPropertyPathInExpression(exp); let result = ns && ns.length > 0 ? ns + "." + propertyPaths[0] : propertyPaths[0]; result = `$Localize:${result};`; return result; } exports.getLocalizationPath = getLocalizationPath; class Localization { constructor(localizationOptions, manifestId) { this.namespace = (name) => { this.localizationNamespaceComposer.setCurrentNamespace(name); return { add: this.localizationNamespaceComposer.add }; }; this.manifestId = manifestId; this.localizationOptions = localizationOptions; if (Localization.localizationRegistrations.indexOf(ManifestRegistry_1.ManifestRegistry.getCurrentManifestPath()) === -1) { Localization.localizationRegistrations.push(ManifestRegistry_1.ManifestRegistry.getCurrentManifestPath()); } if (!this.localizationOptions) { this.localizationOptions = { localeName: models_1.LocaleNames.EnUs }; } let localeName = this.localizationOptions.localeName; this.localizeFileNameFormat = `${manifestId}.loc.${localeName}.json`; if (localizations[localeName] === undefined) localizations[localeName] = {}; //if (localizations[localeName]["Public"] === undefined) // localizations[localeName]["Public"] = {}; //if (localizations[this.localizeFileNameFormat] === undefined) // localizations[this.localizeFileNameFormat] = {}; this.localizationNamespaceComposer = new LocalizationNamespaceComposer(localizations[this.localizationOptions.localeName], this.manifestId); } } exports.Localization = Localization; Localization.localizationRegistrations = []; Localization.getRegistrations = () => { return Localization.localizationRegistrations; }; Localization.clearRegistrations = () => { Localization.localizationRegistrations = []; }; Localization.getClientManifests = (currentServiceId) => { let result = []; return result; }; Localization.clearState = () => { localizations = {}; }; Localization.Subscription = ManifestRegistry_1.ManifestRegistry.registerClientManifestProvider(new ManifestRegistry_1.StaticClientManifestProviderProxy(Localization.getClientManifests, Localization.clearState)); class LocalizationNamespaceComposer { constructor(root, manifestId) { this.add = (localizeObject) => { var currentNamespaceObject = this.getCurrentNamespaceObject(this.currentNamespace); if (localizeObject) { currentNamespaceObject = (0, deep_extend_1.default)(currentNamespaceObject, localizeObject); //Object.keys(localizeObject).forEach((key) => { // if (currentNamespaceObject[key] !== undefined) { // throw new Error(`The manifest ${this.manifestId} already exist key '${key}' localize in namspace ${this.currentNamespace}`); // } // currentNamespaceObject[key] = localizeObject[key]; //}); } return this; }; this.namespace = (name) => { this.currentNamespace = name; return { add: this.add }; }; this.setCurrentNamespace = (name) => { this.currentNamespace = name; }; this.getCurrentNamespaceObject = (namespace) => { if (Utils_1.Utils.isNullOrEmpty(namespace)) { return this.rootNamespaceObject; } var segments = namespace.split("."); var result = null; var tempLocalize = this.rootNamespaceObject; for (var i = 0; i < segments.length; i++) { if (tempLocalize[segments[i]] === undefined) { tempLocalize[segments[i]] = {}; } tempLocalize = tempLocalize[segments[i]]; if (i === segments.length - 1) { result = tempLocalize; } } return result; }; this.manifestId = manifestId; this.rootNamespaceObject = root; } } exports.LocalizationNamespaceComposer = LocalizationNamespaceComposer;