Skip to content

Commit 32fc44a

Browse files
lafrikslunny
authored andcommitted
Make time diff translatable (#2057)
1 parent 9d8fba6 commit 32fc44a

File tree

9 files changed

+67
-101
lines changed

9 files changed

+67
-101
lines changed

models/mail.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func SendTestMail(email string) error {
4747
func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject, info string) {
4848
data := map[string]interface{}{
4949
"Username": u.DisplayName(),
50-
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives),
51-
"ResetPwdCodeLives": base.MinutesToFriendly(setting.Service.ResetPwdCodeLives),
50+
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, c.Locale.Language()),
51+
"ResetPwdCodeLives": base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, c.Locale.Language()),
5252
"Code": code,
5353
}
5454

@@ -79,7 +79,7 @@ func SendResetPasswordMail(c *macaron.Context, u *User) {
7979
func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
8080
data := map[string]interface{}{
8181
"Username": u.DisplayName(),
82-
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives),
82+
"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, c.Locale.Language()),
8383
"Code": u.GenerateEmailActivateCode(email.Email),
8484
"Email": email.Email,
8585
}

modules/base/tool.go

+30-63
Original file line numberDiff line numberDiff line change
@@ -219,84 +219,84 @@ const (
219219
Year = 12 * Month
220220
)
221221

