Run translation substitution in 2 passes

By first substituting variables and then tags after, the translation handling
can now support strings with variables inside tags, such as:

"people <span>reacted with %(foo)s</span>"
pull/21833/head
J. Ryan Stinnett 2019-05-17 11:53:44 +01:00
parent c48ecb66d9
commit 32c68feae2
1 changed files with 7 additions and 2 deletions

View File

@ -125,20 +125,25 @@ export function _t(text, variables, tags) {
* @return a React <span> component if any non-strings were used in substitutions, otherwise a string
*/
export function substitute(text, variables, tags) {
const regexpMapping = {};
let result = text;
if (variables !== undefined) {
const regexpMapping = {};
for (const variable in variables) {
regexpMapping[`%\\(${variable}\\)s`] = variables[variable];
}
result = replaceByRegexes(result, regexpMapping);
}
if (tags !== undefined) {
const regexpMapping = {};
for (const tag in tags) {
regexpMapping[`(<${tag}>(.*?)<\\/${tag}>|<${tag}>|<${tag}\\s*\\/>)`] = tags[tag];
}
result = replaceByRegexes(result, regexpMapping);
}
return replaceByRegexes(text, regexpMapping);
return result;
}
/*