feat(i18n): use plural translations in humanize duration
This commit is contained in:
parent
96369f3b1c
commit
d16d13d730
|
|
@ -149,16 +149,11 @@
|
|||
}
|
||||
},
|
||||
"time": {
|
||||
"one_year": "one year",
|
||||
"one_week": "one week",
|
||||
"one_day": "one day",
|
||||
"one_hour": "one hour",
|
||||
"one_minute": "one minute",
|
||||
"many_years": "%[1]d years",
|
||||
"many_weeks": "%[1]d weeks",
|
||||
"many_days": "%[1]d days",
|
||||
"many_hours": "%[1]d hours",
|
||||
"many_minutes": "%[1]d minutes",
|
||||
"since_years": "one year|%[1]d years",
|
||||
"since_weeks": "one week|%[1]d weeks",
|
||||
"since_days": "one day|%[1]d days",
|
||||
"since_hours": "one hour|%[1]d hours",
|
||||
"since_minutes": "one minute|%[1]d minutes",
|
||||
"list_last_separator": "and"
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,6 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -37,28 +36,25 @@ func HumanizeDuration(duration time.Duration, lang string) string {
|
|||
minutes := int64(math.Mod(duration.Minutes(), 60))
|
||||
|
||||
chunks := []struct {
|
||||
pluralFormatKey string
|
||||
oneKey string
|
||||
amount int64
|
||||
key string
|
||||
amount int64
|
||||
}{
|
||||
{"time.many_years", "time.one_year", years},
|
||||
{"time.many_weeks", "time.one_week", weeks},
|
||||
{"time.many_days", "time.one_day", days},
|
||||
{"time.many_hours", "time.one_hour", hours},
|
||||
{"time.many_minutes", "time.one_minute", minutes},
|
||||
{"time.since_years", years},
|
||||
{"time.since_weeks", weeks},
|
||||
{"time.since_days", days},
|
||||
{"time.since_hours", hours},
|
||||
{"time.since_minutes", minutes},
|
||||
}
|
||||
|
||||
parts := []string{}
|
||||
|
||||
for _, chunk := range chunks {
|
||||
switch chunk.amount {
|
||||
case 0:
|
||||
if chunk.amount <= 0 {
|
||||
continue
|
||||
case 1:
|
||||
parts = append(parts, i18n.T(lang, chunk.oneKey))
|
||||
default:
|
||||
parts = append(parts, fmt.Sprintf(i18n.T(lang, chunk.pluralFormatKey), chunk.amount))
|
||||
}
|
||||
|
||||
translatedText := i18n.TP(lang, chunk.key, chunk.amount, chunk.amount)
|
||||
parts = append(parts, translatedText)
|
||||
}
|
||||
|
||||
if len(parts) > 1 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue