-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (42 loc) · 1.53 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
var geoip = require('geoip-ultralight');
var countries = require('country-data').countries;
var languages = require('country-data').languages;
function getCountryCode(ip, defaultCountry){
if (ip.indexOf('::ffff:') > -1) {
ip = ip.split(':').reverse()[0];
}
var countryCode;
var lookedUpCountry = geoip.lookupCountry(ip);
if (lookedUpCountry) {
countryCode = lookedUpCountry;
}
if ((ip === '127.0.0.1') || (!lookedUpCountry)) {
countryCode = defaultCountry;
}
return countryCode;
}
exports = module.exports = function (opts) {
var cookieLangName = opts.cookieLangName || "ulang";
var defaultCountry = opts.defaultCountry || "US";
var siteLangs = opts.siteLangs || ['en'];
if (siteLangs.constructor !== Array) throw new Error('siteLangs must be an Array with supported langs.');
return function geolang(req, res, next) {
// Geo data isn't in locals
if(!('countryLangData' in req.app.locals)) {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var countryCode=getCountryCode(ip, defaultCountry);
var countryData=countries[countryCode];
var countryLang = languages[countryData.languages[0]];
req.app.locals.countryData=countryData;
req.app.locals.countryLangData=countryLang;
req.app.locals.countryLang=countryLang.alpha2;
}
// Lang isn't already set so skip
if (!(cookieLangName && req.session && req.session[cookieLangName])) {
if (siteLangs.indexOf(req.app.locals.countryLang) > -1) {
req.session[cookieLangName]=req.app.locals.countryLang;
}
}
next();
};
};