i18n Attaching variable in the middle of a string

18 Views Asked by At

I have this phrase :

{{ __("counter.description:This promotion is limited to X participants") }}

I want to change the X by a variable

My key is counter.description

And I tried to do this in my singular value : This promotion is limited to {{totalParticipation}} participants

But it's not working.

Di you have any idea on how to do this ?

1

There are 1 best solutions below

0
Tom Boutell On

This is the right way to get the result you are looking for:

  1. Insert the phrase in your page template this way. Note that the key name has no periods in it. Also note that the interpolated variable is passed as part of an object.
{{ __t('promotionDescription', { n: 5 }) }}
  1. Add the phrase to an i18n/en.json file inside any module of your Apostrophe project. Important: this must be in a subdirectory called i18n inside your module. Otherwise it will not be found and loaded for you.
{
  "promotionDescription": "This promotion is limited to {{ n }} participants"
}
  1. When you're ready to actually translate, copy en.json to fr.json, es.json, etc. (or whatever your configured locales are) in the same directory and translate the contents, keeping the key names and interpolated variable names the same, just translate the text.

  2. Don't forget to actually configure those same locales in the @apostrophecms/i18n module, otherwise they won't be picked up.

See documentation links below:

https://docs.apostrophecms.org/guide/localization/static.html https://docs.apostrophecms.org/guide/localization/overview.html#configuring-locales