N
Novu4mo ago
Dima_hat

Using nested translation variables

We want to create some complex notification massages that require to have nested translated messages/variables. We want to have two translations of same message: "{{firstName}} has changed the status to {{newStatus}}." (English) "{{firstName}} hat den Status auf {{newStatus}} geändert." (German) And here we have a problem of properly setting text for "newStatus". I haven't found a way to store possible statuses as different texts in same i18n file and to reference them in any way Setting such in-app text
{{i18n "conversations.inboxChange" firstName=actor.firstName newStatus={{i18n "conversations.status" context=statusContext}} }}
{{i18n "conversations.inboxChange" firstName=actor.firstName newStatus={{i18n "conversations.status" context=statusContext}} }}
Results in error "Failed to extract variables" Without putting newStatus variable - everything works as expected. Is it possible to somehow use i18n messages in another message? Or what would be your advice regarding such messages?
10 Replies
Tomer Barnea
Tomer Barnea4mo ago
Hey @Dima_hat 👋, let me add here @Pawan Jain to help us 🙂
Gali Baum
Gali Baum4mo ago
Hi @Dima_hat You could try something like this: In your conversations translation file:
{
"newStatus": "bla bla {{statusVar}}",
"inboxChange": "{{firstName}} has changed the status to $t(conversations.newStatus)."
}
{
"newStatus": "bla bla {{statusVar}}",
"inboxChange": "{{firstName}} has changed the status to $t(conversations.newStatus)."
}
and then in the editor you would use it like this:
{{i18n "conversations.inboxChange" firstName=actor.firstName statusVar="busy" }}
{{i18n "conversations.inboxChange" firstName=actor.firstName statusVar="busy" }}
is that what you were looking for?
Dima_hat
Dima_hatOP4mo ago
Hey hey, Thanks for reply. Unfortunately not exactly. For statuses it's also several different translated strings that we want to store in novu, so we need to provide which status exactly should be taken from the translation file
{
"status1": "Todo",
"status2": "In progress",
"inboxChange": "{{firstName}} has changed the status to $t(conversations.status1 || status2)."
}
{
"status1": "Todo",
"status2": "In progress",
"inboxChange": "{{firstName}} has changed the status to $t(conversations.status1 || status2)."
}
And which one of two statuses to take - we need to provide also as payload variable Is that possible to perform somehow?
Gali Baum
Gali Baum4mo ago
{
"status": "status",
"status_busy": "busy",
"status_progress": "progress",
"welcome": "hello {{name}}, your status is: $t(login.status)"
}
{
"status": "status",
"status_busy": "busy",
"status_progress": "progress",
"welcome": "hello {{name}}, your status is: $t(login.status)"
}
in editor:
{{i18n "login.welcome" name="Gali" context=payloadStatus}}
{{i18n "login.welcome" name="Gali" context=payloadStatus}}
and in pauload you would add
{
payloadStatus: 'busy'
}
{
payloadStatus: 'busy'
}
How is that @Dima_hat ? 🙂
Dima_hat
Dima_hatOP4mo ago
Thanks! That's already helps a lot! Just trying to understand regarding possibilities Is it possible to have two different separate contexts? That in translation file we have
{
"status": "status",
"status_busy": "busy",
"status_progress": "progress",
"welcome_formal": "Hi {{name}}, your status is: $t(login.status)",
"welcome_informal": "Welcome {{name}}, your status is: $t(login.status)"
}
{
"status": "status",
"status_busy": "busy",
"status_progress": "progress",
"welcome_formal": "Hi {{name}}, your status is: $t(login.status)",
"welcome_informal": "Welcome {{name}}, your status is: $t(login.status)"
}
And to somehow pack both different busy | progress and formal | informal contexts together Looks like it's impossible with such approach
{{i18n "login.welcome" name="Gali" context=payloadStatus}}
{{i18n "login.welcome" name="Gali" context=payloadStatus}}
And there can be only one context?
Gali Baum
Gali Baum4mo ago
From what I can tell multiple contexts are not really supported by i18next. But, you can dig a bit deeper if they have a work around. Maybe this example could be a good lead (involving a var + context )
Gali Baum
Gali Baum4mo ago
We are using i18next, so what most of what they allow will work in novu except maybe some formats, that might need some adjusting
Gali Baum
Gali Baum4mo ago
But notice that according to them best practice is to separate to fully self-contained strings
Dima_hat
Dima_hatOP4mo ago
Thanks for the docs, will definitely have a look One last question - it's impossible to pass a variable on which string from the translations file should be taken So having in editor:
{{i18n "login.{{textToTake}}"}}
{{i18n "login.{{textToTake}}"}}
doesn't work 😦 . Probably I'm getting the templating wrong or it's really not possible It will also provide possibility to decide what message should be taken by the payload, but without exploiting contexts
Novu_Bot
Novu_Bot4mo ago
@Dima_hat, you just advanced to level 3!
Want results from more Discord servers?
Add your server