diff options
| author | tamaina <tamaina@hotmail.co.jp> | 2021-02-06 18:55:53 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-06 18:55:53 +0900 |
| commit | 40bfa3ef0407f83484031bfe74dcecb149c202a0 (patch) | |
| tree | 8128fa49e2041e00f6b130cb150f6571cf2a5ec7 /src/client/scripts/i18n.ts | |
| parent | Resolve #7096 (diff) | |
| download | misskey-40bfa3ef0407f83484031bfe74dcecb149c202a0.tar.gz misskey-40bfa3ef0407f83484031bfe74dcecb149c202a0.tar.bz2 misskey-40bfa3ef0407f83484031bfe74dcecb149c202a0.zip | |
Resurrect Service Worker (#7108)
* Resolve #7106
* fix lint
* fix lint
* save lang in idb
* fix lint
* fix
* cache locale file
* fix lint
* :v:
* wip
* fix [wip]
* fix [wip]
Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
Diffstat (limited to 'src/client/scripts/i18n.ts')
| -rw-r--r-- | src/client/scripts/i18n.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/client/scripts/i18n.ts b/src/client/scripts/i18n.ts new file mode 100644 index 0000000000..d535e236bb --- /dev/null +++ b/src/client/scripts/i18n.ts @@ -0,0 +1,44 @@ +// Notice: Service Workerでも使用します +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; + } + } +} |