Skip to content

Commit

Permalink
Add language.LanguageCode
Browse files Browse the repository at this point in the history
But keep an alias at Site

Closes #11027
  • Loading branch information
bep committed May 27, 2023
1 parent 8f293a1 commit 6c2db0d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ baseURL = "https://example.com"
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
[languages]
[languages.en]
languageCode = "en-US"
title = "English Title"
[languages.en.params]
myparam = "enParamValue"
Expand All @@ -840,6 +841,7 @@ title: "My English Section"
title: "My Swedish Section"
---
-- layouts/index.html --
LanguageCode: {{ eq site.LanguageCode site.Language.LanguageCode }}|{{ site.Language.LanguageCode }}|
{{ 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 }}|Language string: {{ .Language }}|Languages: {{ .Languages }}
{{ end }}
Expand All @@ -863,10 +865,12 @@ Sections: true|
Param: enParamValue
Param: enParamValue
IsMultiLingual: true
LanguageCode: true|en-US|
`)

b.AssertFileContent("public/sv/index.html", `
Param: svParamValue
Param: svParamValue
LanguageCode: true|sv|
`)

Expand Down
5 changes: 1 addition & 4 deletions hugolib/site_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,7 @@ func (s *Site) Config() page.SiteConfig {
}

func (s *Site) LanguageCode() string {
if s.conf.LanguageCode != "" {
return s.conf.LanguageCode
}
return s.language.Lang
return s.Language().LanguageCode()
}

// Returns all Sites for all languages.
Expand Down
3 changes: 3 additions & 0 deletions langs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type LanguageConfig struct {
// The language name, e.g. "English".
LanguageName string

// The language code, e.g. "en-US".
LanguageCode string

// The language title. When set, this will
// override site.Title for this language.
Title string
Expand Down
7 changes: 7 additions & 0 deletions langs/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ func (l *Language) Params() maps.Params {
return l.params
}

func (l *Language) LanguageCode() string {
if l.LanguageConfig.LanguageCode != "" {
return l.LanguageConfig.LanguageCode
}
return l.Lang
}

func (l *Language) loadLocation(tzStr string) error {
location, err := time.LoadLocation(tzStr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions resources/page/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Site interface {
Title() string

// Returns the configured language code for this Site.
// Deprecated: Use .Language.LanguageCode instead.
LanguageCode() string

// Returns the configured copyright information for this Site.
Expand Down

0 comments on commit 6c2db0d

Please sign in to comment.