mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Added protection for incorrect i18n variable names
closes #9305 - add a try/catch when formatting messages - fallback to the general error message if error ocurred - log the error
This commit is contained in:
parent
0be734a05c
commit
533ae22c2b
1 changed files with 30 additions and 11 deletions
|
@ -1,15 +1,15 @@
|
||||||
/* global Intl */
|
/* global Intl */
|
||||||
|
|
||||||
var supportedLocales = ['en'],
|
var supportedLocales = ['en'],
|
||||||
_ = require('lodash'),
|
_ = require('lodash'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
chalk = require('chalk'),
|
chalk = require('chalk'),
|
||||||
MessageFormat = require('intl-messageformat'),
|
MessageFormat = require('intl-messageformat'),
|
||||||
logging = require('./logging'),
|
logging = require('./logging'),
|
||||||
errors = require('./errors'),
|
errors = require('./errors'),
|
||||||
|
|
||||||
// TODO: fetch this dynamically based on overall blog settings (`key = "default_locale"`) in the `settings` table
|
// TODO: fetch this dynamically based on overall blog settings (`key = "default_locale"`) in the `settings` table
|
||||||
currentLocale = 'en',
|
currentLocale = 'en',
|
||||||
blos,
|
blos,
|
||||||
I18n;
|
I18n;
|
||||||
|
|
||||||
|
@ -34,11 +34,30 @@ I18n = {
|
||||||
string.forEach(function (s) {
|
string.forEach(function (s) {
|
||||||
var m = new MessageFormat(s, currentLocale);
|
var m = new MessageFormat(s, currentLocale);
|
||||||
|
|
||||||
msg.push(m.format(bindings));
|
try {
|
||||||
|
m.format(bindings);
|
||||||
|
} catch (err) {
|
||||||
|
logging.error(err.message);
|
||||||
|
|
||||||
|
// fallback
|
||||||
|
m = new MessageFormat(blos.errors.errors.anErrorOccurred, currentLocale);
|
||||||
|
m = msg.format();
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.push(m);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
msg = new MessageFormat(string, currentLocale);
|
msg = new MessageFormat(string, currentLocale);
|
||||||
msg = msg.format(bindings);
|
|
||||||
|
try {
|
||||||
|
msg = msg.format(bindings);
|
||||||
|
} catch (err) {
|
||||||
|
logging.error(err.message);
|
||||||
|
|
||||||
|
// fallback
|
||||||
|
msg = new MessageFormat(blos.errors.errors.anErrorOccurred, currentLocale);
|
||||||
|
msg = msg.format();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
|
@ -120,7 +139,7 @@ I18n = {
|
||||||
// `Intl` exists, but it doesn't have the data we need, so load the
|
// `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.
|
// polyfill and replace the constructors with need with the polyfill's.
|
||||||
IntlPolyfill = require('intl');
|
IntlPolyfill = require('intl');
|
||||||
Intl.NumberFormat = IntlPolyfill.NumberFormat;
|
Intl.NumberFormat = IntlPolyfill.NumberFormat;
|
||||||
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
|
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue