forked from gpbl/isomorphic500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setLocale.js
29 lines (22 loc) · 805 Bytes
/
setLocale.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
// Express middleware to overwrite the locale from cookie or querystring
import config from "../config";
const debug = require("debug")("isomorphic500");
export default function setLocale(req, res, next) {
debug("Detected locale (from browser) is %s", req.locale);
// Locale can be changed by passing ?hl=<locale> in the querystring
if (req.query.hl) {
// But only the supported ones!
if (config.locales.indexOf(req.query.hl) > -1) {
req.locale = req.query.hl;
debug("Locale has been set from querystring: %s", req.locale);
}
}
// Or by setting a `hl` cookie
else if (req.cookies.hl) {
if (config.locales.indexOf(req.cookies.hl) > -1) {
req.locale = req.cookies.hl;
debug("Locale has been set from cookie: %s", req.locale);
}
}
next();
}