1 |
|
2 | (function() {
|
3 | var Locale, Locales, app, _ref,
|
4 | __slice = [].slice;
|
5 |
|
6 | app = function(supported) {
|
7 | if (!(supported instanceof Locales)) {
|
8 | supported = new Locales(supported);
|
9 | supported.index();
|
10 | }
|
11 | return function(req, res, next) {
|
12 | var locales;
|
13 | locales = new Locales(req.headers["accept-language"]);
|
14 | req.locale = String(locales.best(supported));
|
15 | return next();
|
16 | };
|
17 | };
|
18 |
|
19 | app.Locale = (function() {
|
20 | var serialize;
|
21 |
|
22 | Locale["default"] = new Locale(process.env.LANG || "en_US");
|
23 |
|
24 | function Locale(str) {
|
25 | var country, language, match;
|
26 | if (!(match = str != null ? str.match(/[a-z]+/gi) : void 0)) {
|
27 | return;
|
28 | }
|
29 | language = match[0], country = match[1];
|
30 | this.language = language.toLowerCase();
|
31 | if (country) {
|
32 | this.country = country.toUpperCase();
|
33 | }
|
34 | }
|
35 |
|
36 | serialize = function() {
|
37 | var value;
|
38 | value = [this.language];
|
39 | if (this.country) {
|
40 | value.push(this.country);
|
41 | }
|
42 | return value.join("_");
|
43 | };
|
44 |
|
45 | Locale.prototype.toString = serialize;
|
46 |
|
47 | Locale.prototype.toJSON = serialize;
|
48 |
|
49 | return Locale;
|
50 |
|
51 | })();
|
52 |
|
53 | app.Locales = (function() {
|
54 | var serialize;
|
55 |
|
56 | Locales.prototype.length = 0;
|
57 |
|
58 | Locales.prototype._index = null;
|
59 |
|
60 | Locales.prototype.sort = Array.prototype.sort;
|
61 |
|
62 | Locales.prototype.push = Array.prototype.push;
|
63 |
|
64 | function Locales(str) {
|
65 | var item, locale, q, _i, _len, _ref, _ref1;
|
66 | if (!str) {
|
67 | return;
|
68 | }
|
69 | _ref = (String(str)).split(",");
|
70 | for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
71 | item = _ref[_i];
|
72 | _ref1 = item.split(";"), locale = _ref1[0], q = _ref1[1];
|
73 | locale = new Locale(locale.trim());
|
74 | locale.score = q ? +q.slice(2) || 0 : 1;
|
75 | this.push(locale);
|
76 | }
|
77 | this.sort(function(a, b) {
|
78 | return a.score < b.score;
|
79 | });
|
80 | }
|
81 |
|
82 | Locales.prototype.index = function() {
|
83 | var locale, _i, _len;
|
84 | if (!this._index) {
|
85 | this._index = {};
|
86 | for (_i = 0, _len = this.length; _i < _len; _i++) {
|
87 | locale = this[_i];
|
88 | this._index[locale] = true;
|
89 | }
|
90 | }
|
91 | return this._index;
|
92 | };
|
93 |
|
94 | Locales.prototype.best = function(locales) {
|
95 | var index, item, locale, _i, _len;
|
96 | locale = Locale["default"];
|
97 | if (!locales) {
|
98 | return this[0] || locale;
|
99 | }
|
100 | index = locales.index();
|
101 | for (_i = 0, _len = this.length; _i < _len; _i++) {
|
102 | item = this[_i];
|
103 | if (index[item]) {
|
104 | return item;
|
105 | }
|
106 | }
|
107 | return locale;
|
108 | };
|
109 |
|
110 | serialize = function() {
|
111 | return __slice.call(this);
|
112 | };
|
113 |
|
114 | Locales.prototype.toJSON = serialize;
|
115 |
|
116 | Locales.prototype.toString = function() {
|
117 | return String(this.toJSON());
|
118 | };
|
119 |
|
120 | return Locales;
|
121 |
|
122 | })();
|
123 |
|
124 | _ref = module.exports = app, Locale = _ref.Locale, Locales = _ref.Locales;
|
125 |
|
126 | }).call(this);
|