Made changes to translateRaw

This commit is contained in:
Connor Bryan 2018-07-06 18:22:12 -05:00
parent 06c8ae6e5b
commit 94c8aa7c6f
1 changed files with 14 additions and 6 deletions

View File

@ -67,13 +67,21 @@ export function translateRaw(key: string, variables?: { [name: string]: string }
const translatedString = const translatedString =
(repository[language] && repository[language][key]) || repository[fallbackLanguage][key] || key; (repository[language] && repository[language][key]) || repository[fallbackLanguage][key] || key;
if (!!variables) { /** @desc In RegExp, $foo is two "words", but __foo is only one "word."
// Find each variable and replace it in the translated string * Replace all occurences of '$' with '__' in the entire string and each variable,
let str = translatedString; * then iterate over each variable, replacing the '__variable' in the
Object.keys(variables).forEach(v => { * translation key with the variable's value.
const re = new RegExp(v.replace('$', '\\$'), 'g'); */
str = str.replace(re, variables[v]); if (variables) {
let str = translatedString.replace(/\$/g, '__');
Object.keys(variables).forEach(variable => {
const singleWordVariable = variable.replace(/\$/g, '__');
const re = new RegExp(`\\b${singleWordVariable}\\b`, 'g');
str = str.replace(re, variables[variable]);
}); });
return str; return str;
} }