langs/i18n: Fallback to defaultContentLanguage instead of English

Co-authored-by: 641bill <wo23636@126.com>

Fixes #9216
This commit is contained in:
Joe Mooring 2023-04-22 15:58:44 -07:00 committed by Bjørn Erik Pedersen
parent f1062519ae
commit 0cb6ca5906
2 changed files with 44 additions and 1 deletions

View file

@ -103,3 +103,41 @@ i18n: {{ i18n "a" . }}|
i18n: Reading time: 3|
`)
}
// Issue 9216
func TestI18nDefaultContentLanguage(t *testing.T) {
t.Parallel()
files := `
-- config.toml --
disableKinds = ['RSS','sitemap','taxonomy','term','page','section']
defaultContentLanguage = 'es'
defaultContentLanguageInSubdir = true
[languages.es]
[languages.fr]
-- i18n/es.toml --
cat = 'gato'
-- i18n/fr.toml --
# this file intentionally empty
-- layouts/index.html --
{{ .Title }}_{{ T "cat" }}
-- content/_index.fr.md --
---
title: home_fr
---
-- content/_index.md --
---
title: home_es
---
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()
b.AssertFileContent("public/es/index.html", `home_es_gato`)
b.AssertFileContent("public/fr/index.html", `home_fr_gato`)
}

View file

@ -48,7 +48,12 @@ func NewTranslationProvider() *TranslationProvider {
func (tp *TranslationProvider) Update(d *deps.Deps) error {
spec := source.NewSourceSpec(d.PathSpec, nil, nil)
bundle := i18n.NewBundle(language.English)
var defaultLangTag, err = language.Parse(d.Cfg.GetString("defaultContentLanguage"))
if err != nil {
defaultLangTag = language.English
}
bundle := i18n.NewBundle(defaultLangTag)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
bundle.RegisterUnmarshalFunc("yml", yaml.Unmarshal)