Null guard all interpolated strings passed to _t (#1035)

This commit is contained in:
Kegsay 2017-06-06 13:56:37 +01:00 committed by GitHub
parent e7af9bde23
commit 0b56d33bd2

View file

@ -35,6 +35,25 @@ counterpart.setFallbackLocale('en');
// just import counterpart and use it directly, we end up using a different
// instance.
export function _t(...args) {
// Horrible hack to avoid https://github.com/vector-im/riot-web/issues/4191
// The interpolation library that counterpart uses does not support undefined/null
// values and instead will throw an error. This is a problem since everywhere else
// in JS land passing undefined/null will simply stringify instead, and when converting
// valid ES6 template strings to i18n strings it's extremely easy to pass undefined/null
// if there are no existing null guards. To avoid this making the app completely inoperable,
// we'll check all the values for undefined/null and stringify them here.
if (args[1] && typeof args[1] === 'object') {
Object.keys(args[1]).forEach((k) => {
if (args[1][k] === undefined) {
console.warn("_t called with undefined interpolation name: " + k);
args[1][k] = 'undefined';
}
if (args[1][k] === null) {
console.warn("_t called with null interpolation name: " + k);
args[1][k] = 'null';
}
});
}
return counterpart.translate(...args);
}