Skip to content

Add optional default value to language lang.Translate #11782

Closed
@jmooring

Description

@jmooring

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.
  • 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)

Related:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions