Fix it so languageCode on top level config still works

This is common for monolingual sites, and we broke this in Hugo 0.112.4.

Fixes #11037
This commit is contained in:
Bjørn Erik Pedersen 2023-05-28 18:42:10 +02:00
parent cd59216de4
commit e3dfc76fa8
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 40 additions and 2 deletions

View file

@ -241,8 +241,24 @@ var allDecoderSetups = map[string]decodeWeight{
key: "languages",
decode: func(d decodeWeight, p decodeConfig) error {
var err error
p.c.Languages, err = langs.DecodeConfig(p.p.GetStringMap(d.key))
return err
m := p.p.GetStringMap(d.key)
if len(m) == 1 {
// In v0.112.4 we moved this to the language config, but it's very commmon for mono language sites to have this at the top level.
var first maps.Params
for _, v := range m {
first = v.(maps.Params)
break
}
if _, found := first["languagecode"]; !found {
first["languagecode"] = p.p.GetString("languagecode")
}
}
p.c.Languages, err = langs.DecodeConfig(m)
if err != nil {
return err
}
return nil
},
},
"cascade": {

View file

@ -1044,3 +1044,25 @@ HTACCESS.
b.AssertFileContent("public/.htaccess", "HTACCESS")
}
func TestConfigLanguageCodeTopLevel(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
languageCode = "en-US"
-- layouts/index.html --
LanguageCode: {{ .Site.LanguageCode }}|{{ site.Language.LanguageCode }}|
`
b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()
b.AssertFileContent("public/index.html", "LanguageCode: en-US|en-US|")
}