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 --- README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index ce2d349..9e6cf5a 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,10 @@ ## ki18n - [ ] Add gettext to CI -- [ ] Add time handling ### Setup -1. Call `ki18n.Init(localesFS)` at startup with a filesystem containing `/*.yml` translation files. +1. Call `ki18n.Init(localesFS)` at startup with a filesystem containing `/*.yml` translation files. Pass extra `ki18n.Locale` values to register additional languages. 2. Register `ki18n.LangMiddleware(...)` on your router, passing one or more strategies in priority order. 3. Run `go run github.com/martinlehoux/kagamigo/cmd/gettext -write` to generate translation files. 4. Complete the generated files. @@ -41,6 +40,25 @@ Built-in strategies: If no strategy resolves a language, the middleware falls back to `en-GB`. +### Formatting dates + +```go +ki18n.FormatTime(ctx, time.Now()) // "12 May 2026" in en-GB, "12 mai 2026" in fr-FR +``` + +To add a language, pass a `Locale` to `Init`: + +```go +ki18n.Init(localesFS, ki18n.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()) + }, +}) +``` + +`FormatTime` is optional on `Locale` — omit it to fall back to `en-GB` date formatting. + ### Translating in templ files ```go -- cgit v1.3