Allow more than 2 variable substitutions in the translation function

This commit is contained in:
Alexander Tseung 2018-08-31 12:12:12 -07:00
parent 31089778ba
commit e5ca2aac6f
1 changed files with 4 additions and 4 deletions

View File

@ -20,10 +20,10 @@ const getMessage = (locale, key, substitutions) => {
let phrase = entry.message
// perform substitutions
if (substitutions && substitutions.length) {
phrase = phrase.replace(/\$1/g, substitutions[0])
if (substitutions.length > 1) {
phrase = phrase.replace(/\$2/g, substitutions[1])
}
substitutions.forEach((substitution, index) => {
const regex = new RegExp(`\\$${index + 1}`, 'g')
phrase = phrase.replace(regex, substitution)
})
}
return phrase
}