Skip to content

Commit

Permalink
feat(localisation): add module
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Nov 12, 2023
1 parent 94ea9ef commit 8f9ce2e
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/rande/gonode/modules/debug"
"github.com/rande/gonode/modules/feed"
node_guard "github.com/rande/gonode/modules/guard"
"github.com/rande/gonode/modules/localisation"
"github.com/rande/gonode/modules/media"
"github.com/rande/gonode/modules/prism"
"github.com/rande/gonode/modules/raw"
Expand Down Expand Up @@ -53,6 +54,7 @@ func Configure(configFile string) *goapp.Lifecycle {
feed.Configure(l, conf)

template.Configure(l, conf)
localisation.Configure(l, conf)
dashboard.Configure(l, conf)
bootstrap.Configure(l, conf)
logger.Configure(l, conf)
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ require (
github.com/mitchellh/cli v1.1.5
github.com/mmcdole/gofeed v1.0.0
github.com/orandin/lumberjackrus v1.0.1
github.com/rande/goapp v0.0.0-20160602122430-52b532af1ee5
github.com/rande/goapp v0.0.0-20231103185600-ee84e9faba22
github.com/rande/goexif v0.0.0-20160216213925-90ceb8b2ebfd
github.com/rs/cors v1.9.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
github.com/vorlif/spreak v0.4.0
github.com/zenazn/goji v1.0.1
golang.org/x/crypto v0.9.0
)

require (
github.com/gkampitakis/ciinfo v0.2.4 // indirect
github.com/gkampitakis/go-diff v1.3.2 // indirect
github.com/goodsign/monday v1.0.1 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand Down Expand Up @@ -69,7 +71,7 @@ require (
golang.org/x/image v0.7.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/text v0.14.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 9 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZ
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
github.com/gkampitakis/go-snaps v0.4.8 h1:B/CJswqJ9LwQMI0tiU7ztWK8qlnz6HxOqZm+XIFuEDU=
github.com/gkampitakis/go-snaps v0.4.8/go.mod h1:8HW4KX3JKV8M0GSw69CvT+Jqhd1AlBPMPpBfjBI3bdY=
github.com/goodsign/monday v1.0.1 h1:yJogH0uQNn4blHjoC3ESbdV0P1OhDtGYdd6x0w7QZBo=
github.com/goodsign/monday v1.0.1/go.mod h1:r4T4breXpoFwspQNM+u2sLxJb2zyTaxVGqUfTBjWOu8=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -74,8 +76,6 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
Expand Down Expand Up @@ -103,8 +103,6 @@ github.com/mmcdole/gofeed v1.0.0 h1:PHqwr8fsEm8xarj9s53XeEAFYhRM3E9Ib7Ie766/LTE=
github.com/mmcdole/gofeed v1.0.0/go.mod h1:tkVcyzS3qVMlQrQxJoEH1hkTiuo9a8emDzkMi7TZBu0=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/orandin/lumberjackrus v1.0.1 h1:7ysDQ0MHD79zIFN9/EiDHjUcgopNi5ehtxFDy8rUkWo=
github.com/orandin/lumberjackrus v1.0.1/go.mod h1:xYLt6H8W93pKnQgUQaxsApS0Eb4BwHLOkxk5DVzf5H0=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand All @@ -116,6 +114,8 @@ github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXq
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/rande/goapp v0.0.0-20160602122430-52b532af1ee5 h1:GIYFSvl0syoWLBtr5khq04rxkfJaYK+rD71ww76Awj4=
github.com/rande/goapp v0.0.0-20160602122430-52b532af1ee5/go.mod h1:P0lCjBT1VNAxtnkzor0owNWXt+dkOjfktfmXJMtrT0Y=
github.com/rande/goapp v0.0.0-20231103185600-ee84e9faba22 h1:H93ZIIDS++5NSzmxckQzmZ4CgCQYPGh3LkDbiftX6Ls=
github.com/rande/goapp v0.0.0-20231103185600-ee84e9faba22/go.mod h1:P0lCjBT1VNAxtnkzor0owNWXt+dkOjfktfmXJMtrT0Y=
github.com/rande/goexif v0.0.0-20160216213925-90ceb8b2ebfd h1:GBQsl7h7/lscPPWMvnxPsvHc9zqsOoDRaUsFeL8GsRg=
github.com/rande/goexif v0.0.0-20160216213925-90ceb8b2ebfd/go.mod h1:D+F5iPzWeZt0Gpz8Vr1R8piPVQF83Q+zLUyZBASkqAw=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
Expand Down Expand Up @@ -153,6 +153,8 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/vorlif/spreak v0.4.0 h1:2qUNIoPk8iGtAvMYNL1bCJe6BeKOewIJXKA349rDzB8=
github.com/vorlif/spreak v0.4.0/go.mod h1:6xt/wqWr9j9cnLicoaqGdMUwCmJziJBWNePGXWHbimM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/zenazn/goji v1.0.1 h1:4lbD8Mx2h7IvloP7r2C0D6ltZP6Ufip8Hn0wmSK5LR8=
github.com/zenazn/goji v1.0.1/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
Expand Down Expand Up @@ -207,17 +209,17 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
6 changes: 6 additions & 0 deletions modules/localisation/localisation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package localisation
31 changes: 31 additions & 0 deletions modules/localisation/localisation_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package localisation

import (
"github.com/rande/goapp"
"github.com/rande/gonode/core/config"
"github.com/rande/gonode/modules/template"
)

func Configure(l *goapp.Lifecycle, conf *config.Config) {

l.Prepare(func(app *goapp.App) error {

return nil
})

l.Config(func(app *goapp.App) error {
loader := app.Get("gonode.template").(*template.TemplateLoader)
defaultLocale := "en_GB"

for name, Func := range CreateTemplateFuncMap(defaultLocale) {
loader.FuncMap[name] = Func
}

return nil
})
}
67 changes: 67 additions & 0 deletions modules/localisation/localisation_monday.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package localisation

import (
tpl "html/template"
"time"

"github.com/goodsign/monday"
"github.com/rande/gonode/modules/template"
)

func GetLocale(ctx template.Context) monday.Locale {
locale := "en_US"
if l, ok := ctx["locale"]; ok {
locale = l.(string)
}

return monday.Locale(locale)
}

func GetTimezone(ctx template.Context) *time.Location {
tz := "UTC"
if l, ok := ctx["tz"]; ok {
tz = l.(string)
}

if location, err := time.LoadLocation(tz); err == nil {
return location
}

return time.UTC
}

func CreateTemplateFuncMap(defaultLocale string) map[string]interface{} {

FuncMap := make(map[string]interface{})

FuncMap["short_date"] = func(ctx template.Context, date time.Time) tpl.HTML {
return tpl.HTML(date.Format(monday.ShortFormatsByLocale[GetLocale(ctx)]))
}

FuncMap["medium_date"] = func(ctx template.Context, date time.Time) tpl.HTML {
return tpl.HTML(date.Format(monday.MediumFormatsByLocale[GetLocale(ctx)]))
}

FuncMap["long_date"] = func(ctx template.Context, date time.Time) tpl.HTML {
return tpl.HTML(date.Format(monday.LongFormatsByLocale[GetLocale(ctx)]))
}

FuncMap["time"] = func(ctx template.Context, date time.Time) tpl.HTML {
return tpl.HTML(date.In(GetTimezone(ctx)).Format(monday.TimeFormatsByLocale[GetLocale(ctx)]))
}

FuncMap["tz_time"] = func(ctx template.Context, date time.Time) tpl.HTML {
return tpl.HTML(date.In(GetTimezone(ctx)).Format(monday.TimeFormatsByLocale[GetLocale(ctx)] + " (MST)"))
}

FuncMap["tz"] = func(ctx template.Context, date time.Time) tpl.HTML {
return tpl.HTML(GetTimezone(ctx).String())
}

return FuncMap
}
75 changes: 75 additions & 0 deletions modules/localisation/localisation_monday_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2014-2023 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package localisation

import (
tpl "html/template"
"testing"
"time"

"github.com/rande/gonode/modules/template"
"github.com/stretchr/testify/assert"
)

func GetFunc(funcs map[string]interface{}, name string) func(ctx template.Context, date time.Time) tpl.HTML {
return funcs[name].(func(ctx template.Context, date time.Time) tpl.HTML)
}

var funcs = CreateTemplateFuncMap("en_GB")
var date = time.Date(2023, time.December, 25, 1, 1, 1, 1, time.UTC)
var ctx = template.Context{
"locale": "fr_FR",
"tz": "Europe/Paris",
}
var emptyCtx = template.Context{}

func Test_Format_Short_Date(t *testing.T) {
assert.Equal(t, tpl.HTML("25/12/2023"), GetFunc(funcs, "short_date")(ctx, date))
}

func Test_Format_Short_Date_EmptyCtx(t *testing.T) {
assert.Equal(t, tpl.HTML("12/25/23"), GetFunc(funcs, "short_date")(emptyCtx, date))
}

func Test_Format_Medium_Date(t *testing.T) {
assert.Equal(t, tpl.HTML("25 Dec 2023"), GetFunc(funcs, "medium_date")(ctx, date))
}

func Test_Format_Medium_Date_EmptyCtx(t *testing.T) {
assert.Equal(t, tpl.HTML("Dec 25, 2023"), GetFunc(funcs, "medium_date")(emptyCtx, date))
}

func Test_Format_Long_Date(t *testing.T) {
assert.Equal(t, tpl.HTML("25 December 2023"), GetFunc(funcs, "long_date")(ctx, date))
}

func Test_Format_Long_Date_EmptyCtx(t *testing.T) {
assert.Equal(t, tpl.HTML("December 25, 2023"), GetFunc(funcs, "long_date")(emptyCtx, date))
}

func Test_Format_Time(t *testing.T) {
assert.Equal(t, tpl.HTML("01:01"), GetFunc(funcs, "time")(ctx, date))
}

func Test_Format_Time_EmptyCtx(t *testing.T) {
assert.Equal(t, tpl.HTML("1:01 AM"), GetFunc(funcs, "time")(emptyCtx, date))
}

func Test_Format_Tz_Time(t *testing.T) {
assert.Equal(t, tpl.HTML("01:01 (UTC)"), GetFunc(funcs, "tz_time")(ctx, date))
}

func Test_Format_Tz_Time_EmptyCtx(t *testing.T) {
assert.Equal(t, tpl.HTML("1:01 AM (UTC)"), GetFunc(funcs, "tz_time")(emptyCtx, date))
}

func Test_Format_Tz(t *testing.T) {
assert.Equal(t, tpl.HTML("Europe/Paris"), GetFunc(funcs, "tz")(ctx, date))
}

func Test_Format_Tz_EmptyCtx(t *testing.T) {
assert.Equal(t, tpl.HTML("UTC"), GetFunc(funcs, "tz")(emptyCtx, date))
}
2 changes: 2 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/rande/gonode/modules/debug"
"github.com/rande/gonode/modules/feed"
node_guard "github.com/rande/gonode/modules/guard"
"github.com/rande/gonode/modules/localisation"
"github.com/rande/gonode/modules/media"
"github.com/rande/gonode/modules/prism"
"github.com/rande/gonode/modules/raw"
Expand Down Expand Up @@ -129,6 +130,7 @@ func GetLifecycle(file string) *goapp.Lifecycle {
dashboard.Configure(l, conf)
template.Configure(l, conf)

localisation.Configure(l, conf)
logger.Configure(l, conf)
commands.Configure(l, conf)
security.ConfigureCors(l, conf)
Expand Down

0 comments on commit 8f9ce2e

Please sign in to comment.