Skip to content

Commit

Permalink
langs/i18n: Fallback to defaultContentLanguage instead of English
Browse files Browse the repository at this point in the history
Co-authored-by: 641bill <wo23636@126.com>

Fixes #9216
  • Loading branch information
jmooring authored and bep committed Apr 24, 2023
1 parent f106251 commit 0cb6ca5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
38 changes: 38 additions & 0 deletions langs/i18n/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}
7 changes: 6 additions & 1 deletion langs/i18n/translationProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0cb6ca5

Please sign in to comment.