"context"
"encoding/json"
"fmt"
+ "sort"
"strings"
"github.com/gohugoio/hugo/common/paths"
bundle.RegisterUnmarshalFunc("yml", metadecoders.UnmarshalYaml)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
+ var files []hugofs.FileMetaInfo
w := hugofs.NewWalkway(
hugofs.WalkwayConfig{
Fs: dst.BaseFs.I18n.Fs,
if info.IsDir() {
return nil
}
- return addTranslationFile(bundle, source.NewFileInfo(info))
+ files = append(files, info)
+ return nil
},
})
return err
}
+ // Sort translation files so that base tags (e.g. "de") are always registered
+ // before their subtag variants (e.g. "de-DE"); without this ordering the
+ // fallback chain breaks. See https://github.com/gohugoio/hugo/issues/7982.
+ sort.Slice(files, func(i, j int) bool {
+ si := files[i].Meta().PathInfo.NameNoExt()
+ sj := files[j].Meta().PathInfo.NameNoExt()
+ return strings.Count(si, "-") < strings.Count(sj, "-")
+ })
+
+ for _, info := range files {
+ if err := addTranslationFile(bundle, source.NewFileInfo(info)); err != nil {
+ return err
+ }
+ }
+
tp.t = NewTranslator(bundle, dst.Conf, dst.Log)
dst.Translate = tp.getTranslateFunc(dst)
`FormatCurrency: US$512.50|`,
)
}
+
+func TestMultipleLanguageVariants7982(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ['page','section','rss','sitemap','taxonomy','term']
+defaultContentLanguageInSubdir = true
+[languages.en]
+weight = 1
+[languages.de]
+weight = 2
+[languages.de-de]
+weight = 3
+-- i18n/en.toml --
+file = 'en'
+-- i18n/de.toml --
+file = 'de'
+-- i18n/de-de.toml --
+file = 'de-de'
+-- layouts/index.html --
+language: {{ site.Language.Name }} file: {{ T "file" }}|
+`
+
+ b := hugolib.Test(t, files)
+
+ b.AssertFileContent("public/en/index.html", "language: en file: en|")
+ b.AssertFileContent("public/de/index.html", "language: de file: de|")
+ b.AssertFileContent("public/de-de/index.html", "language: de-de file: de-de|")
+}