222-
func computeTimeDiff(diff int64) (int64, string) {
222+
func computeTimeDiff(diff int64, lang string) (int64, string) {
223223
diffStr := ""
224224
switch {
225225
case diff <= 0:
226226
diff = 0
227-
diffStr = "now"
227+
diffStr = i18n.Tr(lang, "tool.now")
228228
case diff < 2:
229229
diff = 0
230-
diffStr = "1 second"
230+
diffStr = i18n.Tr(lang, "tool.1s")
231231
case diff < 1*Minute:
232-
diffStr = fmt.Sprintf("%d seconds", diff)
232+
diffStr = i18n.Tr(lang, "tool.seconds", diff)
233233
diff = 0
234234

235235
case diff < 2*Minute:
236236
diff -= 1 * Minute
237-
diffStr = "1 minute"
237+
diffStr = i18n.Tr(lang, "tool.1m")
238238
case diff < 1*Hour:
239-
diffStr = fmt.Sprintf("%d minutes", diff/Minute)
239+
diffStr = i18n.Tr(lang, "tool.minutes", diff/Minute)
240240
diff -= diff / Minute * Minute
241241

242242
case diff < 2*Hour:
243243
diff -= 1 * Hour
244-
diffStr = "1 hour"
244+
diffStr = i18n.Tr(lang, "tool.1h")
245245
case diff < 1*Day:
246-
diffStr = fmt.Sprintf("%d hours", diff/Hour)
246+
diffStr = i18n.Tr(lang, "tool.hours", diff/Hour)
247247
diff -= diff / Hour * Hour
248248

249249
case diff < 2*Day:
250250
diff -= 1 * Day
251-
diffStr = "1 day"
251+
diffStr = i18n.Tr(lang, "tool.1d")
252252
case diff < 1*Week:
253-
diffStr = fmt.Sprintf("%d days", diff/Day)
253+
diffStr = i18n.Tr(lang, "tool.days", diff/Day)
254254
diff -= diff / Day * Day
255255

256256
case diff < 2*Week:
257257
diff -= 1 * Week
258-
diffStr = "1 week"
258+
diffStr = i18n.Tr(lang, "tool.1w")
259259
case diff < 1*Month:
260-
diffStr = fmt.Sprintf("%d weeks", diff/Week)
260+
diffStr = i18n.Tr(lang, "tool.weeks", diff/Week)
261261
diff -= diff / Week * Week
262262

263263
case diff < 2*Month:
264264
diff -= 1 * Month
265-
diffStr = "1 month"
265+
diffStr = i18n.Tr(lang, "tool.1mon")
266266
case diff < 1*Year:
267-
diffStr = fmt.Sprintf("%d months", diff/Month)
267+
diffStr = i18n.Tr(lang, "tool.months", diff/Month)
268268
diff -= diff / Month * Month
269269

270270
case diff < 2*Year:
271271
diff -= 1 * Year
272-
diffStr = "1 year"
272+
diffStr = i18n.Tr(lang, "tool.1y")
273273
default:
274-
diffStr = fmt.Sprintf("%d years", diff/Year)
274+
diffStr = i18n.Tr(lang, "tool.years", diff/Year)
275275
diff -= (diff / Year) * Year
276276
}
277277
return diff, diffStr
278278
}
279279

280280
// MinutesToFriendly returns a user friendly string with number of minutes
281281
// converted to hours and minutes.
282-
func MinutesToFriendly(minutes int) string {
282+
func MinutesToFriendly(minutes int, lang string) string {
283283
duration := time.Duration(minutes) * time.Minute
284-
return TimeSincePro(time.Now().Add(-duration))
284+
return TimeSincePro(time.Now().Add(-duration), lang)
285285
}
286286

287287
// TimeSincePro calculates the time interval and generate full user-friendly string.
288-
func TimeSincePro(then time.Time) string {
289-
return timeSincePro(then, time.Now())
288+
func TimeSincePro(then time.Time, lang string) string {
289+
return timeSincePro(then, time.Now(), lang)
290290
}
291291

292-
func timeSincePro(then, now time.Time) string {
292+
func timeSincePro(then, now time.Time, lang string) string {
293293
diff := now.Unix() - then.Unix()
294294

295295
if then.After(now) {
296-
return "future"
296+
return i18n.Tr(lang, "tool.future")
297297
}
298298
if diff == 0 {
299-
return "now"
299+
return i18n.Tr(lang, "tool.now")
300300
}
301301

302302
var timeStr, diffStr string
@@ -305,58 +305,25 @@ func timeSincePro(then, now time.Time) string {
305305
break
306306
}
307307

308-
diff, diffStr = computeTimeDiff(diff)
308+
diff, diffStr = computeTimeDiff(diff, lang)
309309
timeStr += ", " + diffStr
310310
}
311311
return strings.TrimPrefix(timeStr, ", ")
312312
}
313313

314314
func timeSince(then, now time.Time, lang string) string {
315-
lbl := i18n.Tr(lang, "tool.ago")
315+
lbl := "tool.ago"
316316
diff := now.Unix() - then.Unix()
317317
if then.After(now) {
318-
lbl = i18n.Tr(lang, "tool.from_now")
318+
lbl = "tool.from_now"
319319
diff = then.Unix() - now.Unix()
320320
}
321-
322-
switch {
323-
case diff <= 0:
321+
if diff <= 0 {
324322
return i18n.Tr(lang, "tool.now")
325-
case diff <= 1:
326-
return i18n.Tr(lang, "tool.1s", lbl)
327-
case diff < 1*Minute:
328-
return i18n.Tr(lang, "tool.seconds", diff, lbl)
329-
330-
case diff < 2*Minute:
331-
return i18n.Tr(lang, "tool.1m", lbl)
332-
case diff < 1*Hour:
333-
return i18n.Tr(lang, "tool.minutes", diff/Minute, lbl)
334-
335-
case diff < 2*Hour:
336-
return i18n.Tr(lang, "tool.1h", lbl)
337-
case diff < 1*Day:
338-
return i18n.Tr(lang, "tool.hours", diff/Hour, lbl)
339-
340-
case diff < 2*Day:
341-
return i18n.Tr(lang, "tool.1d", lbl)
342-
case diff < 1*Week:
343-
return i18n.Tr(lang, "tool.days", diff/Day, lbl)
344-
345-
case diff < 2*Week:
346-
return i18n.Tr(lang, "tool.1w", lbl)
347-
case diff < 1*Month:
348-
return i18n.Tr(lang, "tool.weeks", diff/Week, lbl)
349-
350-
case diff < 2*Month:
351-
return i18n.Tr(lang, "tool.1mon", lbl)
352-
case diff < 1*Year:
353-
return i18n.Tr(lang, "tool.months", diff/Month, lbl)
354-
355-
case diff < 2*Year:
356-
return i18n.Tr(lang, "tool.1y", lbl)
357-
default:
358-
return i18n.Tr(lang, "tool.years", diff/Year, lbl)
359323
}
324+
325+
_, diffStr := computeTimeDiff(diff, lang)
326+
return i18n.Tr(lang, lbl, diffStr)
360327
}
361328

362329
// RawTimeSince retrieves i18n key of time since t

modules/base/tool_test.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestComputeTimeDiff(t *testing.T) {
145145
// computeTimeDiff(base + offset) == (offset, str)
146146
test := func(base int64, str string, offsets ...int64) {
147147
for _, offset := range offsets {
148-
diff, diffStr := computeTimeDiff(base + offset)
148+
diff, diffStr := computeTimeDiff(base+offset, "en")
149149
assert.Equal(t, offset, diff)
150150
assert.Equal(t, str, diffStr)
151151
}
@@ -170,7 +170,7 @@ func TestComputeTimeDiff(t *testing.T) {
170170
func TestMinutesToFriendly(t *testing.T) {
171171
// test that a number of minutes yields the expected string
172172
test := func(expected string, minutes int) {
173-
actual := MinutesToFriendly(minutes)
173+
actual := MinutesToFriendly(minutes, "en")
174174
assert.Equal(t, expected, actual)
175175
}
176176
test("1 minute", 1)
@@ -186,13 +186,11 @@ func TestTimeSince(t *testing.T) {
186186

187187
// test that each diff in `diffs` yields the expected string
188188
test := func(expected string, diffs ...time.Duration) {
189-
ago := i18n.Tr("en", "tool.ago")
190-
fromNow := i18n.Tr("en", "tool.from_now")
191189
for _, diff := range diffs {
192190
actual := timeSince(BaseDate, BaseDate.Add(diff), "en")
193-
assert.Equal(t, expected+" "+ago, actual)
191+
assert.Equal(t, i18n.Tr("en", "tool.ago", expected), actual)
194192
actual = timeSince(BaseDate.Add(diff), BaseDate, "en")
195-
assert.Equal(t, expected+" "+fromNow, actual)
193+
assert.Equal(t, i18n.Tr("en", "tool.from_now", expected), actual)
196194
}
197195
}
198196
test("1 second", time.Second, time.Second+50*time.Millisecond)
@@ -212,13 +210,13 @@ func TestTimeSince(t *testing.T) {
212210
}
213211

214212
func TestTimeSincePro(t *testing.T) {
215-
assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate))
213+
assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate, "en"))
216214

217215
// test that a difference of `diff` yields the expected string
218216
test := func(expected string, diff time.Duration) {
219-
actual := timeSincePro(BaseDate, BaseDate.Add(diff))
217+
actual := timeSincePro(BaseDate, BaseDate.Add(diff), "en")
220218
assert.Equal(t, expected, actual)
221-
assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate))
219+
assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate, "en"))
222220
}
223221
test("1 second", time.Second)
224222
test("2 seconds", 2*time.Second)

options/locale/locale_en-US.ini

+17-16
Original file line numberDiff line numberDiff line change
@@ -1385,23 +1385,24 @@ push_tag = pushed tag <a href="%s/src/%s">%[2]s</a> to <a href="%[1]s">%[3]s</a>
13851385
compare_commits = Compare %d commits
13861386
13871387
[tool]
1388-
ago = ago
1389-
from_now = from now
1388+
ago = %s ago
1389+
from_now = %s from now
13901390
now = now
1391-
1s = 1 second %s
1392-
1m = 1 minute %s
1393-
1h = 1 hour %s
1394-
1d = 1 day %s
1395-
1w = 1 week %s
1396-
1mon = 1 month %s
1397-
1y = 1 year %s
1398-
seconds = %d seconds %s
1399-
minutes = %d minutes %s
1400-
hours = %d hours %s
1401-
days = %d days %s
1402-
weeks = %d weeks %s
1403-
months = %d months %s
1404-
years = %d years %s
1391+
future = future
1392+
1s = 1 second
1393+
1m = 1 minute
1394+
1h = 1 hour
1395+
1d = 1 day
1396+
1w = 1 week
1397+
1mon = 1 month
1398+
1y = 1 year
1399+
seconds = %d seconds
1400+
minutes = %d minutes
1401+
hours = %d hours
1402+
days = %d days
1403+
weeks = %d weeks
1404+
months = %d months
1405+
years = %d years
14051406
raw_seconds = seconds
14061407
raw_minutes = minutes
14071408

routers/admin/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var sysStatus struct {
7373
}
7474

7575
func updateSystemStatus() {
76-
sysStatus.Uptime = base.TimeSincePro(startTime)
76+
sysStatus.Uptime = base.TimeSincePro(startTime, "en")
7777

7878
m := new(runtime.MemStats)
7979
runtime.ReadMemStats(m)

routers/dev/template.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func TemplatePreview(ctx *context.Context) {
1818
ctx.Data["AppVer"] = setting.AppVer
1919
ctx.Data["AppUrl"] = setting.AppURL
2020
ctx.Data["Code"] = "2014031910370000009fff6782aadb2162b4a997acb69d4400888e0b9274657374"
21-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives)
22-
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives)
21+
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
22+
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
2323
ctx.Data["CurDbValue"] = ""
2424

2525
ctx.HTML(200, base.TplName(ctx.Params("*")))

routers/user/auth.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
677677
models.SendActivateAccountMail(ctx.Context, u)
678678
ctx.Data["IsSendRegisterMail"] = true
679679
ctx.Data["Email"] = u.Email
680-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives)
680+
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
681681
ctx.HTML(200, TplActivate)
682682

683683
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
792792
models.SendActivateAccountMail(ctx.Context, u)
793793
ctx.Data["IsSendRegisterMail"] = true
794794
ctx.Data["Email"] = u.Email
795-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives)
795+
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
796796
ctx.HTML(200, TplActivate)
797797

