UNPKG

@phensley/cldr-core

Version:
86 lines 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var encoding_1 = require("./encoding"); var locale_1 = require("../locale"); var bundle_1 = require("./bundle"); var DELIMITER = '\t'; /** * Layer in the pack that supports all regions for a single language + script. */ var PackScript = /** @class */ (function () { function PackScript(strings, exceptions, regions, defaultRegion) { this._cache = {}; this._strings = strings.split(DELIMITER); this._exceptions = exceptions.split(DELIMITER); this._regions = regions; this._defaultRegion = defaultRegion; } PackScript.prototype.get = function (tag) { var region = tag.region(); var index = this._cache[region] || this.decode(region); if (index === undefined) { region = this._defaultRegion; tag = new locale_1.LanguageTag(tag.language(), tag.script(), region, tag.variant(), tag.extensions(), tag.privateUse()); index = this._cache[region] || this.decode(region); } return new bundle_1.StringBundle(tag.compact(), tag, this._strings, this._exceptions, index); }; PackScript.prototype.decode = function (region) { var raw = this._regions[region]; if (raw === undefined) { return undefined; } var decoded = raw.split(' ').map(encoding_1.base100decode); var index = {}; for (var i = 0; i < decoded.length; i += 2) { var k = decoded[i]; var v = decoded[i + 1]; index[k] = v; } this._cache[region] = index; return index; }; return PackScript; }()); exports.PackScript = PackScript; /** * Runtime resource pack manager. * * @alpha */ var Pack = /** @class */ (function () { function Pack(data) { var _this = this; this.scripts = {}; var raw = typeof data === 'string' ? JSON.parse(data) : data; var version = raw.version, cldr = raw.cldr, language = raw.language; if (version === undefined) { throw new Error('Severe error: data does not look like a valid resource pack.'); } this.version = version; this.cldrVersion = cldr; this.language = language; this.defaultTag = locale_1.LanguageResolver.resolve(raw.default); Object.keys(raw.scripts).forEach(function (k) { var obj = raw.scripts[k]; _this.scripts[k] = new PackScript(obj.strings, obj.exceptions, obj.regions, obj.default); }); } Pack.prototype.get = function (tag) { // We need the script and region to find the correct string layer. Caller should // ideally supply a resolved language tag to avoid the overhead of this call. if (!tag.hasLanguage() || !tag.hasScript() || !tag.hasRegion()) { tag = locale_1.LanguageResolver.resolve(tag); } // Strings for a language are organized by script. var script = this.scripts[tag.script()]; if (script === undefined) { script = this.scripts[this.defaultTag.script()]; return script.get(this.defaultTag); } return script.get(tag); }; return Pack; }()); exports.Pack = Pack; //# sourceMappingURL=pack.js.map