8d06a6475e
* chore: 著作権とライセンスについての情報を各ファイルに追加する * chore: Add the SPDX information to each file Add copyright and licensing information as defined in version 3.0 of the REUSE Specification. * tweak format --------- Co-authored-by: syuilo <Syuilotan@yahoo.co.jp> * chore: Add SPDX-License-Identifier [skip ci] * add missing SPDX-License-Identifier * remove unused file --------- Co-authored-by: Shun Sakai <sorairolake@protonmail.ch> Co-authored-by: syuilo <Syuilotan@yahoo.co.jp> Co-authored-by: Chocolate Pie <106949016+chocolate-pie@users.noreply.github.com>
35 lines
910 B
TypeScript
35 lines
910 B
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
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 any) 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;
|
|
}
|
|
}
|
|
}
|