diff --git a/src/languageHandler.js b/src/languageHandler.js index 798798b6e5..68e8ca6172 100644 --- a/src/languageHandler.js +++ b/src/languageHandler.js @@ -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); }