diff options
author | Bjørn Erik Pedersen <[email protected]> | 2021-08-01 11:50:12 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2021-08-01 13:39:30 +0200 |
commit | 4d221ce468a1209ee9dd6cbece9d1273dad6a29b (patch) | |
tree | 4c6070b598fd81d0687615cdcf29644e9b3b237e /langs | |
parent | e3dc5240f01fd5ec67643e40f27c026d707da110 (diff) | |
download | hugo-4d221ce468a1209ee9dd6cbece9d1273dad6a29b.tar.gz hugo-4d221ce468a1209ee9dd6cbece9d1273dad6a29b.zip |
Fail on invalid time zone
Fixes #8832
Diffstat (limited to 'langs')
-rw-r--r-- | langs/config.go | 10 | ||||
-rw-r--r-- | langs/language.go | 37 |
2 files changed, 32 insertions, 15 deletions
diff --git a/langs/config.go b/langs/config.go index fe4ed9d14..f79b7dd0a 100644 --- a/langs/config.go +++ b/langs/config.go @@ -161,6 +161,12 @@ func LoadLanguageSettings(cfg config.Provider, oldLangs Languages) (c LanguagesC } } + for _, language := range c.Languages { + if language.initErr != nil { + return c, language.initErr + } + } + return c, nil } @@ -197,6 +203,10 @@ func toSortedLanguages(cfg config.Provider, l map[string]interface{}) (Languages for k, vv := range m { language.SetParam(k, vv) } + case "timezone": + if err := language.loadLocation(cast.ToString(v)); err != nil { + return nil, err + } } // Put all into the Params map diff --git a/langs/language.go b/langs/language.go index 758729c25..46f1be60e 100644 --- a/langs/language.go +++ b/langs/language.go @@ -19,6 +19,8 @@ import ( "sync" "time" + "github.com/pkg/errors" + translators "github.com/bep/gotranslators" "github.com/go-playground/locales" "github.com/gohugoio/hugo/common/maps" @@ -77,8 +79,10 @@ type Language struct { // TODO(bep) do the same for some of the others. translator locales.Translator - locationInit sync.Once - location *time.Location + location *time.Location + + // Error during initialization. Will fail the buld. + initErr error } func (l *Language) String() string { @@ -113,6 +117,11 @@ func NewLanguage(lang string, cfg config.Provider) *Language { params: params, translator: translator, } + + if err := l.loadLocation(cfg.GetString("timeZone")); err != nil { + l.initErr = err + } + return l } @@ -248,18 +257,6 @@ func (l *Language) IsSet(key string) bool { return l.Cfg.IsSet(key) } -func (l *Language) getLocation() *time.Location { - l.locationInit.Do(func() { - location, err := time.LoadLocation(l.GetString("timeZone")) - if err != nil { - location = time.UTC - } - l.location = location - }) - - return l.location -} - // Internal access to unexported Language fields. // This construct is to prevent them from leaking to the templates. @@ -268,5 +265,15 @@ func GetTranslator(l *Language) locales.Translator { } func GetLocation(l *Language) *time.Location { - return l.getLocation() + return l.location +} + +func (l *Language) loadLocation(tzStr string) error { + location, err := time.LoadLocation(tzStr) + if err != nil { + return errors.Wrapf(err, "invalid timeZone for language %q", l.Lang) + } + l.location = location + + return nil } |