Made changes to translateRaw
This commit is contained in:
parent
06c8ae6e5b
commit
94c8aa7c6f
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue