This repository has been archived by the owner on May 10, 2019. It is now read-only.
/
i18n.js
232 lines (210 loc) · 7.21 KB
/
i18n.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* i18n-abide
*
* This module abides by the user's language preferences and makes it
* available throughout the app.
*
* This module abides by the Mozilla L10n way of doing things.
*
* The module abides.
*
* See docs/I18N.md for details.
*/
var logger = require('./logging.js').logger,
Gettext = require('node-gettext'),
path = require('path'),
util = require('util');
const BIDI_RTL_LANGS = ['ar', 'db-LB', 'fa', 'he'];
var mo_cache = {};
/**
* Connect middleware which is i18n aware.
*
* Usage:
app.use(i18n.abide({
supported_languages: ['en-US', 'fr', 'pl'],
default_lang: 'en-US',
locale_directory: 'locale'
}));
*
* Other valid options: gettext_alias, ngettext_alias
*/
exports.abide = function (options) {
if (! options.gettext_alias) options.gettext_alias = 'gettext';
if (! options.ngettext_alias) options.ngettext_alias = 'ngettext';
if (! options.supported_languages) options.supported_languages = ['en-US'];
if (! options.default_lang) options.default_lang = 'en-US';
if (! options.debug_lang) options.debug_lang = 'it-CH';
if (! options.locale_directory) options.locale_directory = 'locale';
if (! options.i18n_json_dir) options.i18n_json_dir = 'resources/static/i18n/';
var mo_file_path = function (locale) {
return path.resolve(
path.join(__dirname, '..'),
options.locale_directory,
path.join(locale, 'LC_MESSAGES', 'messages.mo'));
},
json_file_path = function (locale) {
return path.resolve(
path.join(__dirname, '..'),
path.join(options.i18n_json_dir, locale, 'client.json'));
},
debug_locale = localeFrom(options.debug_lang);
options.supported_languages.forEach(function (lang, i) {
var l = localeFrom(lang),
default_locale = localeFrom(options.default_lang);
mo_cache[l] = {
mo_exists: path.existsSync(mo_file_path(l)),
json_exists: path.existsSync(json_file_path(l)),
gt: null
};
if (l !== debug_locale) {
if (! mo_cache[l] || ! mo_cache[l].mo_exists || ! mo_cache[l].json_exists) {
var msg = util.format('Bad locale=[%s] file(s) do not exist [%s] or [%s]. See locale/README',
l, mo_file_path(l), json_file_path(l));
if (mo_cache[l].json_exists && l == default_locale) {
// mo files aren't critical... carry on
logger.warn(msg);
} else {
logger.error(msg);
throw msg;
}
}
}
});
return function(req, resp, next) {
var langs = parseAcceptLanguage(req.headers['accept-language']),
lang_dir,
lang = bestLanguage(langs, options.supported_languages,
options.default_lang),
debug_lang = options.debug_lang.toLowerCase(),
locale;
if (lang && lang.toLowerCase && lang.toLowerCase() == debug_lang) {
lang = 'db-LB'; // What? http://www.youtube.com/watch?v=rJLnGjhPT1Q
}
resp.local('lang', lang);
// BIDI support, which direction does text flow?
lang_dir = BIDI_RTL_LANGS.indexOf(lang) >= 0 ? 'rtl' : 'ltr';
resp.local('lang_dir', lang_dir);
req.lang = lang;
locale = localeFrom(lang);
resp.local('locale', locale);
req.locale = locale;
resp.local('format', format);
req.format = format;
if (mo_cache[locale].mo_exists) {
if (mo_cache[locale].gt === null) {
mo_cache[locale].gt = new Gettext();
mo_path = mo_file_path(locale);
mo_cache[locale].gt.addTextdomain(locale,
fs.readFileSync(mo_path));
mo_cache[locale].gt.textdomain(locale);
}
var gt = mo_cache[locale].gt;
resp.local(options.gettext_alias, gt.gettext.bind(gt));
req.gettext = gt.gettext.bind(gt);
resp.local(options.ngettext_alias, gt.ngettext.bind(gt));
req.ngettext = gt.ngettext.bind(gt);
} else {
// en-US in a non gettext environment... fake it
var identity = function (a, b) { return a; };
resp.local(options.gettext_alias, identity);
req.gettext = identity;
resp.local(options.ngettext_alias, identity);
req.ngettext = identity;
}
next();
};
};
function qualityCmp(a, b) {
if (a.quality == b.quality) {
return 0;
} else if (a.quality < b.quality) {
return 1;
} else {
return -1;
}
};
/**
* Parses the HTTP accept-language header and returns a
* sorted array of objects. Example object:
* {
* lang: 'pl', quality: 0.7
* }
*/
exports.parseAcceptLanguage = parseAcceptLanguage = function (header) {
// pl,fr-FR;q=0.3,en-US;q=0.1
if (! header || ! header.split) {
return [];
}
var raw_langs = header.split(',');
var langs = raw_langs.map(function (raw_lang) {
var parts = raw_lang.split(';');
var q = 1;
if (parts.length > 1 && parts[1].indexOf('q=') == 0) {
qval = parseFloat(parts[1].split('=')[1]);
if (isNaN(qval) === false) {
q = qval;
}
}
return { lang: parts[0].trim(), quality: q };
});
langs.sort(qualityCmp);
return langs;
};
// Given the user's prefered languages and a list of currently
// supported languages, returns the best match or a default language.
//
// languages must be a sorted list, the first match is returned.
function bestLanguage(languages, supported_languages, defaultLanguage) {
var lower = supported_languages.map(function (l) { return l.toLowerCase(); });
for(var i=0; i < languages.length; i++) {
var lq = languages[i];
if (lower.indexOf(lq.lang.toLowerCase()) !== -1) {
return lq.lang;
}
}
return defaultLanguage;
};
/**
* Given a language code, return a locale code the OS understands.
*
* language: en-US
* locale: en_US
*/
exports.localeFrom = localeFrom = function (language) {
if (! language || ! language.split) {
return "";
}
var parts = language.split('-');
if (parts.length === 1) {
return parts[0].toLowerCase();
} else if (parts.length === 2) {
return util.format('%s_%s', parts[0].toLowerCase(), parts[1].toUpperCase());
} else if (parts.length === 3) {
// sr-Cyrl-RS should be sr_RS
return util.format('%s_%s', parts[0].toLowerCase(), parts[2].toUpperCase());
} else {
logger.error(util.format("Unable to map a local from language code [%s]", language));
return language;
}
};
/**
* format provides string interpolation on the client and server side.
* It can be used with either an object for named variables, or an array
* of values for positional replacement.
*
* Named Example:
* format("%(salutation)s %(place)s", {salutation: "Hello", place: "World"}, true);
* Positional Example:
* format("%s %s", ["Hello", "World"]);
*/
exports.format = format = function (fmt, obj, named) {
if (! fmt) return "";
if (named) {
return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
} else {
return fmt.replace(/%s/g, function(match){return String(obj.shift())});
}
};