@senspark/ee
Version:
utility library for cocos creator
210 lines (209 loc) • 7.88 kB
JavaScript
"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 __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var LanguageManager_1 = require("./LanguageManager");
var _a = cc._decorator, ccclass = _a.ccclass, disallowMultiple = _a.disallowMultiple, executeInEditMode = _a.executeInEditMode, inspector = _a.inspector, menu = _a.menu, property = _a.property;
var LanguageComponent = /** @class */ (function (_super) {
__extends(LanguageComponent, _super);
function LanguageComponent() {
var _this = _super.call(this) || this;
/** Gets or sets the multilingual key. */
_this._key = '{null}';
_this._paramValues = [];
/** Associated label component. */
_this.label = null;
_this.componentId = (LanguageComponent_1.counter++).toString();
return _this;
}
LanguageComponent_1 = LanguageComponent;
Object.defineProperty(LanguageComponent.prototype, "key", {
get: function () {
return this._key;
},
set: function (value) {
this._key = value;
this.updateText();
},
enumerable: true,
configurable: true
});
Object.defineProperty(LanguageComponent.prototype, "format", {
/** Gets the multilingual format corresponding to the current key. */
get: function () {
return this.getLanguageManager().getFormat(this.key) || '';
},
enumerable: true,
configurable: true
});
Object.defineProperty(LanguageComponent.prototype, "paramKeys", {
/** Gets the multilingual parameter keys. */
get: function () {
return this.parseParamKeys(this.format);
},
enumerable: true,
configurable: true
});
Object.defineProperty(LanguageComponent.prototype, "paramValues", {
/** Gets or sets the multilingual parameter values. */
get: function () {
while (this._paramValues.length < this.paramKeys.length) {
this._paramValues.push('');
}
return this._paramValues;
},
set: function (value) {
this._paramValues = value;
this.updateText();
},
enumerable: true,
configurable: true
});
Object.defineProperty(LanguageComponent.prototype, "string", {
/** Gets the translated string. */
get: function () {
var options = {};
for (var i = 0; i < this.paramKeys.length; ++i) {
options[this.paramKeys[i]] = this.paramValues[i];
}
return this.getLanguageManager().parseFormat(this.key, options);
},
enumerable: true,
configurable: true
});
Object.defineProperty(LanguageComponent.prototype, "config", {
get: function () {
return this.getLanguageManager().getConfigDir();
},
set: function (value) {
if (value !== undefined && value.length > 0 /* May be empty */) {
this.getLanguageManager().setConfigDir(value);
}
else {
this.getLanguageManager().resetConfigDir();
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(LanguageComponent.prototype, "languages", {
get: function () {
return this.getLanguageManager().getLanguages();
},
enumerable: true,
configurable: true
});
Object.defineProperty(LanguageComponent.prototype, "language", {
get: function () {
return this.getLanguageManager().getCurrentLanguage();
},
set: function (value) {
this.getLanguageManager().setCurrentLanguage(value);
},
enumerable: true,
configurable: true
});
LanguageComponent.prototype.getLanguageManager = function () {
return this.manager || (this.manager = LanguageManager_1.LanguageManager.getInstance());
};
LanguageComponent.prototype.onEnable = function () {
var _this = this;
this.getLanguageManager().addObserver(this.componentId, function () {
_this.updateText();
});
this.updateText();
};
LanguageComponent.prototype.onDisable = function () {
this.getLanguageManager().removeObserver(this.componentId);
};
LanguageComponent.prototype.update = function () {
if (CC_EDITOR) {
// Repeatedly update string when in editor mode.
this.updateText();
}
};
LanguageComponent.prototype.updateText = function () {
if (this.label === null) {
this.label = this.getComponent(cc.Label);
if (this.label === null) {
// Component not found.
return;
}
}
this.label.string = this.string || '';
};
LanguageComponent.prototype.parseParamKeys = function (format) {
var regex = /%{(.*?)}/g;
var match = regex.exec(format);
var params = [];
while (match !== null) {
params.push(match[1]);
match = regex.exec(format);
}
return params;
};
var LanguageComponent_1;
LanguageComponent.counter = 0;
__decorate([
property(cc.String)
], LanguageComponent.prototype, "_key", void 0);
__decorate([
property({ type: cc.String })
], LanguageComponent.prototype, "key", null);
__decorate([
property({
type: cc.String,
readonly: true,
})
], LanguageComponent.prototype, "format", null);
__decorate([
property([cc.String])
], LanguageComponent.prototype, "_paramValues", void 0);
__decorate([
property({ readonly: true })
], LanguageComponent.prototype, "paramKeys", null);
__decorate([
property
], LanguageComponent.prototype, "paramValues", null);
__decorate([
property({
type: cc.String,
readonly: true,
})
], LanguageComponent.prototype, "string", null);
__decorate([
property({ type: cc.String })
], LanguageComponent.prototype, "config", null);
__decorate([
property
], LanguageComponent.prototype, "languages", null);
__decorate([
property({ type: cc.String })
], LanguageComponent.prototype, "language", null);
LanguageComponent = LanguageComponent_1 = __decorate([
ccclass,
disallowMultiple,
executeInEditMode,
inspector('packages://ee/inspector/LanguageInspector.js'),
menu('ee/LanguageComponent')
], LanguageComponent);
return LanguageComponent;
}(cc.Component));
exports.LanguageComponent = LanguageComponent;