Browse Source

Make time diff translatable (#2057)

Lauris BH 2 years ago
parent
commit
32fc44aa83

+ 3 - 3
models/mail.go

@@ -47,8 +47,8 @@ func SendTestMail(email string) error {
47 47
 func SendUserMail(c *macaron.Context, u *User, tpl base.TplName, code, subject, info string) {
48 48
 	data := map[string]interface{}{
49 49
 		"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()),
52 52
 		"Code":              code,
53 53
 	}
54 54
 
@@ -79,7 +79,7 @@ func SendResetPasswordMail(c *macaron.Context, u *User) {
79 79
 func SendActivateEmailMail(c *macaron.Context, u *User, email *EmailAddress) {
80 80
 	data := map[string]interface{}{
81 81
 		"Username":        u.DisplayName(),
82
-		"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives),
82
+		"ActiveCodeLives": base.MinutesToFriendly(setting.Service.ActiveCodeLives, c.Locale.Language()),
83 83
 		"Code":            u.GenerateEmailActivateCode(email.Email),
84 84
 		"Email":           email.Email,
85 85
 	}

+ 30 - 63
modules/base/tool.go

@@ -219,59 +219,59 @@ const (
219 219
 	Year   = 12 * Month
220 220
 )
221 221
 
222
-func computeTimeDiff(diff int64) (int64, string) {
222
+func computeTimeDiff(diff int64, lang string) (int64, string) {
223 223
 	diffStr := ""
224 224
 	switch {
225 225
 	case diff <= 0:
226 226
 		diff = 0
227
-		diffStr = "now"
227
+		diffStr = i18n.Tr(lang, "tool.now")
228 228
 	case diff < 2:
229 229
 		diff = 0
230
-		diffStr = "1 second"
230
+		diffStr = i18n.Tr(lang, "tool.1s")
231 231
 	case diff < 1*Minute:
232
-		diffStr = fmt.Sprintf("%d seconds", diff)
232
+		diffStr = i18n.Tr(lang, "tool.seconds", diff)
233 233
 		diff = 0
234 234
 
235 235
 	case diff < 2*Minute:
236 236
 		diff -= 1 * Minute
237
-		diffStr = "1 minute"
237
+		diffStr = i18n.Tr(lang, "tool.1m")
238 238
 	case diff < 1*Hour:
239
-		diffStr = fmt.Sprintf("%d minutes", diff/Minute)
239
+		diffStr = i18n.Tr(lang, "tool.minutes", diff/Minute)
240 240
 		diff -= diff / Minute * Minute
241 241
 
242 242
 	case diff < 2*Hour:
243 243
 		diff -= 1 * Hour
244
-		diffStr = "1 hour"
244
+		diffStr = i18n.Tr(lang, "tool.1h")
245 245
 	case diff < 1*Day:
246
-		diffStr = fmt.Sprintf("%d hours", diff/Hour)
246
+		diffStr = i18n.Tr(lang, "tool.hours", diff/Hour)
247 247
 		diff -= diff / Hour * Hour
248 248
 
249 249
 	case diff < 2*Day:
250 250
 		diff -= 1 * Day
251
-		diffStr = "1 day"
251
+		diffStr = i18n.Tr(lang, "tool.1d")
252 252
 	case diff < 1*Week:
253
-		diffStr = fmt.Sprintf("%d days", diff/Day)
253
+		diffStr = i18n.Tr(lang, "tool.days", diff/Day)
254 254
 		diff -= diff / Day * Day
255 255
 
256 256
 	case diff < 2*Week:
257 257
 		diff -= 1 * Week
258
-		diffStr = "1 week"
258
+		diffStr = i18n.Tr(lang, "tool.1w")
259 259
 	case diff < 1*Month:
260
-		diffStr = fmt.Sprintf("%d weeks", diff/Week)
260
+		diffStr = i18n.Tr(lang, "tool.weeks", diff/Week)
261 261
 		diff -= diff / Week * Week
262 262
 
263 263
 	case diff < 2*Month:
264 264
 		diff -= 1 * Month
265
-		diffStr = "1 month"
265
+		diffStr = i18n.Tr(lang, "tool.1mon")
266 266
 	case diff < 1*Year:
267
-		diffStr = fmt.Sprintf("%d months", diff/Month)
267
+		diffStr = i18n.Tr(lang, "tool.months", diff/Month)
268 268
 		diff -= diff / Month * Month
269 269
 
270 270
 	case diff < 2*Year:
271 271
 		diff -= 1 * Year
272
-		diffStr = "1 year"
272
+		diffStr = i18n.Tr(lang, "tool.1y")
273 273
 	default:
274
-		diffStr = fmt.Sprintf("%d years", diff/Year)
274
+		diffStr = i18n.Tr(lang, "tool.years", diff/Year)
275 275
 		diff -= (diff / Year) * Year
276 276
 	}
277 277
 	return diff, diffStr
@@ -279,24 +279,24 @@ func computeTimeDiff(diff int64) (int64, string) {
279 279
 
280 280
 // MinutesToFriendly returns a user friendly string with number of minutes
281 281
 // converted to hours and minutes.
282
-func MinutesToFriendly(minutes int) string {
282
+func MinutesToFriendly(minutes int, lang string) string {
283 283
 	duration := time.Duration(minutes) * time.Minute
284
-	return TimeSincePro(time.Now().Add(-duration))
284
+	return TimeSincePro(time.Now().Add(-duration), lang)
285 285
 }
286 286
 
287 287
 // 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)
290 290
 }
291 291
 
292
-func timeSincePro(then, now time.Time) string {
292
+func timeSincePro(then, now time.Time, lang string) string {
293 293
 	diff := now.Unix() - then.Unix()
294 294
 
295 295
 	if then.After(now) {
296
-		return "future"
296
+		return i18n.Tr(lang, "tool.future")
297 297
 	}
298 298
 	if diff == 0 {
299
-		return "now"
299
+		return i18n.Tr(lang, "tool.now")
300 300
 	}
301 301
 
302 302
 	var timeStr, diffStr string
@@ -305,58 +305,25 @@ func timeSincePro(then, now time.Time) string {
305 305
 			break
306 306
 		}
307 307
 
308
-		diff, diffStr = computeTimeDiff(diff)
308
+		diff, diffStr = computeTimeDiff(diff, lang)
309 309
 		timeStr += ", " + diffStr
310 310
 	}
311 311
 	return strings.TrimPrefix(timeStr, ", ")
312 312
 }
313 313
 
314 314
 func timeSince(then, now time.Time, lang string) string {
315
-	lbl := i18n.Tr(lang, "tool.ago")
315
+	lbl := "tool.ago"
316 316
 	diff := now.Unix() - then.Unix()
317 317
 	if then.After(now) {
318
-		lbl = i18n.Tr(lang, "tool.from_now")
318
+		lbl = "tool.from_now"
319 319
 		diff = then.Unix() - now.Unix()
320 320
 	}
321
-
322
-	switch {
323
-	case diff <= 0:
321
+	if diff <= 0 {
324 322
 		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)
359 323
 	}
324
+
325
+	_, diffStr := computeTimeDiff(diff, lang)
326
+	return i18n.Tr(lang, lbl, diffStr)
360 327
 }
361 328
 
362 329
 // RawTimeSince retrieves i18n key of time since t

+ 7 - 9
modules/base/tool_test.go

@@ -145,7 +145,7 @@ func TestComputeTimeDiff(t *testing.T) {
145 145
 	// computeTimeDiff(base + offset) == (offset, str)
146 146
 	test := func(base int64, str string, offsets ...int64) {
147 147
 		for _, offset := range offsets {
148
-			diff, diffStr := computeTimeDiff(base + offset)
148
+			diff, diffStr := computeTimeDiff(base+offset, "en")
149 149
 			assert.Equal(t, offset, diff)
150 150
 			assert.Equal(t, str, diffStr)
151 151
 		}
@@ -170,7 +170,7 @@ func TestComputeTimeDiff(t *testing.T) {
170 170
 func TestMinutesToFriendly(t *testing.T) {
171 171
 	// test that a number of minutes yields the expected string
172 172
 	test := func(expected string, minutes int) {
173
-		actual := MinutesToFriendly(minutes)
173
+		actual := MinutesToFriendly(minutes, "en")
174 174
 		assert.Equal(t, expected, actual)
175 175
 	}
176 176
 	test("1 minute", 1)
@@ -186,13 +186,11 @@ func TestTimeSince(t *testing.T) {
186 186
 
187 187
 	// test that each diff in `diffs` yields the expected string
188 188
 	test := func(expected string, diffs ...time.Duration) {
189
-		ago := i18n.Tr("en", "tool.ago")
190
-		fromNow := i18n.Tr("en", "tool.from_now")
191 189
 		for _, diff := range diffs {
192 190
 			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)
194 192
 			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)
196 194
 		}
197 195
 	}
198 196
 	test("1 second", time.Second, time.Second+50*time.Millisecond)
@@ -212,13 +210,13 @@ func TestTimeSince(t *testing.T) {
212 210
 }
213 211
 
214 212
 func TestTimeSincePro(t *testing.T) {
215
-	assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate))
213
+	assert.Equal(t, "now", timeSincePro(BaseDate, BaseDate, "en"))
216 214
 
217 215
 	// test that a difference of `diff` yields the expected string
218 216
 	test := func(expected string, diff time.Duration) {
219
-		actual := timeSincePro(BaseDate, BaseDate.Add(diff))
217
+		actual := timeSincePro(BaseDate, BaseDate.Add(diff), "en")
220 218
 		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"))
222 220
 	}
223 221
 	test("1 second", time.Second)
224 222
 	test("2 seconds", 2*time.Second)

+ 17 - 16
options/locale/locale_en-US.ini

@@ -1385,23 +1385,24 @@ push_tag = pushed tag <a href="%s/src/%s">%[2]s</a> to <a href="%[1]s">%[3]s</a>
1385 1385
 compare_commits = Compare %d commits
1386 1386
 
1387 1387
 [tool]
1388
-ago = ago
1389
-from_now = from now
1388
+ago = %s ago
1389
+from_now = %s from now
1390 1390
 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
1405 1406
 raw_seconds = seconds
1406 1407
 raw_minutes = minutes
1407 1408
 

+ 1 - 1
routers/admin/admin.go

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

+ 2 - 2
routers/dev/template.go

@@ -18,8 +18,8 @@ func TemplatePreview(ctx *context.Context) {
18 18
 	ctx.Data["AppVer"] = setting.AppVer
19 19
 	ctx.Data["AppUrl"] = setting.AppURL
20 20
 	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())
23 23
 	ctx.Data["CurDbValue"] = ""
24 24
 
25 25
 	ctx.HTML(200, base.TplName(ctx.Params("*")))

+ 5 - 5
routers/user/auth.go

@@ -677,7 +677,7 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
677 677
 		models.SendActivateAccountMail(ctx.Context, u)
678 678
 		ctx.Data["IsSendRegisterMail"] = true
679 679
 		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())
681 681
 		ctx.HTML(200, TplActivate)
682 682
 
683 683
 		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
792 792
 		models.SendActivateAccountMail(ctx.Context, u)
793 793
 		ctx.Data["IsSendRegisterMail"] = true
794 794
 		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())
796 796
 		ctx.HTML(200, TplActivate)
797 797
 
798 798
 		if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {
@@ -818,7 +818,7 @@ func Activate(ctx *context.Context) {
818 818
 			if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
819 819
 				ctx.Data["ResendLimited"] = true
820 820
 			} else {
821
-				ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives)
821
+				ctx.Data["ActiveCodeLives"] = base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())
822 822
 				models.SendActivateAccountMail(ctx.Context, ctx.User)
823 823
 
824 824
 				if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
@@ -913,7 +913,7 @@ func ForgotPasswdPost(ctx *context.Context) {
913 913
 	u, err := models.GetUserByEmail(email)
914 914
 	if err != nil {
915 915
 		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())
917 917
 			ctx.Data["IsResetSent"] = true
918 918
 			ctx.HTML(200, tplForgotPassword)
919 919
 			return
@@ -940,7 +940,7 @@ func ForgotPasswdPost(ctx *context.Context) {
940 940
 		log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
941 941
 	}
942 942
 
943
-	ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives)
943
+	ctx.Data["ResetPwdCodeLives"] = base.MinutesToFriendly(setting.Service.ResetPwdCodeLives, ctx.Locale.Language())
944 944
 	ctx.Data["IsResetSent"] = true
945 945
 	ctx.HTML(200, tplForgotPassword)
946 946
 }

+ 1 - 1
routers/user/auth_openid.go

@@ -415,7 +415,7 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
415 415
 		models.SendActivateAccountMail(ctx.Context, u)
416 416
 		ctx.Data["IsSendRegisterMail"] = true
417 417
 		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())
419 419
 		ctx.HTML(200, TplActivate)
420 420
 
421 421
 		if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil {

+ 1 - 1
routers/user/setting.go

@@ -298,7 +298,7 @@ func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
298 298
 		if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
299 299
 			log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
300 300
 		}
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())))
302 302
 	} else {
303 303
 		ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
304 304
 	}