summaryrefslogtreecommitdiffhomepage
path: root/langs
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2021-07-26 18:28:57 +0200
committerBjørn Erik Pedersen <[email protected]>2021-07-27 19:02:48 +0200
commita57dda854b5efd3429af5f0b1564fc9d9d5439b9 (patch)
tree8e01442c7c43cc5bef5c9d5dbfdb3e0132736efe /langs
parentf9afba933579de07d2d2e36a457895ec5f1b7f01 (diff)
downloadhugo-a57dda854b5efd3429af5f0b1564fc9d9d5439b9.tar.gz
hugo-a57dda854b5efd3429af5f0b1564fc9d9d5439b9.zip
Localize time.Format
Fixes #8797
Diffstat (limited to 'langs')
-rw-r--r--langs/language.go30
1 files changed, 29 insertions, 1 deletions
diff --git a/langs/language.go b/langs/language.go
index a86d82ba3..5f5e8ddef 100644
--- a/langs/language.go
+++ b/langs/language.go
@@ -18,6 +18,8 @@ import (
"strings"
"sync"
+ translators "github.com/bep/gotranslators"
+ "github.com/go-playground/locales"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/config"
)
@@ -68,6 +70,11 @@ type Language struct {
params map[string]interface{}
paramsMu sync.Mutex
paramsSet bool
+
+ // Used for date formatting etc. We don't want this exported to the
+ // templates.
+ // TODO(bep) do the same for some of the others.
+ translator locales.Translator
}
func (l *Language) String() string {
@@ -86,8 +93,22 @@ func NewLanguage(lang string, cfg config.Provider) *Language {
localCfg := config.New()
compositeConfig := config.NewCompositeConfig(cfg, localCfg)
+ translator := translators.Get(lang)
+ if translator == nil {
+ translator = translators.Get(cfg.GetString("defaultContentLanguage"))
+ if translator == nil {
+ translator = translators.Get("en")
+ }
+ }
- l := &Language{Lang: lang, ContentDir: cfg.GetString("contentDir"), Cfg: cfg, LocalCfg: localCfg, Provider: compositeConfig, params: params}
+ l := &Language{
+ Lang: lang,
+ ContentDir: cfg.GetString("contentDir"),
+ Cfg: cfg, LocalCfg: localCfg,
+ Provider: compositeConfig,
+ params: params,
+ translator: translator,
+ }
return l
}
@@ -222,3 +243,10 @@ func (l *Language) IsSet(key string) bool {
}
return l.Cfg.IsSet(key)
}
+
+// Internal access to unexported Language fields.
+// This construct is to prevent them from leaking to the templates.
+
+func GetTranslator(l *Language) locales.Translator {
+ return l.translator
+}