Skip to content

Commit 5d85716

Browse files
committedMay 17, 2023
Deprecate site.Language.Params and some other fixes
Updates #10947
1 parent 0106cf1 commit 5d85716

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed
 

‎hugolib/config_test.go

+33-5
Original file line numberDiff line numberDiff line change
@@ -742,19 +742,35 @@ themeconfigdirparam: {{ site.Params.themeconfigdirparam }}
742742

743743
}
744744

745-
// TODO(beo) find a better place for this.
746745
func TestReproCommentsIn10947(t *testing.T) {
747746
t.Parallel()
748747

749748
files := `
750749
-- hugo.toml --
751750
baseURL = "https://example.com"
752-
-- content/mysection/_index.md --
751+
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
752+
[languages]
753+
[languages.en]
754+
title = "English Title"
755+
[languages.en.params]
756+
myparam = "enParamValue"
757+
[languages.sv]
758+
title = "Svensk Title"
759+
[languages.sv.params]
760+
myparam = "svParamValue"
761+
-- content/mysection/_index.en.md --
762+
---
763+
title: "My English Section"
753764
---
754-
title: "My Section"
765+
-- content/mysection/_index.sv.md --
766+
---
767+
title: "My Swedish Section"
755768
---
756769
-- layouts/index.html --
757-
Sections: {{ if site.Sections }}true{{ end }}|
770+
{{ range $i, $e := (slice site .Site) }}
771+
{{ $i }}|AllPages: {{ len .AllPages }}|Sections: {{ if .Sections }}true{{ end }}| Author: {{ .Authors }}|BuildDrafts: {{ .BuildDrafts }}|IsMultiLingual: {{ .IsMultiLingual }}|Param: {{ .Language.Params.myparam }}|
772+
{{ end }}
773+
758774
759775
760776
`
@@ -765,6 +781,18 @@ Sections: {{ if site.Sections }}true{{ end }}|
765781
},
766782
).Build()
767783

768-
b.AssertFileContent("public/index.html", "Sections: true|")
784+
b.Assert(b.H.Log.LogCounters().WarnCounter.Count(), qt.Equals, uint64(2))
785+
b.AssertFileContent("public/index.html", `
786+
AllPages: 4|
787+
Sections: true|
788+
Param: enParamValue
789+
Param: enParamValue
790+
IsMultiLingual: true
791+
`)
792+
793+
b.AssertFileContent("public/sv/index.html", `
794+
Param: svParamValue
795+
796+
`)
769797

770798
}

‎hugolib/hugo_sites_build.go

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"time"
2424

25+
"github.com/gohugoio/hugo/langs"
2526
"github.com/gohugoio/hugo/publisher"
2627

2728
"github.com/gohugoio/hugo/hugofs"
@@ -40,6 +41,11 @@ import (
4041
"github.com/gohugoio/hugo/helpers"
4142
)
4243

