Skip to content

Commit

Permalink
Deprecate site.Language.Params and some other fixes
Browse files Browse the repository at this point in the history
Updates #10947
  • Loading branch information
bep committed May 17, 2023
1 parent 0106cf1 commit 5d85716
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 5 deletions.
38 changes: 33 additions & 5 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 English Section"
---
title: "My 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 }}
`
Expand All @@ -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
`)

}
6 changes: 6 additions & 0 deletions hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/publisher"

"github.com/gohugoio/hugo/hugofs"
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions hugolib/site_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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() {
Expand Down
26 changes: 26 additions & 0 deletions langs/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
42 changes: 42 additions & 0 deletions resources/page/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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).
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 5d85716

Please sign in to comment.