-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
59 lines (47 loc) · 1.34 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
var list = require('./list');
const DEFAULT_LANGUAGE = 'ru';
function I18N(req) {
this.locales = list;
this.setLocale(req);
};
I18N.prototype = {
/**
* @param {?HTTP.Request}
*/
setLocale: function setLocale(req) {
var ret;
if (req) {
if (req.query && req.query.locale) {
ret = req.query.locale;
this.localeFrom = 'url';
} else if (req.user && req.user.locale) {
ret = req.user.locale;
this.localeFrom = 'user';
} else if (req.cookies && req.cookies.locale) {
ret = req.cookies.locale;
this.localeFrom = 'cookie';
}
}
this.lang = Object.keys(this.locales).indexOf(ret) === -1 ? DEFAULT_LANGUAGE : ret;
this.dictionary = require('./dict/' + this.lang);
},
getLocale: function getLocale() {
return this.lang;
},
/**
* @param {String} key
* @param {String{}} [data]
* @return {String}
*/
get: function get(key, data) {
var str = this.dictionary[key] || ''
if ( ! data) {
return str;
}
var re = new RegExp('\{\{(\\w+)\}\}', 'g');
return str.replace(re, function(all, k) {
return data[k] || '';
});
}
};
module.exports = I18N;