Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72b85d5

Browse files
jmooringclaude
authored andcommitted
langs/i18n: Fix translation lookup when using language variants
Closes #7982 Co-authored-by: Claude Sonnet 4.6 <[email protected]>
1 parent 75f6183 commit 72b85d5

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

langs/i18n/translationProvider.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"context"
1818
"encoding/json"
1919
"fmt"
20+
"sort"
2021
"strings"
2122

2223
"github.com/gohugoio/hugo/common/paths"
@@ -59,6 +60,7 @@ func (tp *TranslationProvider) NewResource(dst *deps.Deps) error {
5960
bundle.RegisterUnmarshalFunc("yml", metadecoders.UnmarshalYaml)
6061
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
6162

63+
var files []hugofs.FileMetaInfo
6264
w := hugofs.NewWalkway(
6365
hugofs.WalkwayConfig{
6466
Fs: dst.BaseFs.I18n.Fs,
@@ -68,14 +70,30 @@ func (tp *TranslationProvider) NewResource(dst *deps.Deps) error {
6870
if info.IsDir() {
6971
return nil
7072
}
71-
return addTranslationFile(bundle, source.NewFileInfo(info))
73+
files = append(files, info)
74+
return nil
7275
},
7376
})
7477

7578
if err := w.Walk(); err != nil {
7679
return err
7780
}
7881

82+
// Sort translation files so that base tags (e.g. "de") are always registered
83+
// before their subtag variants (e.g. "de-DE"); without this ordering the
84+
// fallback chain breaks. See https://github.com/gohugoio/hugo/issues/7982.
85+
sort.Slice(files, func(i, j int) bool {
86+
si := files[i].Meta().PathInfo.NameNoExt()
87+
sj := files[j].Meta().PathInfo.NameNoExt()
88+
return strings.Count(si, "-") < strings.Count(sj, "-")
89+
})
90+
91+
for _, info := range files {
92+
if err := addTranslationFile(bundle, source.NewFileInfo(info)); err != nil {
93+
return err
94+
}
95+
}
96+
7997
tp.t = NewTranslator(bundle, dst.Conf, dst.Log)
8098

8199
dst.Translate = tp.getTranslateFunc(dst)

langs/languages_integration_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,33 @@ FormatCurrency: {{ 512.5032 | lang.FormatCurrency 2 "USD" }}|
296296
`FormatCurrency: US$512.50|`,
297297
)
298298
}
299+
300+
func TestMultipleLanguageVariants7982(t *testing.T) {
301+
t.Parallel()
302+
303+
files := `
304+
-- hugo.toml --
305+
disableKinds = ['page','section','rss','sitemap','taxonomy','term']
306+
defaultContentLanguageInSubdir = true
307+
[languages.en]
308+
weight = 1
309+
[languages.de]
310+
weight = 2
311+
[languages.de-de]
312+
weight = 3
313+
-- i18n/en.toml --
314+
file = 'en'
315+
-- i18n/de.toml --
316+
file = 'de'
317+
-- i18n/de-de.toml --
318+
file = 'de-de'
319+
-- layouts/index.html --
320+
language: {{ site.Language.Name }} file: {{ T "file" }}|
321+
`
322+
323+
b := hugolib.Test(t, files)
324+
325+
b.AssertFileContent("public/en/index.html", "language: en file: en|")
326+
b.AssertFileContent("public/de/index.html", "language: de file: de|")
327+
b.AssertFileContent("public/de-de/index.html", "language: de-de file: de-de|")
328+
}

0 commit comments

Comments
 (0)