forked from samuelmaddock/metastream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
108 lines (95 loc) · 3.18 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import i18n, { TranslationFunction, Resource } from 'i18next'
import { reactI18nextModule } from 'react-i18next'
import enUS from './en-US'
import enGB from './en-GB'
import deDE from './de-DE'
import ptBR from './pt-BR'
import huHU from './hu-HU'
import es from './es'
import ru from './ru'
import ja from './ja'
import ar from './ar'
import koKR from './ko-KR'
import frFR from './fr-FR'
import zhCN from './zh-CN'
import trTR from './tr-TR'
import itIT from './it-IT'
import pl from './pl'
export const DEFAULT_LANGUAGE = 'en-US'
export const locales = [
{ label: 'English', code: 'en-US', translation: enUS, flag: '🇺🇸' },
{ label: 'English', code: 'en-GB', translation: enGB, flag: '🇬🇧' },
{ label: 'Español', code: 'es-ES', translation: es, flag: '🇪🇸' },
{ label: 'Français', code: 'fr-FR', translation: frFR, flag: '🇫🇷' },
{ label: 'Italiano', code: 'it-IT', translation: itIT, flag: '🇮🇹' },
{ label: 'Pусский', code: 'ru-RU', translation: ru, flag: '🇷🇺' },
{ label: 'Polski', code: 'pl', translation: pl, flag: '🇵🇱' },
{ label: 'Português do Brasil', code: 'pt-BR', translation: ptBR, flag: '🇧🇷' },
{ label: 'Deutsch', code: 'de-DE', translation: deDE, flag: '🇩🇪' },
{ label: 'Türkçe', code: 'tr-TR', translation: trTR, flag: '🇹🇷' },
{ label: 'Magyar', code: 'hu-HU', translation: huHU, flag: '🇭🇺' },
{ label: '日本語', code: 'ja-JP', translation: ja, flag: '🇯🇵' },
{ label: '한국어', code: 'ko-KR', translation: koKR, flag: '🇰🇷' },
{ label: '简化字', code: 'zh-CN', translation: zhCN, flag: '🇨🇳' },
{ label: 'العربية الفصحى', code: 'ar', translation: ar, flag: '🇦🇪' }
]
const localeAliases: { [key: string]: string } = {
de: 'de-DE',
en: 'en-US',
es: 'es-ES',
hu: 'hu-HU',
ja: 'ja-JP',
pt: 'pt-BR',
ru: 'ru-RU',
fr: 'fr-FR'
}
const resources: Resource = locales.reduce(
(obj, locale) => ({
...obj,
[locale.code]: {
translation: locale.translation
}
}),
{}
)
Object.keys(localeAliases).forEach(alias => {
resources[alias] = resources[localeAliases[alias]]
})
i18n.use(reactI18nextModule).init({
debug: process.env.NODE_ENV === 'development',
resources,
lng: DEFAULT_LANGUAGE,
fallbackLng: DEFAULT_LANGUAGE,
keySeparator: false, // we do not use keys in form messages.welcome
interpolation: {
escapeValue: false // react already safes from xss
}
})
type keys = keyof typeof enUS
export const t: TranslationFunction<any, object, keys> = i18n.t.bind(i18n)
export const translateEscaped: typeof t = (key, vars) => {
return t(key, {
...vars,
interpolation: {
escapeValue: true
}
})
}
export function initLocale(defaultLocale: string = navigator.language) {
if (process.env.NODE_ENV === 'development') {
Object.assign((window as any).app, { i18n })
}
if (typeof defaultLocale === 'string') {
setLocale(defaultLocale)
}
}
export const setLocale = (locale: string) => {
console.debug(`Setting locale to ${locale}`)
if (locale !== i18n.language) {
i18n.changeLanguage(locale)
}
const { documentElement } = document
if (documentElement.lang) {
documentElement.lang = locale
}
}