mirror of
https://github.com/misskey-dev/misskey
synced 2024-12-24 03:28:43 +09:00
0e4a111f81
Resolve #7779
30 lines
786 B
TypeScript
30 lines
786 B
TypeScript
export class I18n<T extends Record<string, any>> {
|
|
public locale: T;
|
|
|
|
constructor(locale: T) {
|
|
this.locale = locale;
|
|
|
|
//#region BIND
|
|
this.t = this.t.bind(this);
|
|
//#endregion
|
|
}
|
|
|
|
// string にしているのは、ドット区切りでのパス指定を許可するため
|
|
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
|
|
public t(key: string, args?: Record<string, any>): string {
|
|
try {
|
|
let str = key.split('.').reduce((o, i) => o[i], this.locale) 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;
|
|
}
|
|
}
|
|
}
|