diff --git a/packages/client/src/components/global/i18n.ts b/packages/client/src/components/global/i18n.ts index a0113a6549..545df6d252 100644 --- a/packages/client/src/components/global/i18n.ts +++ b/packages/client/src/components/global/i18n.ts @@ -27,14 +27,14 @@ export default defineComponent({ if (nextBracketOpen === -1) { parsed.push(str); break; - } else { - if (nextBracketOpen > 0) parsed.push(str.substr(0, nextBracketOpen)); - parsed.push({ - arg: str.substring(nextBracketOpen + 1, nextBracketClose), - }); } + if (nextBracketOpen > 0) + parsed.push(str.substring(0, nextBracketOpen + 1)); + parsed.push({ + arg: str.substring(nextBracketOpen + 1, nextBracketClose), + }); - str = str.substr(nextBracketClose + 1); + str = str.substring(nextBracketClose + 1); } return h( diff --git a/packages/client/src/i18n.ts b/packages/client/src/i18n.ts index b7aa8c7bef..1b3fdc855d 100644 --- a/packages/client/src/i18n.ts +++ b/packages/client/src/i18n.ts @@ -1,13 +1,36 @@ import { markRaw } from "vue"; import { locale } from "@/config"; -import { I18n } from "@/scripts/i18n"; -export const i18n = markRaw(new I18n(locale)); +class I18n> { + public ts: T; -// このファイルに書きたくないけどここに書かないと何故かVeturが認識しない -declare module "@vue/runtime-core" { - interface ComponentCustomProperties { - $t: (typeof i18n)["t"]; - $ts: (typeof i18n)["locale"]; + 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 { + 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; + } } } + +export const i18n = markRaw(new I18n(locale)); diff --git a/packages/client/src/scripts/i18n.ts b/packages/client/src/scripts/i18n.ts deleted file mode 100644 index 7e2b237118..0000000000 --- a/packages/client/src/scripts/i18n.ts +++ /dev/null @@ -1,31 +0,0 @@ -export class I18n> { - 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 { - 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; - } - } -}