summaryrefslogtreecommitdiff
path: root/src/client/i18n.ts
blob: aeecb58a3ea3bdedca8bf6091473b0d1da67c520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { markRaw } from 'vue';
import { locale } from '@/config';

export class I18n<T extends Record<string, any>> {
	public locale: T;

	constructor(locale: T) {
		this.locale = locale;

		if (_DEV_) {
			console.log('i18n', this.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 string;

			if (_DEV_) {
				if (!str.includes('{')) {
					console.warn(`i18n: '${key}' has no any arg. so ref prop directly instead of call this method.`);
				}
			}

			if (args) {
				for (const [k, v] of Object.entries(args)) {
					str = str.replace(`{${k}}`, v);
				}
			}
			return str;
		} catch (e) {
			if (_DEV_) {
				console.warn(`missing localization '${key}'`);
				return `⚠'${key}'⚠`;
			}

			return key;
		}
	}
}

export const i18n = markRaw(new I18n(locale));

// このファイルに書きたくないけどここに書かないと何故かVeturが認識しない
declare module '@vue/runtime-core' {
	interface ComponentCustomProperties {
		$t: typeof i18n['t'];
		$ts: typeof i18n['locale'];
	}
}