-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
75 lines (64 loc) · 1.62 KB
/
index.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import i18next, { TFunction } from 'i18next'
import path from 'path'
import _ from 'lodash'
import { readJsonSync } from 'fs-extra'
export const TRANSLATOR_VERSION = '2018.2.3'
export const show = false
const i18n = i18next.createInstance()
interface PoiI18nResources {
fixedT?: TFunction
__?: TFunction
__n?: TFunction
setLocale?: () => void
}
declare global {
interface Window {
language: string
isMain: boolean
i18n?: {
resources?: PoiI18nResources
translator?: PoiI18nResources
}
}
}
const readOrIgnoreJsonSync = (p: string): Record<string, string> => {
try {
return readJsonSync(p) as Record<string, string>
} catch (e) {
console.log(e)
return {}
}
}
export const pluginDidLoad = async (): Promise<void> => {
if (window.isMain) {
return
}
if (!window.i18n) {
window.i18n = {}
}
const resourceI18n = _(['ko-KR', 'en-US', 'ja-JP', 'zh-CN', 'zh-TW'])
.map((locale) => [
locale,
{
translator: readOrIgnoreJsonSync(path.join(__dirname, 'i18n', `${locale}.json`)),
},
])
.fromPairs()
.value()
try {
await i18n.init({
fallbackLng: false,
resources: resourceI18n,
keySeparator: false,
})
window.i18n.resources = {
fixedT: i18n.getFixedT(window.language, 'translator'),
}
window.i18n.resources.__ = window.i18n.resources.fixedT
window.i18n.resources.__n = window.i18n.resources.fixedT
window.i18n.resources.setLocale = () => {} // eslint-disable-line @typescript-eslint/no-empty-function
window.i18n.translator = window.i18n.resources
} catch (e) {
console.error(e)
}
}