elmer-ui-core
Version:
web app framework
73 lines (72 loc) • 3.52 kB
JavaScript
;
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.I18nController = void 0;
var elmer_common_1 = require("elmer-common");
var globalUtil_1 = require("../init/globalUtil");
var I18nController = (function (_super) {
__extends(I18nController, _super);
function I18nController() {
var _this = _super.call(this) || this;
_this.initI18n({ prototype: _this }, {});
return _this;
}
I18nController.prototype.initI18n = function (ComponentClass, configData) {
this.initI18nConfig(ComponentClass.prototype, configData);
};
I18nController.prototype.initI18nTranslate = function (targetComponent) {
this.defineReadOnlyProperty(targetComponent, "translate", this.translate.bind(targetComponent));
};
I18nController.prototype.initI18nConfig = function (ComponentPrototype, configData) {
var dataKey = this.getValue(configData, "key", "");
var localeData = this.getLocaleData(configData ? configData.locale : null);
var locale = localeData.locale;
var rootKey = this.isEmpty(dataKey) ? "i18n.data." + locale : "i18n.data." + locale + "." + dataKey;
this.defineReadOnlyProperty(ComponentPrototype, "i18nLocale", locale);
this.defineReadOnlyProperty(ComponentPrototype, "i18nRegion", localeData.region);
this.defineReadOnlyProperty(ComponentPrototype, "i18nRootKey", rootKey);
if (configData && configData.data) {
this.defineReadOnlyProperty(ComponentPrototype, "i18nData", configData.data);
}
locale = null;
rootKey = null;
dataKey = null;
localeData = null;
};
I18nController.prototype.getLocaleData = function (overrideLocale) {
var locale = !this.isEmpty(overrideLocale) ? overrideLocale : (localStorage && this.isFunction(localStorage.getItem) ? localStorage.getItem("i18n_locale") || "" : "");
var lArr = (locale || "").split("-");
return {
locale: lArr && lArr.length > 0 ? lArr[0] : "zh",
region: lArr && lArr.length > 1 ? lArr[1] : "CN"
};
};
I18nController.prototype.translate = function (key) {
var privateKey = "i18nData." + this["i18nLocale"] + "." + key;
var privateResult = this.getValue(this, privateKey);
if (!this.isEmpty(privateResult)) {
return privateResult;
}
else {
var defineKey = this.getValue(this, "i18nRootKey") + "." + key;
return this.getValue(elmerData.elmerState, defineKey, "{{" + defineKey + "}}");
}
};
return I18nController;
}(elmer_common_1.Common));
exports.I18nController = I18nController;
globalUtil_1.addToClassPool("I18nController", I18nController);