44+
func init() {
45+
// To avoid circular dependencies, we set this here.
46+
langs.DeprecationFunc = helpers.Deprecated
47+
}
48+
4349
// Build builds all sites. If filesystem events are provided,
4450
// this is considered to be a potential partial rebuild.
4551
func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {

‎hugolib/site_new.go

+14
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ func NewHugoSites(cfg deps.DepsCfg) (*HugoSites, error) {
132132
return nil, err
133133
}
134134

135+
langs.SetParams(language, conf.Params)
136+
135137
s := &Site{
136138
conf: conf,
137139
language: language,
@@ -411,6 +413,10 @@ func (s *Site) Author() map[string]any {
411413
return s.conf.Author
412414
}
413415

416+
func (s *Site) Authors() page.AuthorList {
417+
return page.AuthorList{}
418+
}
419+
414420
func (s *Site) Social() map[string]string {
415421
return s.conf.Social
416422
}
@@ -434,6 +440,14 @@ func (s *Site) Data() map[string]any {
434440
return s.s.h.Data()
435441
}
436442

443+
func (s *Site) BuildDrafts() bool {
444+
return s.conf.BuildDrafts
445+
}
446+
447+
func (s *Site) IsMultiLingual() bool {
448+
return s.h.isMultiLingual()
449+
}
450+
437451
func (s *Site) LanguagePrefix() string {
438452
conf := s.s.Conf
439453
if !conf.IsMultiLingual() {

‎langs/language.go

+26
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"golang.org/x/text/language"
2424

2525
"github.com/gohugoio/hugo/common/htime"
26+
"github.com/gohugoio/hugo/common/maps"
2627
"github.com/gohugoio/locales"
2728
translators "github.com/gohugoio/localescompressed"
2829
)
@@ -42,6 +43,9 @@ type Language struct {
4243
tag language.Tag
4344
collator *Collator
4445
location *time.Location
46+
47+
// This is just an alias of Site.Params.
48+
params maps.Params
4549
}
4650

4751
// NewLanguage creates a new language.
@@ -76,7 +80,25 @@ func NewLanguage(lang, defaultContentLanguage, timeZone string, languageConfig L
7680
}
7781

7882
return l, l.loadLocation(timeZone)
83+
}
84+
85+
// This is injected from hugolib to avoid cirular dependencies.
86+
var DeprecationFunc = func(item, alternative string, err bool) {}
87+
88+
const paramsDeprecationWarning = `.Language.Params is deprecated and will be removed in a future release. Use site.Params instead.
89+
90+
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.
7991
92+
See https://gohugo.io/content-management/multilingual/#changes-in-hugo-01120
93+
94+
`
95+
96+
// Params returns the language params.
97+
// Note that this is the same as the Site.Params, but we keep it here for legacy reasons.
98+
// Deprecated: Use the site.Params instead.
99+
func (l *Language) Params() maps.Params {
100+
DeprecationFunc(".Language.Params", paramsDeprecationWarning, false)
101+
return l.params
80102
}
81103

82104
func (l *Language) loadLocation(tzStr string) error {
@@ -113,6 +135,10 @@ func (l Languages) AsOrdinalSet() map[string]int {
113135
// Internal access to unexported Language fields.
114136
// This construct is to prevent them from leaking to the templates.
115137

138+
func SetParams(l *Language, params maps.Params) {
139+
l.params = params
140+
}
141+
116142
func GetTimeFormatter(l *Language) htime.TimeFormatter {
117143
return l.timeFormatter
118144
}

‎resources/page/site.go

+42
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type Site interface {
3737

3838
GetPage(ref ...string) (Page, error)
3939

40+
// AllPages returns all pages for all languages.
41+
AllPages() Pages
42+
4043
// Returns all the regular Pages in this Site.
4144
RegularPages() Pages
4245

@@ -104,6 +107,9 @@ type Site interface {
104107
// Author is deprecated and will be removed in a future release.
105108
Author() map[string]interface{}
106109

110+
// Authors is deprecated and will be removed in a future release.
111+
Authors() AuthorList
112+
107113
// Returns the social links for this site.
108114
Social() map[string]string
109115

@@ -115,6 +121,12 @@ type Site interface {
115121

116122
// For internal use only.
117123
GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Page, error)
124+
125+
// BuildDraft is deprecated and will be removed in a future release.
126+
BuildDrafts() bool
127+
128+
// IsMultilingual reports whether this site is configured with more than one language.
129+
IsMultiLingual() bool
118130
}
119131

120132
// Sites represents an ordered list of sites (languages).
@@ -146,6 +158,9 @@ func (s *siteWrapper) Social() map[string]string {
146158
func (s *siteWrapper) Author() map[string]interface{} {
147159
return s.s.Author()
148160
}
161+
func (s *siteWrapper) Authors() AuthorList {
162+
return AuthorList{}
163+
}
149164

150165
func (s *siteWrapper) GoogleAnalytics() string {
151166
return s.s.GoogleAnalytics()
@@ -159,6 +174,10 @@ func (s *siteWrapper) Language() *langs.Language {
159174
return s.s.Language()
160175
}
161176

177+
func (s *siteWrapper) AllPages() Pages {
178+
return s.s.AllPages()
179+
}
180+
162181
func (s *siteWrapper) RegularPages() Pages {
163182
return s.s.RegularPages()
164183
}
@@ -247,6 +266,14 @@ func (s *siteWrapper) GetPageWithTemplateInfo(info tpl.Info, ref ...string) (Pag
247266
return s.s.GetPageWithTemplateInfo(info, ref...)
248267
}
249268

269+
func (s *siteWrapper) BuildDrafts() bool {
270+
return s.s.BuildDrafts()
271+
}
272+
273+
func (s *siteWrapper) IsMultiLingual() bool {
274+
return s.s.IsMultiLingual()
275+
}
276+
250277
func (s *siteWrapper) DisqusShortname() string {
251278
return s.s.DisqusShortname()
252279
}
@@ -259,6 +286,9 @@ type testSite struct {
259286
func (s testSite) Author() map[string]interface{} {
260287
return nil
261288
}
289+
func (s testSite) Authors() AuthorList {
290+
return AuthorList{}
291+
}
262292

263293
func (s testSite) Social() map[string]string {
264294
return make(map[string]string)
@@ -332,6 +362,10 @@ func (t testSite) Pages() Pages {
332362
return nil
333363
}
334364

365+
func (t testSite) AllPages() Pages {
366+
return nil
367+
}
368+
335369
func (t testSite) RegularPages() Pages {
336370
return nil
337371
}
@@ -368,6 +402,14 @@ func (testSite) DisqusShortname() string {
368402
return ""
369403
}
370404

405+
func (s testSite) BuildDrafts() bool {
406+
return false
407+
}
408+
409+
func (s testSite) IsMultiLingual() bool {
410+
return false
411+
}
412+
371413
// NewDummyHugoSite creates a new minimal test site.
372414
func NewDummyHugoSite(cfg config.Provider) Site {
373415
return testSite{

0 commit comments

Comments
 (0)
Please sign in to comment.