;(function() {
'use strict';
const LOCALE_MAP = {
'zh-cn': 'zh-CN', 'zh-hans': 'zh-CN', 'zh-sg': 'zh-CN', 'zh': 'zh-CN',
'zh-tw': 'zh-TW', 'zh-hk': 'zh-TW', 'zh-mo': 'zh-TW', 'zh-hant': 'zh-TW',
'en': 'en', 'en-us': 'en', 'en-gb': 'en', 'en-au': 'en',
'ru': 'ru', 'ru-ru': 'ru',
'fr': 'fr', 'fr-fr': 'fr', 'fr-ca': 'fr', 'fr-ch': 'fr', 'fr-be': 'fr',
'es': 'es', 'es-es': 'es', 'es-mx': 'es', 'es-ar': 'es',
'de': 'de', 'de-de': 'de', 'de-at': 'de', 'de-ch': 'de',
'pt': 'pt', 'pt-pt': 'pt', 'pt-br': 'pt',
'it': 'it', 'it-it': 'it', 'it-ch': 'it',
'hi': 'hi', 'hi-in': 'hi',
'ar': 'ar', 'ar-sa': 'ar', 'ar-eg': 'ar', 'ar-ae': 'ar',
'bn': 'bn', 'bn-bd': 'bn', 'bn-in': 'bn',
};
const FALLBACK = 'en';
class I18n {
constructor() {
this.locale = FALLBACK;
this._messages = {};
this._cache = {};
this._localeVer = 13; }
async init() {
var self = this;
const saved = window.store?.getLocale();
this.locale = saved
? (LOCALE_MAP[saved.toLowerCase()] || FALLBACK)
: FALLBACK;
var locales = ['zh-CN', 'zh-TW', 'en', 'ru', 'fr', 'es', 'de', 'pt', 'it', 'hi', 'ar', 'bn'];
await Promise.all(locales.map(function(l) { return self._load(l); }));
self._messages = self._cache[self.locale] || self._cache[FALLBACK] || {};
}
async _load(locale) {
if (this._cache[locale]) {
this._messages = this._cache[locale];
return;
}
try {
const resp = await fetch('./locales/' + locale + '.json?_v=' + this._localeVer);
if (!resp.ok) throw new Error('HTTP ' + resp.status);
this._cache[locale] = await resp.json();
this._messages = this._cache[locale];
} catch (e) {
console.warn('[i18n] Failed to load ' + locale + ', fallback to ' + FALLBACK + ':', e);
if (locale === FALLBACK) {
this._messages = {};
return;
}
this.locale = FALLBACK;
await this._load(FALLBACK);
}
}
$t(key, params) {
const val = key.split('.').reduce(function(o, k) { return o ? o[k] : undefined; }, this._messages);
if (val == null) return key;
if (!params) return val;
return Object.entries(params).reduce(function(s, arr) {
return s.replace(new RegExp('\\{' + arr[0] + '\\}', 'g'), arr[1]);
}, val);
}
async setLocale(locale) {
const norm = LOCALE_MAP[locale.toLowerCase()];
if (!norm || norm === this.locale) return;
if (this._cache[norm]) {
this._messages = this._cache[norm];
} else {
await this._load(norm);
}
this.locale = norm;
if (window.store) window.store.setLocale(norm);
window.dispatchEvent(new CustomEvent('locale-changed'));
}
install(app) {
const self = this;
app.config.globalProperties.$t = function(key, params) {
return self.$t(key, params);
};
app.provide('i18n', self);
}
}
window.i18n = new I18n();
})();