diff --git a/time.go b/time.go index 42a767d..591b49e 100644 --- a/time.go +++ b/time.go @@ -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: diff --git a/time_test.go b/time_test.go index c7d9c76..46c8519 100644 --- a/time_test.go +++ b/time_test.go @@ -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) {