-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
i18n.js
126 lines (113 loc) · 3.22 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const i18next = require('i18next');
const SUPPORTED_LOCALES = [
'af', // Afrikaans
'ar', // Arabic
'bg', // Bulgarian
'bn', // Bengali
'bs', // Bosnian
'ca', // Catalan
'cs', // Czech
'da', // Danish
'de', // German
'de-CH', // Swiss German
'el', // Greek
'en', // English
'eo', // Esperanto
'es', // Spanish
'et', // Estonian
'fa', // Persian/Farsi
'fi', // Finnish
'fr', // French
'gd', // Gaelic (Scottish)
'he', // Hebrew
'hi', // Hindi
'hr', // Croatian
'hu', // Hungarian
'id', // Indonesian
'is', // Icelandic
'it', // Italian
'ja', // Japanese
'ko', // Korean
'kz', // Kazach
'lt', // Lithuanian
'mk', // Macedonian
'mn', // Mongolian
'ms', // Malay
'ne', // Nepali
'nl', // Dutch
'nn', // Norwegian Nynorsk
'no', // Norwegian
'pl', // Polish
'pt', // Portuguese
'pt-BR', // Portuguese (Brazil)
'ro', // Romanian
'ru', // Russian
'si', // Sinhala
'sk', // Slovak
'sl', // Slovenian
'sq', // Albanian
'sr', // Serbian
'sr-Cyrl', // Serbian (Cyrillic)
'sv', // Swedish
'th', // Thai
'tr', // Turkish
'uk', // Ukrainian
'ur', // Urdu
'uz', // Uzbek
'vi', // Vietnamese
'zh', // Chinese
'zh-Hant', // Traditional Chinese
'sw', // Swahili
'ta' // Tamil
];
function generateResources(locales, ns) {
return locales.reduce((acc, locale) => {
let res;
// add an extra fallback - this handles the case where we have a partial set of translations for some reason
// by falling back to the english translations
try {
res = require(`../locales/${locale}/${ns}.json`);
} catch (err) {
res = require(`../locales/en/${ns}.json`);
}
// Note: due some random thing in TypeScript, 'requiring' a JSON file with a space in a key name, only adds it to the default export
// If changing this behaviour, please also check the comments and signup-form apps in another language (mainly sentences with a space in them)
acc[locale] = {
[ns]: {...res, ...(res.default && typeof res.default === 'object' ? res.default : {})}
};
return acc;
}, {});
}
/**
* @param {string} [lng]
* @param {'ghost'|'portal'|'test'|'signup-form'|'comments'|'search'|'newsletter'} ns
*/
module.exports = (lng = 'en', ns = 'portal') => {
const i18nextInstance = i18next.createInstance();
let interpolation = {};
if (ns === 'newsletter') {
interpolation = {
prefix: '{',
suffix: '}'
};
}
let resources = generateResources(SUPPORTED_LOCALES, ns);
i18nextInstance.init({
lng,
// allow keys to be phrases having `:`, `.`
nsSeparator: false,
keySeparator: false,
// if the value is an empty string, return the key
returnEmptyString: false,
// do not load a fallback
fallbackLng: false,
ns: ns,
defaultNS: ns,
// separators
interpolation,
resources
});
return i18nextInstance;
};
module.exports.SUPPORTED_LOCALES = SUPPORTED_LOCALES;
module.exports.generateResources = generateResources;