12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.$$ = exports.i18nLoader = void 0;
- const fs_1 = __importDefault(require("fs"));
- class i18nLoader {
- constructor(locale) {
- this.locale = locale;
- this.map = {};
- }
- static getInstance(locale) {
- if (!i18nLoader.instance)
- i18nLoader.instance = new i18nLoader(locale !== null && locale !== void 0 ? locale : i18nLoader.defaultLocale);
- return i18nLoader.instance;
- }
- setLocale(locale) {
- this.locale = locale;
- return this;
- }
- load(...paths) {
- for (const p of paths) {
- try {
- const data = fs_1.default.readFileSync(p).toString('utf8');
- try {
- this.loadJson(JSON.parse(data));
- }
- catch (err) {
- console.error('JSON parsing error:', err);
- }
- }
- catch (error) {
- console.error(`i18n file '${p}' not found.`, error);
- }
- }
- return this;
- }
- loadJson(obj) {
- for (const locale of Object.keys(obj)) {
- if (!this.map[locale])
- this.map[locale] = {};
- for (const key of Object.keys(obj[locale]))
- this.map[locale][key] = obj[locale][key];
- }
- return this;
- }
- get(key) {
- var _a;
- let value;
- if (!this.map[this.locale] && !this.map[i18nLoader.defaultLocale])
- return key;
- if (this.map[i18nLoader.defaultLocale])
- value = this.map[i18nLoader.defaultLocale][key];
- if (this.map[this.locale])
- value = (_a = this.map[this.locale][key]) !== null && _a !== void 0 ? _a : value;
- return value !== null && value !== void 0 ? value : key;
- }
- }
- exports.i18nLoader = i18nLoader;
- i18nLoader.defaultLocale = 'en_US';
- const $$ = (key, params) => {
- let text = i18nLoader.getInstance().get(key);
- if (params) {
- for (const param of Object.keys(params)) {
- const regex = new RegExp(`\{${param}\}`, 'g');
- text = text.replace(regex, params[param]);
- }
- }
- return text;
- };
- exports.$$ = $$;
- //# sourceMappingURL=i18n.js.map
|