/
I18n.js
312 lines (265 loc) · 9.4 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
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
const errors = require('@tryghost/errors');
const logging = require('@tryghost/logging');
const fs = require('fs-extra');
const path = require('path');
const MessageFormat = require('intl-messageformat');
const jp = require('jsonpath');
const isString = require('lodash/isString');
const isObject = require('lodash/isObject');
const isEqual = require('lodash/isEqual');
const isNil = require('lodash/isNil');
const merge = require('lodash/merge');
const get = require('lodash/get');
class I18n {
/**
* @param {object} [options]
* @param {string} options.basePath - the base path to the translations directory
* @param {string} [options.locale] - a locale string
* @param {string} [options.stringMode] - which mode our translation keys use
*/
constructor(options = {}) {
this._basePath = options.basePath || __dirname;
this._locale = options.locale || this.defaultLocale();
this._stringMode = options.stringMode || 'dot';
this._strings = null;
}
/**
* BasePath getter & setter used for testing
*/
set basePath(basePath) {
this._basePath = basePath;
}
/**
* Need to call init after this
*/
get basePath() {
return this._basePath;
}
/**
* English is our default locale
*/
defaultLocale() {
return 'en';
}
supportedLocales() {
return [this.defaultLocale()];
}
/**
* Exporting the current locale (e.g. "en") to make it available for other files as well,
* such as core/frontend/helpers/date.js and core/frontend/helpers/lang.js
*/
locale() {
return this._locale;
}
/**
* Helper method to find and compile the given data context with a proper string resource.
*
* @param {string} translationPath Path within the JSON language file to desired string (ie: "errors.init.jsNotBuilt")
* @param {object} [bindings]
* @returns {string}
*/
t(translationPath, bindings) {
let string;
let msg;
string = this._findString(translationPath);
// If the path returns an array (as in the case with anything that has multiple paragraphs such as emails), then
// loop through them and return an array of translated/formatted strings. Otherwise, just return the normal
// translated/formatted string.
if (Array.isArray(string)) {
msg = [];
string.forEach(function (s) {
msg.push(this._formatMessage(s, bindings));
});
} else {
msg = this._formatMessage(string, bindings);
}
return msg;
}
/**
* Setup i18n support:
* - Load proper language file into memory
*/
init() {
this._strings = this._loadStrings();
this._initializeIntl();
}
/**
* Attempt to load strings from a file
*
* @param {string} [locale]
* @returns {object} strings
*/
_loadStrings(locale) {
locale = locale || this.locale();
try {
return this._readTranslationsFile(locale);
} catch (err) {
if (err.code === 'ENOENT') {
this._handleMissingFileError(locale);
if (locale !== this.defaultLocale()) {
this._handleFallbackToDefault();
return this._loadStrings(this.defaultLocale());
}
} else if (err instanceof SyntaxError) {
this._handleInvalidFileError(locale, err);
} else {
throw err;
}
// At this point we've done all we can and strings must be an object
return {};
}
}
/**
* Do the lookup within the JSON file using jsonpath
*
* @param {String} msgPath
*/
_getCandidateString(msgPath) {
// Our default string mode is "dot" for dot-notation, e.g. $.something.like.this used in the backend
// Both jsonpath's dot-notation and bracket-notation start with '$' E.g.: $.store.book.title or $['store']['book']['title']
// While bracket-notation allows any Unicode characters in keys (i.e. for themes / fulltext mode) E.g. $['Read more']
// dot-notation allows only word characters in keys for backend messages (that is \w or [A-Za-z0-9_] in RegExp)
let jsonPath = `$.${msgPath}`;
let fallback = null;
if (this._stringMode === 'fulltext') {
jsonPath = jp.stringify(['$', msgPath]);
// In fulltext mode we can use the passed string as a fallback
fallback = msgPath;
}
try {
return jp.value(this._strings, jsonPath) || fallback;
} catch (err) {
this._handleInvalidKeyError(msgPath, err);
}
}
/**
* Parse JSON file for matching locale, returns string giving path.
*
* @param {string} msgPath Path with in the JSON language file to desired string (ie: "errors.init.jsNotBuilt")
* @returns {string}
*/
_findString(msgPath, opts) {
const options = merge({log: true}, opts || {});
let candidateString;
let matchingString;
// no path? no string
if (msgPath.length === 0 || !isString(msgPath)) {
this._handleEmptyKeyError();
return '';
}
// If not in memory, load translations for core
if (isNil(this._strings)) {
this._handleUninitialisedError(msgPath);
}
candidateString = this._getCandidateString(msgPath);
matchingString = candidateString || {};
if (isObject(matchingString) || isEqual(matchingString, {})) {
if (options.log) {
this._handleMissingKeyError(msgPath);
}
matchingString = this._fallbackError();
}
return matchingString;
}
_translationFileDirs() {
return [this.basePath];
}
// If we are passed a locale, use that, else use this.locale
_translationFileName(locale) {
return `${locale || this.locale()}.json`;
}
/**
* Read the translations file
* Error handling to be done by consumer
*
* @param {string} locale
*/
_readTranslationsFile(locale) {
const filePath = path.join(...this._translationFileDirs(), this._translationFileName(locale));
const content = fs.readFileSync(filePath, 'utf8');
return JSON.parse(content);
}
/**
* Format the string using the correct locale and applying any bindings
* @param {String} string
* @param {Object} bindings
*/
_formatMessage(string, bindings) {
let currentLocale = this.locale();
let msg = new MessageFormat(string, currentLocale);
try {
msg = msg.format(bindings);
} catch (err) {
this._handleFormatError(err);
// fallback
msg = new MessageFormat(this._fallbackError(), currentLocale);
msg = msg.format();
}
return msg;
}
/**
* [Private] Setup i18n support:
* - Polyfill node.js if it does not have Intl support or support for a particular locale
*/
_initializeIntl() {
let hasBuiltInLocaleData;
let IntlPolyfill;
if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
hasBuiltInLocaleData = this.supportedLocales().every(function (locale) {
return Intl.NumberFormat.supportedLocalesOf(locale)[0] === locale &&
Intl.DateTimeFormat.supportedLocalesOf(locale)[0] === locale;
});
if (!hasBuiltInLocaleData) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and replace the constructors with need with the polyfill's.
IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
}
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl');
}
}
_handleUninitialisedError(key) {
logging.warn(`i18n was used before it was initialised with key ${key}`);
this.init();
}
_handleFormatError(err) {
logging.error(err.message);
}
_handleFallbackToDefault() {
logging.warn(`i18n is falling back to ${this.defaultLocale()}.json.`);
}
_handleMissingFileError(locale) {
logging.warn(`i18n was unable to find ${locale}.json.`);
}
_handleInvalidFileError(locale, err) {
logging.error(new errors.IncorrectUsageError({
err,
message: `i18n was unable to parse ${locale}.json. Please check that it is valid JSON.`
}));
}
_handleEmptyKeyError() {
logging.warn('i18n.t() was called without a key');
}
_handleMissingKeyError(key) {
logging.error(new errors.IncorrectUsageError({
message: `i18n.t() was called with a key that could not be found: ${key}`
}));
}
_handleInvalidKeyError(key, err) {
throw new errors.IncorrectUsageError({
err,
message: `i18n.t() called with an invalid key: ${key}`
});
}
/**
* A really basic error for if everything goes wrong
*/
_fallbackError() {
return get(this._strings, 'errors.errors.anErrorOccurred', 'An error occurred');
}
}
module.exports = I18n;