chore (client): remove global $t, $ts
This commit is contained in:
parent
47bc534b1f
commit
ced76bf978
3 changed files with 36 additions and 44 deletions
|
@ -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(
|
||||
|
|
|
@ -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<T extends Record<string, any>> {
|
||||
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, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const i18n = markRaw(new I18n(locale));
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue