FoundKey/packages/backend/src/misc/i18n.ts
Johann150 4b6c3b2f37
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
properly await promise
2022-10-19 15:26:37 +02:00

29 lines
830 B
TypeScript

const locales = await import('../../../../locales/index.js').then(mod => mod.default);
export class I18n {
public ts: Record<string, any>;
constructor(locale: string) {
this.ts = locales[locale];
this.t = this.t.bind(this);
}
// string にしているのは、ドット区切りでのパス指定を許可するため
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
public t(key: string, args?: Record<string, any>): string {
try {
let str = key.split('.').reduce((o, i) => o[i], this.ts) as string;
if (args) {
for (const [k, v] of Object.entries(args)) {
str = str.replace(`{${k}}`, v);
}
}
return str;
} catch (e) {
console.warn(`missing localization '${key}'`);
return key;
}
}
}