aboutsummaryrefslogtreecommitdiff
path: root/README.md
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 /README.md
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 'README.md')
-rw-r--r--README.md22
1 files changed, 20 insertions, 2 deletions
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 `<lang>/*.yml` translation files.
+1. Call `ki18n.Init(localesFS)` at startup with a filesystem containing `<lang>/*.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