summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/i18n.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/frontend/src/scripts/i18n.ts')
-rw-r--r--packages/frontend/src/scripts/i18n.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/frontend/src/scripts/i18n.ts b/packages/frontend/src/scripts/i18n.ts
new file mode 100644
index 0000000000..54184386da
--- /dev/null
+++ b/packages/frontend/src/scripts/i18n.ts
@@ -0,0 +1,29 @@
+export class I18n<T extends Record<string, any>> {
+ public ts: T;
+
+ constructor(locale: T) {
+ this.ts = locale;
+
+ //#region BIND
+ this.t = this.t.bind(this);
+ //#endregion
+ }
+
+ // string にしているのは、ドット区切りでのパス指定を許可するため
+ // なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
+ public t(key: string, args?: Record<string, string | number>): string {
+ try {
+ let str = key.split('.').reduce((o, i) => o[i], this.ts) as unknown as string;
+
+ if (args) {
+ for (const [k, v] of Object.entries(args)) {
+ str = str.replace(`{${k}}`, v.toString());
+ }
+ }
+ return str;
+ } catch (err) {
+ console.warn(`missing localization '${key}'`);
+ return key;
+ }
+ }
+}