-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
196 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters