Deprecate site.Language.Params and some other fixes

Updates #10947
This commit is contained in:
Bjørn Erik Pedersen 2023-05-17 09:59:57 +02:00
parent 0106cf1a6d
commit 5d857165fe
5 changed files with 121 additions and 5 deletions

View file

@ -742,19 +742,35 @@ themeconfigdirparam: {{ site.Params.themeconfigdirparam }}
}
// TODO(beo) find a better place for this.
func TestReproCommentsIn10947(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
baseURL = "https://example.com"
-- content/mysection/_index.md --
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
[languages]
[languages.en]
title = "English Title"
[languages.en.params]
myparam = "enParamValue"
[languages.sv]
title = "Svensk Title"
[languages.sv.params]
myparam = "svParamValue"
-- content/mysection/_index.en.md --
---
title: "My Section"
title: "My English Section"
---
-- content/mysection/_index.sv.md --
---
title: "My Swedish Section"
---
-- layouts/index.html --
Sections: {{ if site.Sections }}true{{ end }}|
{{ range $i, $e := (slice site .Site) }}
{{ $i }}|AllPages: {{ len .AllPages }}|Sections: {{ if .Sections }}true{{ end }}| Author: {{ .Authors }}|BuildDrafts: {{ .BuildDrafts }}|IsMultiLingual: {{ .IsMultiLingual }}|Param: {{ .Language.Params.myparam }}|
{{ end }}
`
@ -765,6 +781,18 @@ Sections: {{ if site.Sections }}true{{ end }}|
},
).Build()
b.AssertFileContent("public/index.html", "Sections: true|")
b.Assert(b.H.Log.LogCounters().WarnCounter.Count(), qt.Equals, uint64(2))
b.AssertFileContent("public/index.html", `
AllPages: 4|
Sections: true|
Param: enParamValue
Param: enParamValue
IsMultiLingual: true
`)
b.AssertFileContent("public/sv/index.html", `
Param: svParamValue
`)
}

View file

@ -22,6 +22,7 @@ import (
"strings"
"time"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/publisher"
"github.com/gohugoio/hugo/hugofs"
@ -40,6 +41,11 @@ import (
"github.com/gohugoio/hugo/helpers"
)
func init() {
// To avoid circular dependencies, we set this here.
langs.DeprecationFunc = helpers.Deprecated
}
// Build builds all sites. If filesystem events are provided,
// this is considered to be a potential partial rebuild.
func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {

View file

@ -132,6 +132,8 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
return nil, err
}
langs.SetParams(language, conf.Params)
s := &Site{
conf: conf,
language: language,
@ -411,6 +413,10 @@ func (s *Site) Author() map[string]any {
return s.conf.Author
}
func (s *Site) Authors() page.AuthorList {
return page.AuthorList{}
}
func (s *Site) Social() map[string]string {
return s.conf.Social
}
@ -434,6 +440,14 @@ func (s *Site) Data() map[string]any {
return s.s.h.Data()
}
func (s *Site) BuildDrafts() bool {
return s.conf.BuildDrafts
}
func (s *Site) IsMultiLingual() bool {
return s.h.isMultiLingual()
}
func (s *Site) LanguagePrefix() string {
conf := s.s.Conf
if !conf.IsMultiLingual() {

View file

@ -23,6 +23,7 @@ import (
"golang.org/x/text/language"
"github.com/gohugoio/hugo/common/htime"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/locales"
translators "github.com/gohugoio/localescompressed"
)
@ -42,6 +43,9 @@ type Language struct {
tag language.Tag
collator *Collator
location *time.Location
// This is just an alias of Site.Params.
params maps.Params
}
// NewLanguage creates a new language.
@ -76,7 +80,25 @@ func NewLanguage(lang, defaultContentLanguage, timeZone string, languageConfig L
}
return l, l.loadLocation(timeZone)
}
// This is injected from hugolib to avoid cirular dependencies.
var DeprecationFunc = func(item, alternative string, err bool) {}
const paramsDeprecationWarning = `.Language.Params is deprecated and will be removed in a future release. Use site.Params instead.
Also, for all but custom parameters, you need to use the built in Hugo variables, e.g. site.Title, site.LanguageCode; site.Language.Params.Title will not work.
See https://gohugo.io/content-management/multilingual/#changes-in-hugo-01120
`
// Params returns the language params.
// Note that this is the same as the Site.Params, but we keep it here for legacy reasons.
// Deprecated: Use the site.Params instead.
func (l *Language) Params() maps.Params {
DeprecationFunc(".Language.Params", paramsDeprecationWarning, false)
return l.params
}
func (l *Language) loadLocation(tzStr string) error {
@ -113,6 +135,10 @@ func (l Languages) AsOrdinalSet() map[string]int {
// Internal access to unexported Language fields.
// This construct is to prevent them from leaking to the templates.
func SetParams(l *Language, params maps.Params) {
l.params = params
}
func GetTimeFormatter(l *Language) htime.TimeFormatter {
return l.timeFormatter
}

View file

@ -37,6 +37,9 @@ type Site interface {
GetPage(ref ...string) (Page, error)
// AllPages returns all pages for all languages.
AllPages() Pages
// Returns all the regular Pages in this Site.
RegularPages() Pages
@ -104,6 +107,9 @@ type Site interface {
// Author is deprecated and will be removed in a future release.
Author() map[string]interface{}
// Authors is deprecated and will be removed in a future release.
Authors() AuthorList
// Returns the social links for this site.
Social() map[string]string
@ -115,6 +121,12 @@ type Site interface {
// For internal use only.
GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Page, error)
// BuildDraft is deprecated and will be removed in a future release.
BuildDrafts() bool
// IsMultilingual reports whether this site is configured with more than one language.
IsMultiLingual() bool
}
// Sites represents an ordered list of sites (languages).
@ -146,6 +158,9 @@ func (s *siteWrapper) Social() map[string]string {
func (s *siteWrapper) Author() map[string]interface{} {
return s.s.Author()
}
func (s *siteWrapper) Authors() AuthorList {
return AuthorList{}
}
func (s *siteWrapper) GoogleAnalytics() string {
return s.s.GoogleAnalytics()
@ -159,6 +174,10 @@ func (s *siteWrapper) Language() *langs.Language {
return s.s.Language()
}
func (s *siteWrapper) AllPages() Pages {
return s.s.AllPages()
}
func (s *siteWrapper) RegularPages() Pages {
return s.s.RegularPages()
}
@ -247,6 +266,14 @@ func (s *siteWrapper) GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Pag
return s.s.GetPageWithTemplateInfo(info, ref...)
}
func (s *siteWrapper) BuildDrafts() bool {
return s.s.BuildDrafts()
}
func (s *siteWrapper) IsMultiLingual() bool {
return s.s.IsMultiLingual()
}
func (s *siteWrapper) DisqusShortname() string {
return s.s.DisqusShortname()
}
@ -259,6 +286,9 @@ type testSite struct {
func (s testSite) Author() map[string]interface{} {
return nil
}
func (s testSite) Authors() AuthorList {
return AuthorList{}
}
func (s testSite) Social() map[string]string {
return make(map[string]string)
@ -332,6 +362,10 @@ func (t testSite) Pages() Pages {
return nil
}
func (t testSite) AllPages() Pages {
return nil
}
func (t testSite) RegularPages() Pages {
return nil
}
@ -368,6 +402,14 @@ func (testSite) DisqusShortname() string {
return ""
}
func (s testSite) BuildDrafts() bool {
return false
}
func (s testSite) IsMultiLingual() bool {
return false
}
// NewDummyHugoSite creates a new minimal test site.
func NewDummyHugoSite(cfg config.Provider) Site {
return testSite{