From 88a7a05984619de598ddd12b0808b87ee19eb94a Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Sat, 22 Apr 2023 15:58:44 -0700 Subject: [PATCH] langs/i18n: Fallback to defaultContentLanguage instead of English Co-authored-by: 641bill Fixes #9216 --- langs/i18n/integration_test.go | 38 +++++++++++++++++++++++++++++++ langs/i18n/translationProvider.go | 7 +++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/langs/i18n/integration_test.go b/langs/i18n/integration_test.go index a433fc4cdfc..c010ac1113b 100644 --- a/langs/i18n/integration_test.go +++ b/langs/i18n/integration_test.go @@ -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`) +} diff --git a/langs/i18n/translationProvider.go b/langs/i18n/translationProvider.go index d9d334567f9..782bbf719fb 100644 --- a/langs/i18n/translationProvider.go +++ b/langs/i18n/translationProvider.go @@ -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)