-
Notifications
You must be signed in to change notification settings - Fork 591
/
index.js
225 lines (209 loc) · 6.58 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
const path = require('path');
const fs = require('fs');
const i18next = require('i18next');
const i18nextMiddleware = require('i18next-express-middleware');
const FilesystemBackend = require('i18next-node-fs-backend');
const DEVELOPMENT = process.env.NODE_ENV !== 'production';
const LOCALE_PARAM = 'locale';
const defaultConfig = {
preload: ['en_US'],
fallbackLng: 'en_US',
backend: {},
detection: {
order: ['querystring', 'path', 'cookie'],
// keys to lookup in the http path (req.params)
lookupPath: LOCALE_PARAM,
lookupQuerystring: LOCALE_PARAM,
nsLookupPath: 'ns',
// cache the user's language in a cookie
caches: ['cookie'],
/**
* Hack to fix path language detection:
* By default, if req.params is not present, language detector will attempt
* to split the url and find locale and ns using positions in the url.
* This fails for routes which do not include the language in the path.
* Here's an example:
* app.use('/heres/my/route', i18nextMiddleware.handle())
* will detect 'my' as the language, but instead we should detect nothing
* and fallback to english
**/
lookupFromPathIndex: NaN,
},
};
/**
* Builds a default working i18n instance.
* - Uses language detection to pull the user's language from the url path
* - Stores translations on the local filesystem and serves them asyncronously.
*
* @param config can be an object to override default config options or a
* function. If a function is passed, the default config will be passed and the
* function's return value will be used as the config.
**/
export function i18nBuilder(
config: void | Object | ((defaultConfig: Object) => Object),
) {
const initConfig =
typeof config === 'function'
? config(defaultConfig)
: typeof config === 'object'
? Object.assign(defaultConfig, config)
: defaultConfig;
if (!initConfig.backend) {
initConfig.backend = initFsBackendOptions();
}
i18next
.use(i18nextMiddleware.LanguageDetector)
.use(FilesystemBackend)
.init(initConfig);
makeLocaleDirectory(
{locale: i18next.options.fallbackLng[0], namespace: 'translation'},
i18next,
);
return i18next;
}
export function initFsBackendOptions(
options?: {localesDir: string} = {localesDir: './locales'},
) {
const LOCALES_DIR = options.localesDir || './locales';
const fsBackendOptions = {
// path where resources get loaded from
loadPath: path.join(LOCALES_DIR, '/{{lng}}/{{ns}}.json'),
// path to post missing resources
addPath: path.join(LOCALES_DIR, '/{{lng}}/{{ns}}.json'),
// jsonIndent to use when storing json files
jsonIndent: 2,
};
return fsBackendOptions;
}
/**
* Gets all translations for a specific locale
* Language is detected
* Namespace is pulled from the url path
*/
export function getLocaleHandler(i18NextInstance: any) {
return [
i18nextMiddleware.handle(i18NextInstance),
(req: any, res: any) => {
// this comes from language detection
const locale = req.i18n.language;
let ns = req.params[req.i18n.options.detection.nsLookupPath || 'ns'];
if (!ns) {
ns = 'translation';
}
const getIsLangLoaded = () => req.i18n.hasResourceBundle(locale, ns);
return new Promise((resolve, reject) => {
if (getIsLangLoaded()) {
resolve(req.i18n.getResourceBundle(locale, ns));
} else {
req.i18n.loadLanguages(locale, err => {
const isLangLoaded = getIsLangLoaded();
if (err || !isLangLoaded) {
return reject(err);
} else if (isLangLoaded) {
return resolve(req.i18n.getResourceBundle(locale, ns));
} else {
reject(
new Error('could not load requested language or a fallback'),
);
}
});
}
})
.then(resources => {
res.send(resources);
})
.catch(_err => {
return res.status(404).send({message: 'error loading language'});
});
},
];
}
/**
* Creates a directory for a locale based on the loadPath parameter provided to
* fs backend options.
**/
export function makeLocaleDirectory(
{locale, namespace}: {locale: string, namespace?: string},
i18NextInstance: any,
) {
if (typeof namespace === 'undefined' || namespace.trim() === '') {
namespace = 'translation';
}
const addPath = i18NextInstance.services.interpolator.interpolate(
i18next.options.backend.addPath,
{
lng: locale,
ns: namespace,
},
);
const addPathDirectory = path.dirname(addPath);
if (!fs.existsSync(addPathDirectory)) {
mkDirByPathSync(addPathDirectory);
}
}
/**
* DEVELOPMENT ONLY
* Source key extraction - saves untranslated keys to the configured backend
**/
export function addMissingKeysHandler(i18NextInstance: any) {
return [
developmentOnly(
'translation key extraction is only enabled in development',
),
i18nextMiddleware.handle(i18NextInstance),
i18nextMiddleware.missingKeyHandler(i18NextInstance, {
lngParam: LOCALE_PARAM,
}),
];
}
function developmentOnly(message) {
return (req: any, res: any, next: any) => {
if (!DEVELOPMENT) {
return res
.status(403)
.send({error: message || 'Development only feature'});
}
return next();
};
}
/**
* node v6 does not support mkdir -p via the recursive flag
* https://stackoverflow.com/a/40686853/2188014
**/
function mkDirByPathSync(targetDir) {
const sep = path.sep;
const initDir = path.isAbsolute(targetDir) ? sep : '';
return targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve('.', parentDir, childDir);
try {
fs.mkdirSync(curDir);
} catch (err) {
if (err.code === 'EEXIST') {
// curDir already exists!
return curDir;
}
/*
* To avoid `EISDIR` error on Mac and
* `EACCES`-->`ENOENT` and `EPERM` on Windows.
*/
if (err.code === 'ENOENT') {
// Throw the original parentDir error on curDir `ENOENT` failure.
throw new Error(`EACCES: permission denied, mkdir '${parentDir}'`);
}
const caughtErr = ['EACCES', 'EPERM', 'EISDIR'].indexOf(err.code) > -1;
if (!caughtErr || (caughtErr && curDir === path.resolve(targetDir))) {
throw err; // Throw if it's just the last created dir.
}
}
return curDir;
}, initDir);
}