/
index.js
161 lines (137 loc) · 4.04 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
// Generated by CoffeeScript 1.6.3
(function() {
var Locale, Locales, app, _ref,
__slice = [].slice;
app = function(supported, def) {
if (!(supported instanceof Locales)) {
supported = new Locales(supported, def);
supported.index();
}
return function(req, res, next) {
var bestLocale, locales;
locales = new Locales(req.headers["accept-language"]);
bestLocale = locales.best(supported);
req.locale = String(bestLocale);
req.rawLocale = bestLocale;
return next();
};
};
app.Locale = (function() {
var serialize;
Locale["default"] = new Locale(process.env.LANG || "en_US");
function Locale(str) {
var country, language, match, normalized;
if (!(match = str != null ? str.match(/[a-z]+/gi) : void 0)) {
return;
}
language = match[0], country = match[1];
this.code = str;
this.language = language.toLowerCase();
if (country) {
this.country = country.toUpperCase();
}
normalized = [this.language];
if (this.country) {
normalized.push(this.country);
}
this.normalized = normalized.join("_");
}
serialize = function() {
if (this.language) {
return this.code;
} else {
return null;
}
};
Locale.prototype.toString = serialize;
Locale.prototype.toJSON = serialize;
return Locale;
})();
app.Locales = (function() {
var serialize;
Locales.prototype.length = 0;
Locales.prototype._index = null;
Locales.prototype.sort = Array.prototype.sort;
Locales.prototype.push = Array.prototype.push;
function Locales(str, def) {
var item, locale, q, _i, _len, _ref, _ref1;
if (def) {
this["default"] = new Locale(def);
}
if (!str) {
return;
}
_ref = (String(str)).split(",");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
_ref1 = item.split(";"), locale = _ref1[0], q = _ref1[1];
locale = new Locale(locale.trim());
locale.score = q ? +q.slice(2) || 0 : 1;
this.push(locale);
}
this.sort(function(a, b) {
return b.score - a.score;
});
}
Locales.prototype.index = function() {
var idx, locale, _i, _len;
if (!this._index) {
this._index = {};
for (idx = _i = 0, _len = this.length; _i < _len; idx = ++_i) {
locale = this[idx];
this._index[locale.normalized] = idx;
}
}
return this._index;
};
Locales.prototype.best = function(locales) {
var index, item, l, languageIndex, locale, normalizedIndex, setLocale, _i, _j, _len, _len1;
setLocale = function(l) {
var r;
r = l;
r.defaulted = false;
return r;
};
locale = Locale["default"];
if (locales && locales["default"]) {
locale = locales["default"];
}
locale.defaulted = true;
if (!locales) {
if (this[0]) {
locale = this[0];
locale.defaulted = true;
}
return locale;
}
index = locales.index();
for (_i = 0, _len = this.length; _i < _len; _i++) {
item = this[_i];
normalizedIndex = index[item.normalized];
languageIndex = index[item.language];
if (normalizedIndex != null) {
return setLocale(locales[normalizedIndex]);
} else if (languageIndex != null) {
return setLocale(locales[languageIndex]);
} else {
for (_j = 0, _len1 = locales.length; _j < _len1; _j++) {
l = locales[_j];
if (l.language === item.language) {
return setLocale(l);
}
}
}
}
return locale;
};
serialize = function() {
return __slice.call(this);
};
Locales.prototype.toJSON = serialize;
Locales.prototype.toString = function() {
return String(this.toJSON());
};
return Locales;
})();
_ref = module.exports = app, Locale = _ref.Locale, Locales = _ref.Locales;
}).call(this);