From 310457059b32b0503c8575d8a6093599000e4ade Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 22 Aug 2019 18:31:02 +0100 Subject: [PATCH] [i18n] only append tail if it is actually needed Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/languageHandler.js | 12 ++++++++++-- test/i18n-test/languageHandler-test.js | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/languageHandler.js b/src/languageHandler.js index 9e354cee9e..e5656e5f69 100644 --- a/src/languageHandler.js +++ b/src/languageHandler.js @@ -177,6 +177,10 @@ export function replaceByRegexes(text, mapping) { // If we insert any components we need to wrap the output in a span. React doesn't like just an array of components. let shouldWrapInSpan = false; + if (text === "You are now ignoring %(userId)s") { + debugger; + } + for (const regexpString in mapping) { // TODO: Cache regexps const regexp = new RegExp(regexpString, "g"); @@ -233,11 +237,15 @@ export function replaceByRegexes(text, mapping) { // add the text between prevMatch and this one // or the end of the string if prevMatch is the last match + let tail; if (match) { const startIndex = prevMatch.index + prevMatch[0].length; - parts.push(inputText.substr(startIndex, match.index - startIndex)); + tail = inputText.substr(startIndex, match.index - startIndex); } else { - parts.push(inputText.substr(prevMatch.index + prevMatch[0].length)); + tail = inputText.substr(prevMatch.index + prevMatch[0].length); + } + if (tail) { + parts.push(tail); } } diff --git a/test/i18n-test/languageHandler-test.js b/test/i18n-test/languageHandler-test.js index 07e3f2cb8b..0d96bc15ab 100644 --- a/test/i18n-test/languageHandler-test.js +++ b/test/i18n-test/languageHandler-test.js @@ -73,12 +73,12 @@ describe('languageHandler', function() { it('multiple replacements of the same variable', function() { const text = '%(var1)s %(var1)s'; - expect(languageHandler._t(text, { var1: 'val1' })).toBe('val1 val1'); + expect(languageHandler.substitute(text, { var1: 'val1' })).toBe('val1 val1'); }); it('multiple replacements of the same tag', function() { const text = 'Click here to join the discussion! or here'; - expect(languageHandler._t(text, {}, { 'a': (sub) => `x${sub}x` })) + expect(languageHandler.substitute(text, {}, { 'a': (sub) => `x${sub}x` })) .toBe('xClick herex to join the discussion! xor herex'); }); });