-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
314 lines (270 loc) · 8.16 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
'use strict';
const Debug = require('debug');
const debug = Debug('koa-locales');
const debugSilly = Debug('koa-locales:silly');
const ini = require('ini');
const util = require('util');
const fs = require('fs');
const path = require('path');
const ms = require('humanize-ms');
const assign = require('object-assign');
const yaml = require('js-yaml');
const DEFAULT_OPTIONS = {
defaultLocale: 'en-US',
queryField: 'locale',
cookieField: 'locale',
localeAlias: {},
writeCookie: true,
cookieMaxAge: '1y',
dir: undefined,
dirs: [path.join(process.cwd(), 'locales')],
functionName: '__',
};
module.exports = function (app, options) {
options = assign({}, DEFAULT_OPTIONS, options);
const defaultLocale = formatLocale(options.defaultLocale);
const queryField = options.queryField;
const cookieField = options.cookieField;
const cookieDomain = options.cookieDomain;
const localeAlias = options.localeAlias;
const writeCookie = options.writeCookie;
const cookieMaxAge = ms(options.cookieMaxAge);
const localeDir = options.dir;
const localeDirs = options.dirs;
const functionName = options.functionName;
const resources = {};
/**
* @Deprecated Use options.dirs instead.
*/
if (localeDir && localeDirs.indexOf(localeDir) === -1) {
localeDirs.push(localeDir);
}
for (let i = 0; i < localeDirs.length; i++) {
const dir = localeDirs[i];
if (!fs.existsSync(dir)) {
continue;
}
const names = fs.readdirSync(dir);
for (let j = 0; j < names.length; j++) {
const name = names[j];
const filepath = path.join(dir, name);
// support en_US.js => en-US.js
const locale = formatLocale(name.split('.')[0]);
let resource = {};
if (name.endsWith('.js') || name.endsWith('.json')) {
resource = flattening(require(filepath));
} else if (name.endsWith('.properties')) {
resource = ini.parse(fs.readFileSync(filepath, 'utf8'));
} else if (name.endsWith('.yml') || name.endsWith('.yaml')) {
resource = flattening(yaml.safeLoad(fs.readFileSync(filepath, 'utf8')));
}
resources[locale] = resources[locale] || {};
assign(resources[locale], resource);
}
}
debug('Init locales with %j, got %j resources', options, Object.keys(resources));
if (typeof app[functionName] !== 'undefined') {
console.warn('[koa-locales] will override exists "%s" function on app', functionName);
}
function gettext(locale, key, value) {
if (arguments.length === 0 || arguments.length === 1) {
// __()
// --('en')
return '';
}
const resource = resources[locale] || {};
let text = resource[key];
if (text === undefined) {
text = key;
}
debugSilly('%s: %j => %j', locale, key, text);
if (!text) {
return '';
}
if (arguments.length === 2) {
// __(locale, key)
return text;
}
if (arguments.length === 3) {
if (isObject(value)) {
// __(locale, key, object)
// __('zh', '{a} {b} {b} {a}', {a: 'foo', b: 'bar'})
// =>
// foo bar bar foo
return formatWithObject(text, value);
}
if (Array.isArray(value)) {
// __(locale, key, array)
// __('zh', '{0} {1} {1} {0}', ['foo', 'bar'])
// =>
// foo bar bar foo
return formatWithArray(text, value);
}
// __(locale, key, value)
return util.format(text, value);
}
// __(locale, key, value1, ...)
const args = new Array(arguments.length - 1);
args[0] = text;
for (let i = 2; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
return util.format.apply(util, args);
}
app[functionName] = gettext;
app.context[functionName] = function (key, value) {
if (arguments.length === 0) {
// __()
return '';
}
const locale = this.__getLocale();
if (arguments.length === 1) {
return gettext(locale, key);
}
if (arguments.length === 2) {
return gettext(locale, key, value);
}
const args = new Array(arguments.length + 1);
args[0] = locale;
for (let i = 0; i < arguments.length; i++) {
args[i + 1] = arguments[i];
}
return gettext.apply(this, args);
};
// 1. query: /?locale=en-US
// 2. cookie: locale=zh-TW
// 3. header: Accept-Language: zh-CN,zh;q=0.5
app.context.__getLocale = function () {
if (this.__locale) {
return this.__locale;
}
const cookieLocale = this.cookies.get(cookieField, { signed: false });
// 1. Query
let locale = this.query[queryField];
let localeOrigin = 'query';
// 2. Cookie
if (!locale) {
locale = cookieLocale;
localeOrigin = 'cookie';
}
// 3. Header
if (!locale) {
// Accept-Language: zh-CN,zh;q=0.5
// Accept-Language: zh-CN
let languages = this.acceptsLanguages();
if (languages) {
if (Array.isArray(languages)) {
if (languages[0] === '*') {
languages = languages.slice(1);
}
if (languages.length > 0) {
for (let i = 0; i < languages.length; i++) {
const lang = formatLocale(languages[i]);
if (resources[lang] || localeAlias[lang]) {
locale = lang;
localeOrigin = 'header';
break;
}
}
}
} else {
locale = languages;
localeOrigin = 'header';
}
}
// all missing, set it to defaultLocale
if (!locale) {
locale = defaultLocale;
localeOrigin = 'default';
}
}
// cookie alias
if (locale in localeAlias) {
const originalLocale = locale;
locale = localeAlias[locale];
debugSilly('Used alias, received %s but using %s', originalLocale, locale);
}
locale = formatLocale(locale);
// validate locale
if (!resources[locale]) {
debugSilly('Locale %s is not supported. Using default (%s)', locale, defaultLocale);
locale = defaultLocale;
}
// if header not send, set the locale cookie
if (writeCookie && cookieLocale !== locale && !this.headerSent) {
updateCookie(this, locale);
}
debug('Locale: %s from %s', locale, localeOrigin);
debugSilly('Locale: %s from %s', locale, localeOrigin);
this.__locale = locale;
this.__localeOrigin = localeOrigin;
return locale;
};
app.context.__getLocaleOrigin = function () {
if (this.__localeOrigin) return this.__localeOrigin;
this.__getLocale();
return this.__localeOrigin;
};
app.context.__setLocale = function (locale) {
this.__locale = locale;
this.__localeOrigin = 'set';
updateCookie(this, locale);
};
function updateCookie(ctx, locale) {
const cookieOptions = {
// make sure brower javascript can read the cookie
httpOnly: false,
maxAge: cookieMaxAge,
signed: false,
domain: cookieDomain,
overwrite: true,
};
ctx.cookies.set(cookieField, locale, cookieOptions);
debugSilly('Saved cookie with locale %s', locale);
}
};
function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
const ARRAY_INDEX_RE = /\{(\d+)\}/g;
function formatWithArray(text, values) {
return text.replace(ARRAY_INDEX_RE, function (orignal, matched) {
const index = parseInt(matched);
if (index < values.length) {
return values[index];
}
// not match index, return orignal text
return orignal;
});
}
const Object_INDEX_RE = /\{(.+?)\}/g;
function formatWithObject(text, values) {
return text.replace(Object_INDEX_RE, function (orignal, matched) {
const value = values[matched];
if (value) {
return value;
}
// not match index, return orignal text
return orignal;
});
}
function formatLocale(locale) {
// support zh_CN, en_US => zh-CN, en-US
return locale.replace('_', '-').toLowerCase();
}
function flattening(data) {
const result = {};
function deepFlat(data, keys) {
Object.keys(data).forEach(function (key) {
const value = data[key];
const k = keys ? keys + '.' + key : key;
if (isObject(value)) {
deepFlat(value, k);
} else {
result[k] = String(value);
}
});
}
deepFlat(data, '');
return result;
}