fix(time): fallback just-now time strings
All checks were successful
Security Scan / security (push) Successful in 12s
Test / test (push) Successful in 1m47s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 02:38:45 +00:00
parent b8444b1780
commit bfada30290
2 changed files with 23 additions and 1 deletions

View file

@ -17,7 +17,10 @@ func TimeAgo(t time.Time) string {
}
switch {
case duration < 5*time.Second:
return T("time.just_now")
if text := T("time.just_now"); text != "time.just_now" {
return text
}
return "just now"
case duration < time.Minute:
return FormatAgo(int(duration/time.Second), "second")
case duration < time.Hour:

View file

@ -2,6 +2,7 @@ package i18n
import (
"testing"
"testing/fstest"
"time"
"github.com/stretchr/testify/assert"
@ -93,6 +94,24 @@ func TestTimeAgo_Good_SingleUnits(t *testing.T) {
assert.Contains(t, got, "1 week ago")
}
func TestTimeAgo_Good_MissingJustNowKeyFallback(t *testing.T) {
svc, err := NewWithFS(fstest.MapFS{
"xx.json": &fstest.MapFile{
Data: []byte(`{}`),
},
}, ".")
require.NoError(t, err)
prev := Default()
SetDefault(svc)
t.Cleanup(func() {
SetDefault(prev)
})
got := TimeAgo(time.Now().Add(-4 * time.Second))
assert.Equal(t, "just now", got)
}
// --- FormatAgo ---
func TestFormatAgo_Good(t *testing.T) {