forked from koa-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (109 loc) · 3.01 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
/*!
* i18n
* Copyright(c) 2015 Fangdun Cai and Other Contributors
* MIT Licensed
*/
/*jshint esnext:true */
/**
* Module dependencies.
*/
var debug = require('debug')('koa:i18n2');
var i18n2 = require('i18n-2');
var preferredLanguages = require('negotiator/lib/language');
/**
* Hacked i18n.
*/
function I18n(opts) {
i18n2.call(this, opts);
var enables = this.enables = [];
var modes = opts.modes || [];
modes.forEach(function (v) {
if(typeof v !== 'function') {
v = localeMethods.filter(function (t) {
return t.toLowerCase() === v.toLowerCase();
})[0];
}
if (v) {
enables.push(v);
}
});
}
Object.setPrototypeOf(I18n, i18n2);
Object.setPrototypeOf(I18n.prototype, i18n2.prototype);
var localeMethods = [ 'Subdomain', 'Cookie', 'Header', 'Query', 'Url', 'TLD' ];
var SET_PREFIX = 'setLocaleFrom';
var GET_PREFIX = 'getLocaleFrom';
localeMethods.forEach(function (m) {
Object.defineProperty(I18n.prototype, SET_PREFIX + m, {
value: function () {
var locale;
var availableLanguages = Object.keys(this.locales);
// If method is header, use Koa's built-in language negotiator so we get the closest
// match rather than going back to default if it's not an exact match
if (m === 'Header') {
locale = this.request.acceptsLanguages(availableLanguages);
} else {
locale = this.request[GET_PREFIX + m]();
}
if (!locale) {
return false;
}
var preferredLanguage = preferredLanguages(locale, availableLanguages);
locale = preferredLanguage && preferredLanguage[0] || '';
if (locale.toLowerCase() === this.getLocale().toLowerCase()) return true;
if (locale) {
this.setLocale(locale);
debug('Overriding locale from %s : %s', m.toLowerCase(), locale);
return true;
}
}
});
});
/**
* Expose ial.
*/
module.exports = ial;
// Internationalization and Localization
function ial(app, opts) {
/**
* Lazily creates an i18n.
*
* @api public
*/
Object.defineProperty(app.context, 'i18n', {
get: function () {
if (this._i18n) {
return this._i18n;
}
var i18n = this._i18n = new I18n(opts);
i18n.request = this.request;
// merge into ctx.state
registerMethods(this.state, this._i18n);
debug('app.ctx.i18n %j', this._i18n);
return this._i18n;
}
});
Object.defineProperty(app.request, 'i18n', {
get: function () {
return this.ctx.i18n;
}
});
return function *i18nMiddleware(next) {
this.i18n.enables.some(function (key) {
var customLocaleMethod = typeof key === 'function' && this.i18n.setLocale(key.apply(this));
if (customLocaleMethod || this.i18n[SET_PREFIX + key]()) return true;
}.bind(this));
yield next;
};
}
/**
* Register methods
*/
function registerMethods(helpers, i18n) {
I18n.resMethods.forEach(function (method) {
helpers[method] = function () {
return i18n[method].apply(i18n, arguments);
};
});
return helpers;
}