123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- var Messages = (function () {
-
- function Messages(msgData, defaultLocale) {
- var _this = this;
-
- this._data = {};
-
- this._fallback = {};
-
- this._defaultLocale = null;
-
- this._locale = null;
- Object.keys(msgData).forEach(function (lc) {
- if (lc !== 'toString') {
- _this._data[lc] = msgData[lc];
- if (defaultLocale === undefined)
- defaultLocale = lc;
- }
- });
- this.locale = defaultLocale || null;
- this._defaultLocale = this.locale;
- }
- Object.defineProperty(Messages.prototype, "availableLocales", {
-
- get: function () {
- return Object.keys(this._data);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Messages.prototype, "locale", {
-
- get: function () {
- return this._locale;
- },
- set: function (locale) {
- this._locale = this.resolveLocale(locale);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Messages.prototype, "defaultLocale", {
-
- get: function () {
- return this._defaultLocale;
- },
- set: function (locale) {
- this._defaultLocale = this.resolveLocale(locale);
- },
- enumerable: false,
- configurable: true
- });
-
- Messages.prototype.addMessages = function (data, locale, keypath) {
- var lc = locale || String(this.locale);
- if (typeof data !== 'function') {
- data = Object.keys(data).reduce(function (map, key) {
- if (key !== 'toString')
- map[key] = data[key];
- return map;
- }, {});
- }
- if (Array.isArray(keypath) && keypath.length > 0) {
- var parent_1 = this._data[lc];
- for (var i = 0; i < keypath.length - 1; ++i) {
- var key = keypath[i];
- if (!parent_1[key])
- parent_1[key] = {};
- parent_1 = parent_1[key];
- }
- parent_1[keypath[keypath.length - 1]] = data;
- }
- else {
- this._data[lc] = data;
- }
- return this;
- };
-
- Messages.prototype.resolveLocale = function (locale) {
- var lc = String(locale);
- if (this._data[lc])
- return locale;
- if (locale) {
- while ((lc = lc.replace(/[-_]?[^-_]*$/, ''))) {
- if (this._data[lc])
- return lc;
- }
- var ll = this.availableLocales;
- var re = new RegExp('^' + locale + '[-_]');
- for (var i = 0; i < ll.length; ++i) {
- if (re.test(ll[i]))
- return ll[i];
- }
- }
- return null;
- };
-
- Messages.prototype.getFallback = function (locale) {
- var lc = locale || String(this.locale);
- return (this._fallback[lc] ||
- (lc === this.defaultLocale || !this.defaultLocale
- ? []
- : [this.defaultLocale]));
- };
-
- Messages.prototype.setFallback = function (lc, fallback) {
- this._fallback[lc] = Array.isArray(fallback) ? fallback : null;
- return this;
- };
-
- Messages.prototype.hasMessage = function (key, locale, fallback) {
- var lc = locale || String(this.locale);
- var fb = fallback ? this.getFallback(lc) : null;
- return _has(this._data, lc, key, fb, 'function');
- };
-
- Messages.prototype.hasObject = function (key, locale, fallback) {
- var lc = locale || String(this.locale);
- var fb = fallback ? this.getFallback(lc) : null;
- return _has(this._data, lc, key, fb, 'object');
- };
-
- Messages.prototype.get = function (key, props, locale) {
- var lc = locale || String(this.locale);
- var msg = _get(this._data[lc], key);
- if (msg)
- return typeof msg == 'function' ? msg(props) : msg;
- var fb = this.getFallback(lc);
- for (var i = 0; i < fb.length; ++i) {
- msg = _get(this._data[fb[i]], key);
- if (msg)
- return typeof msg == 'function' ? msg(props) : msg;
- }
- return key;
- };
- return Messages;
- }());
- exports.default = Messages;
- function _get(obj, key) {
- if (!obj)
- return null;
- var res = obj;
- if (Array.isArray(key)) {
- for (var i = 0; i < key.length; ++i) {
- if (typeof res !== 'object')
- return null;
- res = res[key[i]];
- if (!res)
- return null;
- }
- return res;
- }
- return typeof res === 'object' ? res[key] : null;
- }
- function _has(data, lc, key, fallback, type) {
- var msg = _get(data[lc], key);
- if (msg)
- return typeof msg === type;
- if (fallback) {
- for (var i = 0; i < fallback.length; ++i) {
- msg = _get(data[fallback[i]], key);
- if (msg)
- return typeof msg === type;
- }
- }
- return false;
- }
|