Possible undocumented i18n feature. Worth updating the docs?

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.

I’m surprised that isn’t in the docs.

  • You can pass any data as the second argument to i18n
  • If what you pass is a number, then we also use that to determine the plural state (0, 1, many)
  • If it’s a map/struct, we also look for a .Count/.count field/entry to determine the plural state
2 Likes

Thanks! I’ll go ahead and see about making a PR to add this info.

Edit: Upon further research I eventually discovered that this functionality is documented. However, it’s buried deep in the multi language article, where most people looking for a reference about how the specific i18n function works wouldn’t find it. So I’m still going to make a PR to add that info to the function reference article.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.