-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathheader.js
42 lines (33 loc) · 977 Bytes
/
header.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
export default {
name: 'header',
lookup(req, res, options) {
let found;
if (typeof req !== 'undefined') {
let headers = req.headers;
if (!headers) return found;
let locales = [];
let acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language'];
if (acceptLanguage) {
let lngs = [], i, m;
const rgx = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
do {
m = rgx.exec(acceptLanguage);
if (m) {
const lng = m[1], weight = m[5] || '1', q = Number(weight);
if (lng && !isNaN(q)) {
lngs.push({lng, q});
}
}
} while (m);
lngs.sort(function(a,b) {
return b.q - a.q;
});
for (i = 0; i < lngs.length; i++) {
locales.push(lngs[i].lng);
}
if (locales.length) found = locales;
}
}
return found;
}
};