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

Skip to content

Commit 633cc77

Browse files
jmooringclaude
authored andcommitted
langs/i18n: Improve default content language fallback
The fallback order for translations is now: 1. Current language's locale (e.g., pt-BR → pt-br.toml) 2. Current language's key (e.g., pt → pt.toml) 3. Default language's locale (e.g., es-AR → es-ar.toml) ← new 4. Default language's key (e.g., es → es.toml) Closes #14243 Co-authored-by: Claude Sonnet 4.6 <[email protected]>
1 parent 90d8bf3 commit 633cc77

3 files changed

Lines changed: 73 additions & 8 deletions

File tree

hugolib/menu_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ menu: main
567567

568568
b.AssertFileContent("public/en/index.html", `<a href="/en/p1/">p1</a><a href="/en/p2/">p2</a>`)
569569
b.AssertFileContent("public/fr/index.html", `<a href="/fr/p1/">p1</a>`)
570-
b.AssertLogContains("! WARN")
570+
b.AssertLogContains("! duplicate menu entry")
571571
}
572572

573573
func TestSectionPagesIssue12399(t *testing.T) {

langs/i18n/translationProvider.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/gohugoio/hugo/common/paths"
24+
"github.com/gohugoio/hugo/config"
2425
"github.com/gohugoio/hugo/langs"
2526
"github.com/gohugoio/hugo/parser/metadecoders"
2627

@@ -150,17 +151,29 @@ func (tp *TranslationProvider) CloneResource(dst, src *deps.Deps) error {
150151
return nil
151152
}
152153

154+
func defaultLanguage(conf config.AllProvider) *langs.Language {
155+
key := conf.DefaultContentLanguage()
156+
for _, l := range conf.Languages().(langs.Languages) {
157+
if l.Lang == key {
158+
return l
159+
}
160+
}
161+
return conf.Language().(*langs.Language)
162+
}
163+
153164
// getTranslateFunc returns the translation function for the language in Deps.
154-
// We first try the locale (e.g. "en-US"), then the language key (e.g. "en").
165+
// The lookup order is: current locale, current key, default locale, default key.
155166
func (tp *TranslationProvider) getTranslateFunc(dst *deps.Deps) func(ctx context.Context, translationID string, templateData any) string {
156-
l := dst.Conf.Language().(*langs.Language)
157-
if locale := l.Locale(); locale != "" {
158-
if fn, ok := tp.t.Lookup(strings.ToLower(locale)); ok {
159-
return fn
167+
current := dst.Conf.Language().(*langs.Language)
168+
defaultLang := defaultLanguage(dst.Conf)
169+
for _, l := range []*langs.Language{current, defaultLang} {
170+
for _, key := range []string{strings.ToLower(l.Locale()), l.Lang} {
171+
if fn, ok := tp.t.Lookup(key); ok {
172+
return fn
173+
}
160174
}
161175
}
162-
// Func will fall back to the default language if not found.
163-
return tp.t.Func(l.Lang)
176+
return tp.t.Func("en")
164177
}
165178

166179
func errWithFileContext(inerr error, r *source.File) error {

langs/languages_integration_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,55 @@ language: {{ site.Language.Name }} file: {{ T "file" }}|
326326
b.AssertFileContent("public/de/index.html", "language: de file: de|")
327327
b.AssertFileContent("public/de-de/index.html", "language: de-de file: de-de|")
328328
}
329+
330+
func TestDefaultContentLanguageFallback14243(t *testing.T) {
331+
t.Parallel()
332+
333+
files := `
334+
-- hugo.toml --
335+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
336+
defaultContentLanguage = 'es'
337+
defaultContentLanguageInSubdir = true
338+
339+
[languages.es]
340+
locale = 'es-AR'
341+
weight = 1
342+
343+
[languages.pt]
344+
locale = 'pt-BR'
345+
weight = 2
346+
-- layouts/home.html --
347+
{{ T "foo"}}|
348+
-- i18n/es-ar.toml --
349+
foo = 'foo es-ar'
350+
-- i18n/es.toml --
351+
foo = 'foo es'
352+
-- i18n/pt-br.toml --
353+
foo = 'foo pt-br'
354+
-- i18n/pt.toml --
355+
foo = 'foo pt'
356+
`
357+
358+
b := hugolib.Test(t, files)
359+
b.AssertFileContent("public/pt/index.html", "foo pt-br|")
360+
361+
files = strings.ReplaceAll(files, "i18n/pt-br.toml", "unused-a.toml")
362+
363+
b = hugolib.Test(t, files)
364+
b.AssertFileContent("public/pt/index.html", "foo pt|")
365+
366+
files = strings.ReplaceAll(files, "i18n/pt.toml", "unused-b.toml")
367+
368+
b = hugolib.Test(t, files)
369+
b.AssertFileContent("public/pt/index.html", "foo es-ar|")
370+
371+
files = strings.ReplaceAll(files, "i18n/es-ar.toml", "unused-c.toml")
372+
373+
b = hugolib.Test(t, files)
374+
b.AssertFileContent("public/pt/index.html", "foo es|")
375+
376+
files = strings.ReplaceAll(files, "i18n/es.toml", "unused-d.toml")
377+
378+
b = hugolib.Test(t, files)
379+
b.AssertFileContent("public/pt/index.html", "|")
380+
}

0 commit comments

Comments
 (0)