-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.js
69 lines (61 loc) · 1.7 KB
/
i18n.js
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
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import Backend from 'i18next-http-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
const queryParameterLangDetector = {
name: 'queryParameterLangDetector',
lookup(options) {
let found
if (typeof window !== 'undefined') {
const query = window.location.search.substring(1)
const params = query.split('&').reduce((acc, element) => {
const [key, value] = element.split('=')
acc[key] = value
return acc
}, {})
if (params[options.lookupQuerystring]) {
found = params[options.lookupQuerystring]
}
}
return found
},
cacheUserLanguage(lng, options) {
console.log('Set language to ' + lng)
if (options.lookupLocalStorage && typeof window !== 'undefined') {
window.localStorage.setItem(options.lookupLocalStorage, lng)
}
},
}
import ptBR from './public/locales/pt-BR/translation.json'
import en from './public/locales/en/translation.json'
const resource = {
en: {
translation: en,
},
'pt-BR': {
translation: ptBR,
},
}
i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources: resource,
fallbackLng: 'en',
backend: {
loadPath: '/locales/{{lng}}/{{ns}}.json',
},
detection: {
order: ['queryParameterLangDetector', 'localStorage', 'navigator'],
lookupQuerystring: 'lang',
lookupLocalStorage: 'i18nextLng',
caches: ['localStorage'],
checkWhitelist: true, // Ensure the detected language is in the whitelist
},
whitelist: Object.keys(resource),
interpolation: {
escapeValue: false,
},
})
export default i18n