fix(time): fallback just-now time strings
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
b8444b1780
commit
bfada30290
2 changed files with 23 additions and 1 deletions
5
time.go
5
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:
|
||||
|
|
|
|||
19
time_test.go
19
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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue