If you use the LinkedIn fork of Dust, you can simply do {text[locale]}
. I tested this in the LinkedIn playground here.
Use of dust.js parameters to set locale for strings in Json
-
01-07-2022 - |
Question
I have the following json structure:
{
locale : "en",
text {
"en": "Hello",
"fr": "Bonjour"
}
}
I want to pick up the locale in my template and use it when accessing the text. I thought something like this might work:
{#text foo=locale}
{text.foo}
{/text}
But I get nothing.
Any ideas? Thanks.
Solution
OTHER TIPS
Managed to solve it in the end with a helper function.
Helper function looks something like this:
dust.helpers.locale = function(chunk, ctx, bodies, params){
var locale = params.loc;
var text = params.txt;
return chunk.write(text[locale]);
}
I can then call it from the template like so:
{@locale loc=locale txt=text /}
Not sure if there is a way of doing this without the helper but this seems to work fine.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow