go-i18n/time.go
Claude e8a87b0f50
feat: grammar-aware i18n module extracted from core
Standalone grammar-aware translation engine with:
- 3-tier verb/noun fallback (JSON locale → irregular maps → regular rules)
- 6 built-in i18n.* namespace handlers (label, progress, count, done, fail, numeric)
- Nested en.json with gram/prompt/time/lang sections (no flat command keys)
- CLDR plural rules for 10 languages
- Subject fluent API, number/time formatting, RTL detection
- 55 tests passing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 19:51:27 +00:00

39 lines
993 B
Go

package i18n
import (
"fmt"
"time"
)
// TimeAgo returns a localised relative time string.
//
// TimeAgo(time.Now().Add(-5 * time.Minute)) // "5 minutes ago"
func TimeAgo(t time.Time) string {
duration := time.Since(t)
switch {
case duration < time.Minute:
return T("time.just_now")
case duration < time.Hour:
return FormatAgo(int(duration.Minutes()), "minute")
case duration < 24*time.Hour:
return FormatAgo(int(duration.Hours()), "hour")
case duration < 7*24*time.Hour:
return FormatAgo(int(duration.Hours()/24), "day")
default:
return FormatAgo(int(duration.Hours()/(24*7)), "week")
}
}
// FormatAgo formats "N unit ago" with proper pluralisation.
func FormatAgo(count int, unit string) string {
svc := Default()
if svc == nil {
return fmt.Sprintf("%d %ss ago", count, unit)
}
key := "time.ago." + unit
result := svc.T(key, map[string]any{"Count": count})
if result == key {
return fmt.Sprintf("%d %s ago", count, Pluralize(unit, count))
}
return result
}