I noticed an interesting thing that another dev who works on the same website as me dropped into our code. I initially assumed it must be junk since it’s not mentioned at all in the docs, but after seeing that it worked I actually think it’s an undocumented feature of the i18n
function.
I was thinking of making a PR to the Hugo Docs to add info about it to the i18n
article, but after reading about what sorts of PRs are accepted, I’m a bit unsure if this is the kind of thing that should be documented, or if it would be considered “not a best practice” and therefore should remain undocumented.
The feature is the ability to put Hugo tags within i18n
entries and then pass a dictionary of replacement text as the second argument to i18n
. Here is the working example from our code base to demonstrate:
Simplified version of our code as it appears in the template partial:
<img alt="{{- ( i18n "name_talks_about_gh_accelerator" (dict "Name" .slide.alt)) -}}">
Relevant entry in translation file:
[name_talks_about_gh_accelerator]
other = "{{ .Name }} talks about the GH Accelerator"
And on the site I can see that the text outputted to our HTML is inserting the value of .slide.alt
in place of {{ .Name }}
in the translated text. EG (if `.slide.alt === “Bob Johnson”):
<img alt="Bob Johnson talks about the GH Accelerator">
Please let me know what y’all think about this.