-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.js
executable file
·92 lines (74 loc) · 2.69 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const i18n = require('i18next')
const XHR = require('i18next-xhr-backend')
const LanguageDetector = require('i18next-browser-languagedetector')
const detectionOptions = {
// order and from where user language should be detected
order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
// keys or params to lookup language from
lookupQuerystring: 'lng',
lookupCookie: 'lng', //'i18next',
lookupLocalStorage: 'lng', //'i18nextLng',
// cache user language on
// caches: ['localStorage', 'cookie'],
// excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage)
// optional expire and domain for set cookie
// cookieMinutes: 10,
// cookieDomain: 'myDomain',
// optional htmlTag with lang attribute, the default is:
// htmlTag: document.documentElement
};
const options = {
fallbackLng: 'en',
// given 'en-US': 'all' --> ['en-US', 'en', 'dev'], 'currentOnly' --> 'en-US', 'languageOnly' --> 'en'
load: 'languageOnly', // no region specific locals like en-US, de-DE (only en, de)
whitelist: ['en','de'],
nonExplicitWhitelist: false, // if true will pass eg. en-US if finding en in whitelist
// have a common namespace used around the full app
// ns: [ 'common', 'home', 'login', 'signup', 'workout', 'user' ],
ns: 'common',
defaultNS: 'common',
fallbackNS: false,
debug: true,
saveMissing: true,
interpolation: {
escapeValue: false, // not needed for react!!
formatSeparator: ',',
format: (value, format, lng) => {
if (format === 'uppercase') return value.toUpperCase()
return value
}
},
detection: detectionOptions,
// react: {
// wait: false,
// nsMode: 'default'
// }
}
// for browser use xhr backend to load translations and browser lng detector
if (process.browser) {
i18n
.use(XHR)
// .use(Cache)
.use(LanguageDetector)
}
// initialize if not already initialized
if (!i18n.isInitialized) i18n.init(options)
// a simple helper to getInitialProps passed on loaded i18n data
i18n.getInitialProps = (req, namespaces) => {
if (!namespaces) namespaces = i18n.options.defaultNS
if (typeof namespaces === 'string') namespaces = [namespaces]
req.i18n.toJSON = () => null // do not serialize i18next instance and send to client
const initialI18nStore = {}
req.i18n.languages.forEach((l) => {
initialI18nStore[l] = {}
namespaces.forEach((ns) => {
initialI18nStore[l][ns] = req.i18n.services.resourceStore.data[l][ns] || {}
})
})
return {
i18n: req.i18n, // use the instance on req - fixed language on request (avoid issues in race conditions with lngs of different users)
initialI18nStore,
initialLanguage: req.i18n.language
}
}
module.exports = i18n