UNPKG

@senspark/ee

Version:

utility library for cocos creator

163 lines (162 loc) 6.18 kB
"use strict"; 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 Polyglot = require("node-polyglot"); var ObserverManager_1 = require("./ObserverManager"); var ProfileManager_1 = require("./ProfileManager"); var LanguageManager = /** @class */ (function (_super) { __extends(LanguageManager, _super); function LanguageManager() { var _this = _super.call(this) || this; // Hide construction. _this.configs = {}; _this.polyglot = new Polyglot(); var profile = ProfileManager_1.ProfileManager.getInstance(); _this.currentLanguage = profile.loadData('current_language'); _this.configDir = profile.loadData('config_dir'); _this.updateConfigs(); return _this; } /** Gets the singleton. */ LanguageManager.getInstance = function () { return this.sharedInstance || (this.sharedInstance = new this()); }; /** * Gets all available languages. * @return Array of language names. */ LanguageManager.prototype.getLanguages = function () { return Object.keys(this.configs); }; /** * Gets the config dir. * Used in editor only. */ LanguageManager.prototype.getConfigDir = function () { return this.configDir; }; /** * Sets the config directory. * @param path The directory path. */ LanguageManager.prototype.setConfigDir = function (path) { this.configDir = path; if (CC_EDITOR) { var profile = ProfileManager_1.ProfileManager.getInstance(); profile.saveData('config_dir', this.configDir); } this.updateConfigs(); this.updateLanguage(); }; /** Clears the language directory. */ LanguageManager.prototype.resetConfigDir = function () { this.configDir = undefined; if (CC_EDITOR) { var profile = ProfileManager_1.ProfileManager.getInstance(); profile.saveData('config_dir', this.configDir); } this.updateConfigs(); }; LanguageManager.prototype.updateConfigs = function () { var _this = this; this.configs = {}; if (this.configDir === undefined) { this.updateLanguage(); return; } if (CC_EDITOR) { Editor.assetdb.queryUrlByUuid(this.configDir, function (err, url) { var pattern = url + '/*.json'; var type = (cc.ENGINE_VERSION >= '2' ? 'json' : 'text'); Editor.assetdb.queryAssets(pattern, type, function (err2, result) { result.forEach(function (item) { Promise.resolve().then(function () { return require(item.path); }).then(function (config) { var language = _this.parseLanguage(item.path); _this.configs[language] = config; _this.updateLanguage(); }); }); }); }); } else { cc.loader.loadResDir(this.configDir, function (err, results, urls) { for (var i = 0; i < urls.length; ++i) { var path = urls[i] + '.json'; // Suffixed with .json for regex matching. var content = results[i]; var language = _this.parseLanguage(path); if (cc.ENGINE_VERSION >= '2') { _this.configs[language] = content.json; } else { _this.configs[language] = content; } _this.updateLanguage(); } }); } }; LanguageManager.prototype.updateLanguage = function () { this.polyglot.clear(); if (this.currentLanguage !== undefined) { var config = this.configs[this.currentLanguage]; if (config !== undefined) { this.polyglot.extend(config); } } this.dispatch(function (observer) { return observer(); }); }; /** Gets the active language. */ LanguageManager.prototype.getCurrentLanguage = function () { return this.currentLanguage; }; /** Sets the active language. */ LanguageManager.prototype.setCurrentLanguage = function (language) { this.currentLanguage = language; if (CC_EDITOR) { var profile = ProfileManager_1.ProfileManager.getInstance(); profile.saveData('current_language', language); } this.updateLanguage(); }; /** Gets the language format in the current language. */ LanguageManager.prototype.getFormat = function (key) { if (this.currentLanguage === undefined) { return undefined; } var config = this.configs[this.currentLanguage]; if (config === undefined) { return undefined; } return config[key]; }; LanguageManager.prototype.parseFormat = function (key, options) { if (this.polyglot.has(key)) { return this.polyglot.t(key, options); } return undefined; }; /** Detects language for the config path. */ LanguageManager.prototype.parseLanguage = function (path) { var regex = /\/(\w+)\.\w+$/g; var match = regex.exec(path); if (match === null) { return ''; } return match[1]; }; return LanguageManager; }(ObserverManager_1.ObserverManager)); exports.LanguageManager = LanguageManager;