seyfert
Version:
The most advanced framework for discord bots
79 lines (78 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LangsHandler = void 0;
const common_1 = require("../common");
const router_1 = require("./router");
class LangsHandler extends common_1.BaseHandler {
values = {};
__paths = {};
filter = (path) => path.endsWith('.js') || (!path.endsWith('.d.ts') && path.endsWith('.ts')) || path.endsWith('.json');
defaultLang;
aliases = [];
getLocale(locale) {
return this.aliases.find(([_key, aliases]) => aliases.includes(locale))?.[0] ?? locale;
}
getKey(lang, message) {
let value = this.values[lang];
try {
for (const i of message.split('.')) {
value = value[i];
}
}
catch {
return;
}
if (typeof value !== 'string') {
return;
}
return value;
}
get(userLocale) {
const locale = this.getLocale(userLocale);
return (0, router_1.LangRouter)(locale, this.defaultLang ?? locale, this.values)();
}
async load(dir) {
const files = await this.loadFilesK(await this.getFiles(dir));
for (const i of files) {
this.parse(i);
}
}
parse(file) {
const locale = file.name.split('.').slice(0, -1).join('.') || file.name;
const result = this.onFile(locale, file.file);
if ('path' in file)
this.__paths[locale] = file.path;
if (result)
this.values[locale] = result;
}
set(instances) {
for (const i of instances) {
this.parse(i);
}
}
async reload(lang) {
if ((0, common_1.isCloudfareWorker)()) {
throw new Error('Reload in cloudfare worker is not supported');
}
const value = this.__paths[lang];
if (!value)
return null;
delete require.cache[value];
return (this.values[lang] = await (0, common_1.magicImport)(value).then(x => this.onFile(lang, x)));
}
async reloadAll(stopIfFail = true) {
for (const i in this.__paths) {
try {
await this.reload(i);
}
catch (e) {
if (stopIfFail)
throw e;
}
}
}
onFile(_locale, file) {
return file.default ?? false;
}
}
exports.LangsHandler = LangsHandler;