Skip to content

Artificial language codes longer than 8 alphanumeric characters break i18n #9119

@fnordfish

Description

@fnordfish

When using a language with a custom key including a slash the generated translate function keys won't match the i18n translation files.

According to the documentation:

From Hugo 0.31 you no longer need to use a valid language code. It can be anything.
#3564

When using a language config like this:

languages:
  fragen:
    languageName: Deutsch
    # ....
  en/questions:
    languageName: English
    # ....

The generated translate function will use the i18n translation file names as keys.
But executing the translation, the lookup key will be using the original languages key ("fragen" and "en/questions" respectively)
Since I cannot name the translation files including a slash, I tried replacing it with a colon, but had no success.

For me this patch works, but it is hardly a generic solution

--- a/langs/i18n/i18n.go
+++ b/langs/i18n/i18n.go
@@ -49,6 +49,7 @@ func NewTranslator(b *i18n.Bundle, cfg config.Provider, logger loggers.Logger) T
 // Func gets the translate func for the given language, or for the default
 // configured language if not found.
 func (t Translator) Func(lang string) translateFunc {
+       lang = strings.SplitN(lang, "/", 2)[0]
        if f, ok := t.translateFuncs[lang]; ok {
                return f
        }

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.89.0-ADE966B8 linux/amd64 BuildDate=2021-11-02T10:00:18Z VendorInfo=gohugoio

Does this issue reproduce with the latest release?

Yes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions