summaryrefslogtreecommitdiff
path: root/ki18n/ki18n_test.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-05-12 17:27:59 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-05-12 17:27:59 +0200
commit3e09f3b6dc1484820658d07187096cda541fed46 (patch)
tree0819bfc676e613b334da9719f56258768881cbbe /ki18n/ki18n_test.go
parent19cfae39af4e7cbb8a0719e64d619ba0248f9d76 (diff)
ki18n: add locale-aware date formatting via FormatTime
Adds FormatTime(ctx, t) for locale-aware date output (e.g. "12 mai 2026" in fr-FR). Init now accepts extra Locale values so callers can register additional languages with custom formatters; en-GB and fr-FR are built-in defaults. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'ki18n/ki18n_test.go')
-rw-r--r--ki18n/ki18n_test.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/ki18n/ki18n_test.go b/ki18n/ki18n_test.go
index fa1f226..5956be5 100644
--- a/ki18n/ki18n_test.go
+++ b/ki18n/ki18n_test.go
@@ -9,6 +9,7 @@ import (
"strings"
"testing"
"testing/fstest"
+ "time"
"github.com/a-h/templ"
"github.com/martinlehoux/kagamigo/kcore"
@@ -54,6 +55,64 @@ func TestTr_withoutLangInContext(t *testing.T) {
assert.Equal(t, "Hello", result)
}
+func TestFormatTime_englishLocale(t *testing.T) {
+ ctx := context.WithValue(context.Background(), contextKey{}, "en-GB")
+ d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
+
+ assert.Equal(t, "12 May 2026", FormatTime(ctx, d))
+}
+
+func TestFormatTime_frenchLocale(t *testing.T) {
+ ctx := context.WithValue(context.Background(), contextKey{}, "fr-FR")
+ d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
+
+ assert.Equal(t, "12 mai 2026", FormatTime(ctx, d))
+}
+
+func TestFormatTime_unknownLocale(t *testing.T) {
+ ctx := context.WithValue(context.Background(), contextKey{}, "de-DE")
+ d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
+
+ assert.Equal(t, "12 May 2026", FormatTime(ctx, d))
+}
+
+func TestFormatTime_extraLocale(t *testing.T) {
+ spanishMonths := [...]string{
+ "enero", "febrero", "marzo", "abril", "mayo", "junio",
+ "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre",
+ }
+ fs := fstest.MapFS{
+ "es-ES/index.yml": &fstest.MapFile{Data: []byte{}},
+ }
+ kcore.Expect(Init(fs, Locale{
+ Lang: "es-ES",
+ FormatTime: func(t time.Time) string {
+ return fmt.Sprintf("%d de %s de %d", t.Day(), spanishMonths[t.Month()-1], t.Year())
+ },
+ }), "failed to init fs")
+ ctx := context.WithValue(context.Background(), contextKey{}, "es-ES")
+ d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
+
+ assert.Equal(t, "12 de mayo de 2026", FormatTime(ctx, d))
+}
+
+func TestFormatTime_extraLocaleNoFormatter(t *testing.T) {
+ fs := fstest.MapFS{
+ "es-ES/index.yml": &fstest.MapFile{Data: []byte{}},
+ }
+ kcore.Expect(Init(fs, Locale{Lang: "es-ES"}), "failed to init fs")
+ ctx := context.WithValue(context.Background(), contextKey{}, "es-ES")
+ d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
+
+ assert.Equal(t, "12 May 2026", FormatTime(ctx, d))
+}
+
+func TestFormatTime_noLocaleInContext(t *testing.T) {
+ d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
+
+ assert.Equal(t, "12 May 2026", FormatTime(context.Background(), d))
+}
+
func TestCookieStrategy_found(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.AddCookie(&http.Cookie{Name: "lang", Value: "fr-FR"})