Interpolation allows dynamic values to be used within translations. Interpolation is used where a value is uncertain, dynamic or susceptible to change. It is also used with pluralisation.
How to use interpolation
In the JSON file, we use {{ }} brackets to indicate where we will be using interpolation. This value will change depending on our provided value in the translation hook.
Code Block | ||
---|---|---|
| ||
user_pet: "I have a pet {{animal}}" |
Our code where the translation key is rendered:
Code Block | ||
---|---|---|
| ||
t('user_pet', {animal: "cat"}) |
The code will now appear as:
Code Block | ||
---|---|---|
| ||
"I have a pet cat" |
Implications
Interpolation can cause issues when concatenating values. Depending on the interpolated value, the rest of the sentence may need to be changed, this can be seen here in the documentation. Interpolation should be used as little as possible. It can not be avoided in cases of:
Time stamps
User-inputted data
More about the implications and best practices of interpolation can be seen here.