diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-05-12 17:27:59 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-05-12 17:27:59 +0200 |
| commit | 3e09f3b6dc1484820658d07187096cda541fed46 (patch) | |
| tree | 0819bfc676e613b334da9719f56258768881cbbe /ki18n/ki18n.go | |
| parent | 19cfae39af4e7cbb8a0719e64d619ba0248f9d76 (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.go')
| -rw-r--r-- | ki18n/ki18n.go | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/ki18n/ki18n.go b/ki18n/ki18n.go index 8f20ad4..b517d06 100644 --- a/ki18n/ki18n.go +++ b/ki18n/ki18n.go @@ -2,8 +2,10 @@ package ki18n import ( "context" + "fmt" "io/fs" "net/http" + "time" "github.com/a-h/templ" "github.com/kataras/i18n" @@ -64,6 +66,42 @@ func LangMiddleware(strategies ...Strategy) func(http.Handler) http.Handler { } } +var frenchMonths = [...]string{ + "janvier", "février", "mars", "avril", "mai", "juin", + "juillet", "août", "septembre", "octobre", "novembre", "décembre", +} + +// Locale configures a language for use with ki18n. Lang is a BCP-47 tag (e.g. "es-ES"). +// FormatTime is optional; if nil, en-GB formatting is used. +type Locale struct { + Lang string + FormatTime func(time.Time) string +} + +var defaultLocales = []Locale{ + { + Lang: "en-GB", + FormatTime: func(t time.Time) string { return t.Format("2 January 2006") }, + }, + { + Lang: "fr-FR", + FormatTime: func(t time.Time) string { + return fmt.Sprintf("%d %s %d", t.Day(), frenchMonths[t.Month()-1], t.Year()) + }, + }, +} + +var timeFormatters map[string]func(time.Time) string + +func FormatTime(ctx context.Context, t time.Time) string { + lang, _ := ctx.Value(contextKey{}).(string) + f, ok := timeFormatters[lang] + if !ok { + f = timeFormatters[defaultLang] + } + return f(t) +} + func Tr(ctx context.Context, format string, args ...any) templ.Component { lang, _ := ctx.Value(contextKey{}).(string) if lang == "" { @@ -76,12 +114,25 @@ func Tr(ctx context.Context, format string, args ...any) templ.Component { return templ.Raw(msg) } -func Init(fs fs.FS) error { - loader, err := i18n.FS(fs, "*/*.yml") +func Init(localesFS fs.FS, extra ...Locale) error { + all := append(defaultLocales, extra...) + + langs := make([]string, len(all)) + timeFormatters = make(map[string]func(time.Time) string, len(all)) + for i, loc := range all { + langs[i] = loc.Lang + if loc.FormatTime != nil { + timeFormatters[loc.Lang] = loc.FormatTime + } else { + timeFormatters[loc.Lang] = timeFormatters[defaultLang] + } + } + + loader, err := i18n.FS(localesFS, "*/*.yml") if err != nil { return err } - inst, err := i18n.New(loader, "en-GB", "fr-FR") + inst, err := i18n.New(loader, langs...) if err != nil { return err } |
