diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-26 18:28:57 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-27 19:02:48 +0200 |
commit | a57dda854b5efd3429af5f0b1564fc9d9d5439b9 (patch) | |
tree | 8e01442c7c43cc5bef5c9d5dbfdb3e0132736efe /tpl | |
parent | f9afba933579de07d2d2e36a457895ec5f1b7f01 (diff) |
Localize time.Format
Fixes #8797
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/time/init.go | 6 | ||||
-rw-r--r-- | tpl/time/init_test.go | 7 | ||||
-rw-r--r-- | tpl/time/time.go | 16 | ||||
-rw-r--r-- | tpl/time/time_test.go | 10 |
4 files changed, 30 insertions, 9 deletions
diff --git a/tpl/time/init.go b/tpl/time/init.go index 23e5263ba..775878f71 100644 --- a/tpl/time/init.go +++ b/tpl/time/init.go @@ -15,6 +15,7 @@ package time import ( "github.com/gohugoio/hugo/deps" + "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/tpl/internal" ) @@ -22,7 +23,10 @@ const name = "time" func init() { f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { - ctx := New() + if d.Language == nil { + panic("Language must be set") + } + ctx := New(langs.GetTranslator(d.Language)) ns := &internal.TemplateFuncsNamespace{ Name: name, diff --git a/tpl/time/init_test.go b/tpl/time/init_test.go index 672b03547..8c00ec51f 100644 --- a/tpl/time/init_test.go +++ b/tpl/time/init_test.go @@ -16,6 +16,9 @@ package time import ( "testing" + "github.com/gohugoio/hugo/config" + "github.com/gohugoio/hugo/langs" + "github.com/gohugoio/hugo/htesting/hqt" qt "github.com/frankban/quicktest" @@ -29,7 +32,9 @@ func TestInit(t *testing.T) { var ns *internal.TemplateFuncsNamespace for _, nsf := range internal.TemplateFuncsNamespaceRegistry { - ns = nsf(&deps.Deps{}) + ns = nsf(&deps.Deps{ + Language: langs.NewDefaultLanguage(config.New()), + }) if ns.Name == name { found = true break diff --git a/tpl/time/time.go b/tpl/time/time.go index 8ed4606d6..6fb901cc3 100644 --- a/tpl/time/time.go +++ b/tpl/time/time.go @@ -18,6 +18,10 @@ import ( "fmt" _time "time" + "github.com/gohugoio/hugo/common/htime" + + "github.com/go-playground/locales" + "github.com/spf13/cast" ) @@ -49,12 +53,16 @@ var timeFormats = []string{ } // New returns a new instance of the time-namespaced template functions. -func New() *Namespace { - return &Namespace{} +func New(translator locales.Translator) *Namespace { + return &Namespace{ + timeFormatter: htime.NewTimeFormatter(translator), + } } // Namespace provides template functions for the "time" namespace. -type Namespace struct{} +type Namespace struct { + timeFormatter htime.TimeFormatter +} // AsTime converts the textual representation of the datetime string into // a time.Time interface. @@ -105,7 +113,7 @@ func (ns *Namespace) Format(layout string, v interface{}) (string, error) { return "", err } - return t.Format(layout), nil + return ns.timeFormatter.Format(t, layout), nil } // Now returns the current local time. diff --git a/tpl/time/time_test.go b/tpl/time/time_test.go index d9e112878..ed689f9a8 100644 --- a/tpl/time/time_test.go +++ b/tpl/time/time_test.go @@ -16,12 +16,14 @@ package time import ( "testing" "time" + + translators "github.com/bep/gotranslators" ) func TestTimeLocation(t *testing.T) { t.Parallel() - ns := New() + ns := New(translators.Get("en")) for i, test := range []struct { value string @@ -59,7 +61,7 @@ func TestTimeLocation(t *testing.T) { func TestFormat(t *testing.T) { t.Parallel() - ns := New() + ns := New(translators.Get("en")) for i, test := range []struct { layout string @@ -76,6 +78,8 @@ func TestFormat(t *testing.T) { {time.RFC1123, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"}, {time.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"}, {time.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"}, + // Custom layouts, as introduced in Hugo 0.87. + {":date_medium", "2015-01-21", "Jan 21, 2015"}, } { result, err := ns.Format(test.layout, test.value) if b, ok := test.expect.(bool); ok && !b { @@ -97,7 +101,7 @@ func TestFormat(t *testing.T) { func TestDuration(t *testing.T) { t.Parallel() - ns := New() + ns := New(translators.Get("en")) for i, test := range []struct { unit interface{} |