i18n.ts
This commit is contained in:
parent
57534be253
commit
f126676c29
|
@ -1,8 +1,8 @@
|
|||
export class I18n<T extends Record<string, any>> {
|
||||
public locale: T;
|
||||
public ts: T;
|
||||
|
||||
constructor(locale: T) {
|
||||
this.locale = locale;
|
||||
this.ts = locale;
|
||||
|
||||
//#region BIND
|
||||
this.t = this.t.bind(this);
|
||||
|
@ -11,13 +11,9 @@ export class I18n<T extends Record<string, any>> {
|
|||
|
||||
// string にしているのは、ドット区切りでのパス指定を許可するため
|
||||
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
|
||||
public t(key: string, args?: Record<string, any>): string {
|
||||
public t(key: string, args?: Record<string, string>): string {
|
||||
try {
|
||||
let str = key.split('.').reduce((o, i) => o[i], this.locale as T | any | string);
|
||||
|
||||
if (typeof str !== 'string') {
|
||||
return key;
|
||||
}
|
||||
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)) {
|
||||
|
@ -25,7 +21,7 @@ export class I18n<T extends Record<string, any>> {
|
|||
}
|
||||
}
|
||||
return str;
|
||||
} catch (e) {
|
||||
} catch (err) {
|
||||
console.warn(`missing localization '${key}'`);
|
||||
return key;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue