Default value for lang.Translate function

This is a common, or at least useful, construct:

{{ or (T "word") "default" }}

Meaning, lookup the translation for “word”, and if there isn’t one, use “default” instead.This works great, but when you run hugo --printI18nWarnings you get:

WARN  i18n|MISSING_TRANSLATION|en|word
WARN  i18n|MISSING_TRANSLATION|de|word

In the construct above, we don’t care that a translation does not exist… we’ve already handled that with a default value.

The current signature is:

lang.Translate KEY [CONTEXT]

Wondering about something like…

lang.Translate KEY [CONTEXT] [DEFAULT]

That’s a bit wonky, because (theoretically) someone might want to pass a string as CONTEXT, though usually CONTEXT will be float, integer, or map. We could take the risk:

  • If there are three args the last is the default, and the second must be an integer, float, or map.
  • If there are two args, and the second is a string, it’s the default. Although unlikely, this could break some sites, but I have no data to backup the risk estimate.

We can get the desired behavior by (a) mounting i18n to data and (b) creating a partial that uses site.Data to check for key existence before calling lang.Translate. But the translation call isn’t nearly as elegant as {{ T "word" "default" }} or {{ T "word" 42 "default" }}.

If we do this, both of the following should ignore cases where a default value is specified:

  1. --printI18nWarnings (must have)
  2. enableMissingTranslationPlaceholders = true (nice to have)
2 Likes

That feature would be great!

I recently removed those warnings from my theme by a not very elegant workaround adding 6 lines of code for each occurance :frowning:

1 Like

I’ve opened an issue; comments welcome:
https://github.com/gohugoio/hugo/issues/11782