2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2021-02-06 09:55:53 +00:00
|
|
|
export class I18n<T extends Record<string, any>> {
|
|
|
|
public locale: T;
|
|
|
|
|
|
|
|
constructor(locale: T) {
|
|
|
|
this.locale = locale;
|
|
|
|
|
|
|
|
//#region BIND
|
2022-12-04 06:03:09 +00:00
|
|
|
//this.t = this.t.bind(this);
|
2021-02-06 09:55:53 +00:00
|
|
|
//#endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
// string にしているのは、ドット区切りでのパス指定を許可するため
|
|
|
|
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
|
|
|
|
public t(key: string, args?: Record<string, any>): string {
|
|
|
|
try {
|
2023-02-09 02:46:08 +00:00
|
|
|
let str = key.split('.').reduce((o, i) => o[i], this.locale as any) as string;
|
2021-02-06 09:55:53 +00:00
|
|
|
|
|
|
|
if (args) {
|
|
|
|
for (const [k, v] of Object.entries(args)) {
|
|
|
|
str = str.replace(`{${k}}`, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
} catch (e) {
|
2021-02-13 03:28:26 +00:00
|
|
|
console.warn(`missing localization '${key}'`);
|
2021-02-06 09:55:53 +00:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|