From 3e09f3b6dc1484820658d07187096cda541fed46 Mon Sep 17 00:00:00 2001 From: Martin Kagamino Lehoux Date: Tue, 12 May 2026 17:27:59 +0200 Subject: 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 --- ki18n/ki18n_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'ki18n/ki18n_test.go') 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"}) -- cgit v1.3