I have figured this out, please see my own reply for a followup question.
Hi guys,
I haven’t yet been able to find a good way of doing this just yet: We’re looking at potentially replacing our in-house static site generator with Hugo. Our website is translated into roughly 20 languages across about 8 different templates. Most of them are similar which we could bridge with partials, but my question is right now we use json files to handle translations. What could be a good way of doing translations that populate templates? Front Matter?
Alright, I’ve figured out how to use Front Matter as a way of handling translations setting up an array like this:
[translation]
title = "some title"
text = "some text"
and simply referencing {{ .Params.translation.title }} in my templates.
The new question I have is is there a way without doing isset functions in my templates to output the variable if it doesn’t exist? So in the translation example given the following example:
Obviously I do not have category defined. Is there anyway in Hugo to have it literally render {{ .Params.translation.category }} to the screen? Reason being is we will often have around 150 translations for a page and it’s a lot easier to see something is missing if default text is thrown in there rather than blanking it out entirely.
Now I’m aware that I can do something like the following but it’s incredibly verbose:
{{ if isset .Params.translation "category" }}{{ .Params.translation.category }}{{ else }}<!- category ->{{ end }}
I believe what he REALLY wants is a variation of echoParam:
{{echoParam .Params “translation.category” }}
But instead of returning a blank String, return “<! - translation.category ->” – but that would need another template function. Wouldn’t be hard to implement, I guess.
What is the difference between {{ echoParam .Params "translation.category" }} and {{ .Params.translation.category }} ? In testing they both appear to behave in the same way.
The with option is definitely less verbose than doing issets everywhere and I might be willing to roll with that solution.