Merge branch 'develop' into adjust-polling

This commit is contained in:
Daniel Ternyak 2018-07-06 18:40:55 -05:00 committed by GitHub
commit e472b15491
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 =
(repository[language] && repository[language][key]) || repository[fallbackLanguage][key] || key;
if (!!variables) {
// Find each variable and replace it in the translated string
let str = translatedString;
Object.keys(variables).forEach(v => {
const re = new RegExp(v.replace('$', '\\$'), 'g');
str = str.replace(re, variables[v]);
/** @desc In RegExp, $foo is two "words", but __foo is only one "word."
* Replace all occurences of '$' with '__' in the entire string and each variable,
* then iterate over each variable, replacing the '__variable' in the
* translation key with the variable's value.
*/
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;
}