798798
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
@@ -818,7 +818,7 @@ func Activate(ctx *context.Context) {
818818
if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
819819
ctx.Data["ResendLimited"] = true
820820
} else {
821-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives)
821+
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
822822
models.SendActivateAccountMail(ctx.Context, ctx.User)
823823

824824
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
@@ -913,7 +913,7 @@ func ForgotPasswdPost(ctx *context.Context) {
913913
u, err := models.GetUserByEmail(email)
914914
if err != nil {
915915
if models.IsErrUserNotExist(err) {
916-
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives)
916+
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
917917
ctx.Data["IsResetSent"] = true
918918
ctx.HTML(200, tplForgotPassword)
919919
return
@@ -940,7 +940,7 @@ func ForgotPasswdPost(ctx *context.Context) {
940940
log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
941941
}
942942

943-
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives)
943+
ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
944944
ctx.Data["IsResetSent"] = true
945945
ctx.HTML(200, tplForgotPassword)
946946
}

routers/user/auth_openid.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
415415
models.SendActivateAccountMail(ctx.Context, u)
416416
ctx.Data["IsSendRegisterMail"] = true
417417
ctx.Data["Email"] = u.Email
418-
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives)
418+
ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
419419
ctx.HTML(200, TplActivate)
420420

421421
if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {

routers/user/setting.go

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

0 commit comments

Comments
 (0)