Add language.LanguageCode

But keep an alias at Site

Closes #11027
This commit is contained in:
Bjørn Erik Pedersen 2023-05-27 15:44:41 +02:00
parent 8f293a1855
commit 6c2db0dfb0
5 changed files with 17 additions and 5 deletions

View file

@ -824,6 +824,7 @@ baseURL = "https://example.com"
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"]
[languages] [languages]
[languages.en] [languages.en]
languageCode = "en-US"
title = "English Title" title = "English Title"
[languages.en.params] [languages.en.params]
myparam = "enParamValue" myparam = "enParamValue"
@ -840,6 +841,7 @@ title: "My English Section"
title: "My Swedish Section" title: "My Swedish Section"
--- ---
-- layouts/index.html -- -- layouts/index.html --
LanguageCode: {{ eq site.LanguageCode site.Language.LanguageCode }}|{{ site.Language.LanguageCode }}|
{{ range $i, $e := (slice site .Site) }} {{ 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 }} {{ $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 }} {{ end }}
@ -863,10 +865,12 @@ Sections: true|
Param: enParamValue Param: enParamValue
Param: enParamValue Param: enParamValue
IsMultiLingual: true IsMultiLingual: true
LanguageCode: true|en-US|
`) `)
b.AssertFileContent("public/sv/index.html", ` b.AssertFileContent("public/sv/index.html", `
Param: svParamValue Param: svParamValue
LanguageCode: true|sv|
`) `)

View file

@ -361,10 +361,7 @@ func (s *Site) Config() page.SiteConfig {
} }
func (s *Site) LanguageCode() string { func (s *Site) LanguageCode() string {
if s.conf.LanguageCode != "" { return s.Language().LanguageCode()
return s.conf.LanguageCode
}
return s.language.Lang
} }
// Returns all Sites for all languages. // Returns all Sites for all languages.

View file

@ -24,6 +24,9 @@ type LanguageConfig struct {
// The language name, e.g. "English". // The language name, e.g. "English".
LanguageName string LanguageName string
// The language code, e.g. "en-US".
LanguageCode string
// The language title. When set, this will // The language title. When set, this will
// override site.Title for this language. // override site.Title for this language.
Title string Title string

View file

@ -102,6 +102,13 @@ func (l *Language) Params() maps.Params {
return l.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 { func (l *Language) loadLocation(tzStr string) error {
location, err := time.LoadLocation(tzStr) location, err := time.LoadLocation(tzStr)
if err != nil { if err != nil {

View file

@ -65,6 +65,7 @@ type Site interface {
Title() string Title() string
// Returns the configured language code for this Site. // Returns the configured language code for this Site.
// Deprecated: Use .Language.LanguageCode instead.
LanguageCode() string LanguageCode() string
// Returns the configured copyright information for this Site. // Returns the configured copyright information for this Site.