Make time diff translatable (#2057)

This commit is contained in:
Lauris BH 2017-06-28 08:43:28 +03:00 committed by Lunny Xiao
parent 9d8fba62b3
commit 32fc44aa83
9 changed files with 67 additions and 101 deletions

View file

@ -47,8 +47,8 @@ func SendTestMail(email string) error {
func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject, info string) { func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject, info string) {
data := map[string]interface{}{ data := map[string]interface{}{
"Username": u.DisplayName(), "Username": u.DisplayName(),
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives), "ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, c.Locale.Language()),
"ResetPwdCodeLives": base.MinutesToFriendly(setting.Service.ResetPwdCodeLives), "ResetPwdCodeLives": base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, c.Locale.Language()),
"Code": code, "Code": code,
} }
@ -79,7 +79,7 @@ func SendResetPasswordMail(c *macaron.Context, u *User) {
func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) { func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
data := map[string]interface{}{ data := map[string]interface{}{
"Username": u.DisplayName(), "Username": u.DisplayName(),
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives), "ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, c.Locale.Language()),
"Code": u.GenerateEmailActivateCode(email.Email), "Code": u.GenerateEmailActivateCode(email.Email),
"Email": email.Email, "Email": email.Email,
} }

View file

@ -219,59 +219,59 @@ const (
Year = 12 * Month Year = 12 * Month
) )
func computeTimeDiff(diff int64) (int64, string) { func computeTimeDiff(diff int64, lang string) (int64, string) {
diffStr := "" diffStr := ""
switch { switch {
case diff <= 0: case diff <= 0:
diff = 0 diff = 0
diffStr = "now" diffStr = i18n.Tr(lang, "tool.now")
case diff < 2: case diff < 2:
diff = 0 diff = 0
diffStr = "1 second" diffStr = i18n.Tr(lang, "tool.1s")
case diff < 1*Minute: case diff < 1*Minute:
diffStr = fmt.Sprintf("%d seconds", diff) diffStr = i18n.Tr(lang, "tool.seconds", diff)
diff = 0 diff = 0
case diff < 2*Minute: case diff < 2*Minute:
diff -= 1 * Minute diff -= 1 * Minute
diffStr = "1 minute" diffStr = i18n.Tr(lang, "tool.1m")
case diff < 1*Hour: case diff < 1*Hour:
diffStr = fmt.Sprintf("%d minutes", diff/Minute) diffStr = i18n.Tr(lang, "tool.minutes", diff/Minute)
diff -= diff / Minute * Minute diff -= diff / Minute * Minute
case diff < 2*Hour: case diff < 2*Hour:
diff -= 1 * Hour diff -= 1 * Hour
diffStr = "1 hour" diffStr = i18n.Tr(lang, "tool.1h")
case diff < 1*Day: case diff < 1*Day:
diffStr = fmt.Sprintf("%d hours", diff/Hour) diffStr = i18n.Tr(lang, "tool.hours", diff/Hour)
diff -= diff / Hour * Hour diff -= diff / Hour * Hour
case diff < 2*Day: case diff < 2*Day:
diff -= 1 * Day diff -= 1 * Day
diffStr = "1 day" diffStr = i18n.Tr(lang, "tool.1d")
case diff < 1*Week: case diff < 1*Week:
diffStr = fmt.Sprintf("%d days", diff/Day) diffStr = i18n.Tr(lang, "tool.days", diff/Day)
diff -= diff / Day * Day diff -= diff / Day * Day
case diff < 2*Week: case diff < 2*Week:
diff -= 1 * Week diff -= 1 * Week
diffStr = "1 week" diffStr = i18n.Tr(lang, "tool.1w")
case diff < 1*Month: case diff < 1*Month:
diffStr = fmt.Sprintf("%d weeks", diff/Week) diffStr = i18n.Tr(lang, "tool.weeks", diff/Week)
diff -= diff / Week * Week diff -= diff / Week * Week
case diff < 2*Month: case diff < 2*Month:
diff -= 1 * Month diff -= 1 * Month
diffStr = "1 month" diffStr = i18n.Tr(lang, "tool.1mon")
case diff < 1*Year: case diff < 1*Year:
diffStr = fmt.Sprintf("%d months", diff/Month) diffStr = i18n.Tr(lang, "tool.months", diff/Month)
diff -= diff / Month * Month diff -= diff / Month * Month
case diff < 2*Year: case diff < 2*Year:
diff -= 1 * Year diff -= 1 * Year
diffStr = "1 year" diffStr = i18n.Tr(lang, "tool.1y")
default: default:
diffStr = fmt.Sprintf("%d years", diff/Year) diffStr = i18n.Tr(lang, "tool.years", diff/Year)
diff -= (diff / Year) * Year diff -= (diff / Year) * Year
} }
return diff, diffStr return diff, diffStr
@ -279,24 +279,24 @@ func computeTimeDiff(diff int64) (int64, string) {
// MinutesToFriendly returns a user friendly string with number of minutes // MinutesToFriendly returns a user friendly string with number of minutes
// converted to hours and minutes. // converted to hours and minutes.
func MinutesToFriendly(minutes int) string { func MinutesToFriendly(minutes int, lang string) string {
duration := time.Duration(minutes) * time.Minute duration := time.Duration(minutes) * time.Minute
return TimeSincePro(time.Now().Add(-duration)) return TimeSincePro(time.Now().Add(-duration), lang)
} }
// TimeSincePro calculates the time interval and generate full user-friendly string. // TimeSincePro calculates the time interval and generate full user-friendly string.
func TimeSincePro(then time.Time) string { func TimeSincePro(then time.Time, lang string) string {
return timeSincePro(then, time.Now()) return timeSincePro(then, time.Now(), lang)
} }
func timeSincePro(then, now time.Time) string { func timeSincePro(then, now time.Time, lang string) string {
diff := now.Unix() - then.Unix() diff := now.Unix() - then.Unix()
if then.After(now) { if then.After(now) {
return "future" return i18n.Tr(lang, "tool.future")
} }
if diff == 0 { if diff == 0 {
return "now" return i18n.Tr(lang, "tool.now")
} }
var timeStr, diffStr string var timeStr, diffStr string
@ -305,58 +305,25 @@ func timeSincePro(then, now time.Time) string {
break break
} }
diff, diffStr = computeTimeDiff(diff) diff, diffStr = computeTimeDiff(diff, lang)
timeStr += ", " + diffStr timeStr += ", " + diffStr
} }
return strings.TrimPrefix(timeStr, ", ") return strings.TrimPrefix(timeStr, ", ")
} }
func timeSince(then, now time.Time, lang string) string { func timeSince(then, now time.Time, lang string) string {
lbl := i18n.Tr(lang, "tool.ago") lbl := "tool.ago"
diff := now.Unix() - then.Unix() diff := now.Unix() - then.Unix()
if then.After(now) { if then.After(now) {
lbl = i18n.Tr(lang, "tool.from_now") lbl = "tool.from_now"
diff = then.Unix() - now.Unix() diff = then.Unix() - now.Unix()
} }
if diff <= 0 {
switch {
case diff <= 0:
return i18n.Tr(lang, "tool.now") return i18n.Tr(lang, "tool.now")
case diff <= 1:
return i18n.Tr(lang, "tool.1s", lbl)
case diff < 1*Minute:
return i18n.Tr(lang, "tool.seconds", diff, lbl)
case diff < 2*Minute:
return i18n.Tr(lang, "tool.1m", lbl)
case diff < 1*Hour:
return i18n.Tr(lang, "tool.minutes", diff/Minute, lbl)
case diff < 2*Hour:
return i18n.Tr(lang, "tool.1h", lbl)
case diff < 1*Day:
return i18n.Tr(lang, "tool.hours", diff/Hour, lbl)
case diff < 2*Day:
return i18n.Tr(lang, "tool.1d", lbl)
case diff < 1*Week:
return i18n.Tr(lang, "tool.days", diff/Day, lbl)
case diff < 2*Week:
return i18n.Tr(lang, "tool.1w", lbl)
case diff < 1*Month:
return i18n.Tr(lang, "tool.weeks", diff/Week, lbl)
case diff < 2*Month:
return i18n.Tr(lang, "tool.1mon", lbl)
case diff < 1*Year:
return i18n.Tr(lang, "tool.months", diff/Month, lbl)
case diff < 2*Year:
return i18n.Tr(lang, "tool.1y", lbl)
default:
return i18n.Tr(lang, "tool.years", diff/Year, lbl)
} }
_, diffStr := computeTimeDiff(diff, lang)
return i18n.Tr(lang, lbl, diffStr)
} }
// RawTimeSince retrieves i18n key of time since t // RawTimeSince retrieves i18n key of time since t

