UNPKG

@nu-art/thunder

Version:

Thunder - React & Typescript based frontend framework

88 lines 3.66 kB
"use strict"; /* * Thunder is a typescript & react frontend foundation that natively * runs on firebase function and is a part of the Thunderstorm larger project * * Copyright (C) 2018 Adam van der Kruk aka TacB0sS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var ts_common_1 = require("@nu-art/ts-common"); var util_1 = require("util"); var LocalizationModule_Class = /** @class */ (function (_super) { __extends(LocalizationModule_Class, _super); function LocalizationModule_Class() { return _super !== null && _super.apply(this, arguments) || this; } LocalizationModule_Class.prototype.init = function () { var defaultLocale = this.config.defaultLocale; if (!defaultLocale) throw Error("MUST set defaultLocale in the config data"); for (var key in this.config.locales) { this.config.locales[key].locale = key; } this.setLanguage(defaultLocale); }; LocalizationModule_Class.prototype.setLanguage = function (locale) { var localeDef = this.config.locales[locale]; if (!localeDef) throw new Error("Unsupported language: " + locale); this.activeLocale = locale; }; LocalizationModule_Class.prototype.getAvailableLanguages = function () { var ret = []; for (var key in this.config.locales) { ret.push(this.config.locales[key]); } return ret; }; LocalizationModule_Class.prototype.getActiveLocale = function () { return this.activeLocale; }; LocalizationModule_Class.prototype.getString = function (key) { var params = []; for (var _i = 1; _i < arguments.length; _i++) { params[_i - 1] = arguments[_i]; } var text = this.getStringFromLocale(this.activeLocale, key); if (!text) text = this.getStringFromLocale(this.config.defaultLocale, key); if (!text) return key; return util_1.format(text, params); }; LocalizationModule_Class.prototype.getStringFromLocale = function (locale, key) { var languageData = this.config.languages.get(this.activeLocale); if (!languageData) return; return languageData.get(key); }; return LocalizationModule_Class; }(ts_common_1.Module)); exports.LocalizationModule_Class = LocalizationModule_Class; exports.LocalizationModule = new LocalizationModule_Class(); //# sourceMappingURL=localization-module.js.map