Using Mailgun's Handlebars implementation I'd like to have multi-language templates. I.e. wrap each text block in a condition and display the language passed as parameter.
Now for the crucial part: If the language
parameter is missing or has a value not supported (i.e. no specified in the template) then an English string should be displayed as fallback language.
If Mailgun were to support custom helpers you could define a helper according to this SO answer like Handlebars.registerHelper('eq', (a, b) => a == b)
And then do:
{{#if (eq language "sv")}}Hej
{{else if (eq language "de")}}Hallo
{{else}}Hello
{{/if}}
However, as far as I know, there is no way to define own helpers in Mailgun.
So, how would I do this without extra helpers?
I know that I could, theoretically, transform the input to booleans and then do something like
{{#if isSwedish}}Hej
{{else if isGerman}}Hallo
{{else}}Hello
{{/if}}
But that would be very inconvenient for me and I would rather prefer to pass a string property language
.
The Mailgun support told me that it must be somehow possible in Handlebars but refused to tell me how exactly.
The Mailgun API docs mentions an equal
block helper which looks to behave just like the eq
helper you suggested in your post. Therefore, I would assume the following template would achieve your objective:
{{~#equal language "sv"~}}
Hej
{{~else equal language "de"~}}
Hallo
{{~else~}}
Hello
{{~/equal~}}
Note: I am using ~
characters to eliminate extra whitespace caused by the indentations in the template for readability. See: https://stackoverflow.com/a/23636497/3397771