View file

@ -145,7 +145,7 @@ func TestComputeTimeDiff(t *testing.T) {
// computeTimeDiff(base + offset) == (offset, str) // computeTimeDiff(base + offset) == (offset, str)
test := func(base int64, str string, offsets ...int64) { test := func(base int64, str string, offsets ...int64) {
for _, offset := range offsets { for _, offset := range offsets {
diff, diffStr := computeTimeDiff(base + offset) diff, diffStr := computeTimeDiff(base+offset, "en")
assert.Equal(t, offset, diff) assert.Equal(t, offset, diff)
assert.Equal(t, str, diffStr) assert.Equal(t, str, diffStr)
} }
@ -170,7 +170,7 @@ func TestComputeTimeDiff(t *testing.T) {
func TestMinutesToFriendly(t *testing.T) { func TestMinutesToFriendly(t *testing.T) {
// test that a number of minutes yields the expected string // test that a number of minutes yields the expected string
test := func(expected string, minutes int) { test := func(expected string, minutes int) {
actual := MinutesToFriendly(minutes) actual := MinutesToFriendly(minutes, "en")
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
test("1 minute", 1) test("1 minute", 1)
@ -186,13 +186,11 @@ func TestTimeSince(t *testing.T) {
// test that each diff in `diffs` yields the expected string // test that each diff in `diffs` yields the expected string
test := func(expected string, diffs ...time.Duration) { test := func(expected string, diffs ...time.Duration) {
ago := i18n.Tr("en", "tool.ago")
fromNow := i18n.Tr("en", "tool.from_now")
for _, diff := range diffs { for _, diff := range diffs {
actual := timeSince(BaseDate, BaseDate.Add(diff), "en") actual := timeSince(BaseDate, BaseDate.Add(diff), "en")
assert.Equal(t, expected+" "+ago, actual) assert.Equal(t, i18n.Tr("en", "tool.ago", expected), actual)
actual = timeSince(BaseDate.Add(diff), BaseDate, "en") actual = timeSince(BaseDate.Add(diff), BaseDate, "en")
assert.Equal(t, expected+" "+fromNow, actual) assert.Equal(t, i18n.Tr("en", "tool.from_now", expected), actual)
} }
} }
test("1 second", time.Second, time.Second+50*time.Millisecond) test("1 second", time.Second, time.Second+50*time.Millisecond)
@ -212,13 +210,13 @@ func TestTimeSince(t *testing.T) {
} }
func TestTimeSincePro(t *testing.T) { func TestTimeSincePro(t *testing.T) {
assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate)) assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate, "en"))
// test that a difference of `diff` yields the expected string // test that a difference of `diff` yields the expected string
test := func(expected string, diff time.Duration) { test := func(expected string, diff time.Duration) {
actual := timeSincePro(BaseDate, BaseDate.Add(diff)) actual := timeSincePro(BaseDate, BaseDate.Add(diff), "en")
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate)) assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate, "en"))
} }
test("1 second", time.Second) test("1 second", time.Second)
test("2 seconds", 2*time.Second) test("2 seconds", 2*time.Second)

View file

@ -1385,23 +1385,24 @@ push_tag = pushed tag <a href="%s/src/%s">%[2]s</a> to <a href="%[1]s">%[3]s</a>
compare_commits = Compare %d commits compare_commits = Compare %d commits
[tool] [tool]
ago = ago ago = %s ago
from_now = from now from_now = %s from now
now = now now = now
1s = 1 second %s future = future
1m = 1 minute %s 1s = 1 second
1h = 1 hour %s 1m = 1 minute
1d = 1 day %s 1h = 1 hour
1w = 1 week %s 1d = 1 day
1mon = 1 month %s 1w = 1 week
1y = 1 year %s 1mon = 1 month
seconds = %d seconds %s 1y = 1 year
minutes = %d minutes %s seconds = %d seconds
hours = %d hours %s minutes = %d minutes
days = %d days %s hours = %d hours
weeks = %d weeks %s days = %d days
months = %d months %s weeks = %d weeks
years = %d years %s months = %d months
years = %d years
raw_seconds = seconds raw_seconds = seconds
raw_minutes = minutes raw_minutes = minutes

View file

@ -73,7 +73,7 @@ var sysStatus struct {
} }
func updateSystemStatus() { func updateSystemStatus() {
sysStatus.Uptime = base.TimeSincePro(startTime) sysStatus.Uptime = base.TimeSincePro(startTime, "en")
m := new(runtime.MemStats) m := new(runtime.MemStats)
runtime.ReadMemStats(m) runtime.ReadMemStats(m)

View file

@ -18,8 +18,8 @@ func TemplatePreview(ctx *context.Context) {
ctx.Data["AppVer"] = setting.AppVer ctx.Data["AppVer"] = setting.AppVer
ctx.Data["AppUrl"] = setting.AppURL ctx.Data["AppUrl"] = setting.AppURL
ctx.Data["Code"] = "2014031910370000009fff6782aadb2162b4a997acb69d4400888e0b9274657374" ctx.Data["Code"] = "2014031910370000009fff6782aadb2162b4a997acb69d4400888e0b9274657374"
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives) ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives) ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
ctx.Data["CurDbValue"] = "" ctx.Data["CurDbValue"] = ""
ctx.HTML(200, base.TplName(ctx.Params("*"))) ctx.HTML(200, base.TplName(ctx.Params("*")))

View file

@ -677,7 +677,7 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
models.SendActivateAccountMail(ctx.Context, u) models.SendActivateAccountMail(ctx.Context, u)
ctx.Data["IsSendRegisterMail"] = true ctx.Data["IsSendRegisterMail"] = true
ctx.Data["Email"] = u.Email ctx.Data["Email"] = u.Email
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives) ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
ctx.HTML(200, TplActivate) ctx.HTML(200, TplActivate)
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
@ -792,7 +792,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
models.SendActivateAccountMail(ctx.Context, u) models.SendActivateAccountMail(ctx.Context, u)
ctx.Data["IsSendRegisterMail"] = true ctx.Data["IsSendRegisterMail"] = true
ctx.Data["Email"] = u.Email ctx.Data["Email"] = u.Email
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives) ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
ctx.HTML(200, TplActivate) ctx.HTML(200, TplActivate)
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
@ -818,7 +818,7 @@ func Activate(ctx *context.Context) {
if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
ctx.Data["ResendLimited"] = true ctx.Data["ResendLimited"] = true
} else { } else {
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives) ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
models.SendActivateAccountMail(ctx.Context, ctx.User) models.SendActivateAccountMail(ctx.Context, ctx.User)
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
@ -913,7 +913,7 @@ func ForgotPasswdPost(ctx *context.Context) {
u, err := models.GetUserByEmail(email) u, err := models.GetUserByEmail(email)
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives) ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
ctx.Data["IsResetSent"] = true ctx.Data["IsResetSent"] = true
ctx.HTML(200, tplForgotPassword) ctx.HTML(200, tplForgotPassword)
return return
@ -940,7 +940,7 @@ func ForgotPasswdPost(ctx *context.Context) {
log.Error(4, "Set cache(MailResendLimit) fail: %v", err) log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
} }
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives) ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
ctx.Data["IsResetSent"] = true ctx.Data["IsResetSent"] = true
ctx.HTML(200, tplForgotPassword) ctx.HTML(200, tplForgotPassword)
} }

View file

@ -415,7 +415,7 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
models.SendActivateAccountMail(ctx.Context, u) models.SendActivateAccountMail(ctx.Context, u)
ctx.Data["IsSendRegisterMail"] = true ctx.Data["IsSendRegisterMail"] = true
ctx.Data["Email"] = u.Email ctx.Data["Email"] = u.Email
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives) ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
ctx.HTML(200, TplActivate) ctx.HTML(200, TplActivate)
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {

View file

@ -298,7 +298,7 @@ func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
log.Error(4, "Set cache(MailResendLimit) fail: %v", err) log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
} }
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, base.MinutesToFriendly(setting.Service.ActiveCodeLives))) ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
} else { } else {
ctx.Flash.Success(ctx.Tr("settings.add_email_success")) ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
} }