summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2017-06-28 08:43:28 +0300
committerLunny Xiao <xiaolunwen@gmail.com>2017-06-28 13:43:28 +0800
commit32fc44aa83e61acb93dbdcf6d47dfadc69f5c044 (patch)
tree60c08a5a57d10e710c36d27685f199af465f0b97 /modules
parent9d8fba62b37aee328f47411d1e187ca8f18396e4 (diff)
downloadgitea-32fc44aa83e61acb93dbdcf6d47dfadc69f5c044.tar.gz
gitea-32fc44aa83e61acb93dbdcf6d47dfadc69f5c044.zip
Make time diff translatable (#2057)
Diffstat (limited to 'modules')
-rw-r--r--modules/base/tool.go93
-rw-r--r--modules/base/tool_test.go16
2 files changed, 37 insertions, 72 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index 3fe5a3d84f..8952e7a8c9 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -219,59 +219,59 @@ const (
Year = 12 * Month
)
-func computeTimeDiff(diff int64) (int64, string) {
+func computeTimeDiff(diff int64, lang string) (int64, string) {
diffStr := ""
switch {
case diff <= 0:
diff = 0
- diffStr = "now"
+ diffStr = i18n.Tr(lang, "tool.now")
case diff < 2:
diff = 0
- diffStr = "1 second"
+ diffStr = i18n.Tr(lang, "tool.1s")
case diff < 1*Minute:
- diffStr = fmt.Sprintf("%d seconds", diff)
+ diffStr = i18n.Tr(lang, "tool.seconds", diff)
diff = 0
case diff < 2*Minute:
diff -= 1 * Minute
- diffStr = "1 minute"
+ diffStr = i18n.Tr(lang, "tool.1m")
case diff < 1*Hour:
- diffStr = fmt.Sprintf("%d minutes", diff/Minute)
+ diffStr = i18n.Tr(lang, "tool.minutes", diff/Minute)
diff -= diff / Minute * Minute
case diff < 2*Hour:
diff -= 1 * Hour
- diffStr = "1 hour"
+ diffStr = i18n.Tr(lang, "tool.1h")
case diff < 1*Day:
- diffStr = fmt.Sprintf("%d hours", diff/Hour)
+ diffStr = i18n.Tr(lang, "tool.hours", diff/Hour)
diff -= diff / Hour * Hour
case diff < 2*Day:
diff -= 1 * Day
- diffStr = "1 day"
+ diffStr = i18n.Tr(lang, "tool.1d")
case diff < 1*Week:
- diffStr = fmt.Sprintf("%d days", diff/Day)
+ diffStr = i18n.Tr(lang, "tool.days", diff/Day)
diff -= diff / Day * Day
case diff < 2*Week:
diff -= 1 * Week
- diffStr = "1 week"
+ diffStr = i18n.Tr(lang, "tool.1w")
case diff < 1*Month:
- diffStr = fmt.Sprintf("%d weeks", diff/Week)
+ diffStr = i18n.Tr(lang, "tool.weeks", diff/Week)
diff -= diff / Week * Week
case diff < 2*Month:
diff -= 1 * Month
- diffStr = "1 month"
+ diffStr = i18n.Tr(lang, "tool.1mon")
case diff < 1*Year:
- diffStr = fmt.Sprintf("%d months", diff/Month)
+ diffStr = i18n.Tr(lang, "tool.months", diff/Month)
diff -= diff / Month * Month
case diff < 2*Year:
diff -= 1 * Year
- diffStr = "1 year"
+ diffStr = i18n.Tr(lang, "tool.1y")
default:
- diffStr = fmt.Sprintf("%d years", diff/Year)
+ diffStr = i18n.Tr(lang, "tool.years", diff/Year)
diff -= (diff / Year) * Year
}
return diff, diffStr
@@ -279,24 +279,24 @@ func computeTimeDiff(diff int64) (int64, string) {
// MinutesToFriendly returns a user friendly string with number of minutes
// converted to hours and minutes.
-func MinutesToFriendly(minutes int) string {
+func MinutesToFriendly(minutes int, lang string) string {
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.
-func TimeSincePro(then time.Time) string {
- return timeSincePro(then, time.Now())
+func TimeSincePro(then time.Time, lang string) string {
+ 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()
if then.After(now) {
- return "future"
+ return i18n.Tr(lang, "tool.future")
}
if diff == 0 {
- return "now"
+ return i18n.Tr(lang, "tool.now")
}
var timeStr, diffStr string
@@ -305,58 +305,25 @@ func timeSincePro(then, now time.Time) string {
break
}
- diff, diffStr = computeTimeDiff(diff)
+ diff, diffStr = computeTimeDiff(diff, lang)
timeStr += ", " + diffStr
}
return strings.TrimPrefix(timeStr, ", ")
}
func timeSince(then, now time.Time, lang string) string {
- lbl := i18n.Tr(lang, "tool.ago")
+ lbl := "tool.ago"
diff := now.Unix() - then.Unix()
if then.After(now) {
- lbl = i18n.Tr(lang, "tool.from_now")
+ lbl = "tool.from_now"
diff = then.Unix() - now.Unix()
}
-
- switch {
- case diff <= 0:
+ if diff <= 0 {
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
diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go
index bd9c6e276e..f3e787dd13 100644
--- a/modules/base/tool_test.go
+++ b/modules/base/tool_test.go
@@ -145,7 +145,7 @@ func TestComputeTimeDiff(t *testing.T) {
// computeTimeDiff(base + offset) == (offset, str)
test := func(base int64, str string, offsets ...int64) {
for _, offset := range offsets {
- diff, diffStr := computeTimeDiff(base + offset)
+ diff, diffStr := computeTimeDiff(base+offset, "en")
assert.Equal(t, offset, diff)
assert.Equal(t, str, diffStr)
}
@@ -170,7 +170,7 @@ func TestComputeTimeDiff(t *testing.T) {
func TestMinutesToFriendly(t *testing.T) {
// test that a number of minutes yields the expected string
test := func(expected string, minutes int) {
- actual := MinutesToFriendly(minutes)
+ actual := MinutesToFriendly(minutes, "en")
assert.Equal(t, expected, actual)
}
test("1 minute", 1)
@@ -186,13 +186,11 @@ func TestTimeSince(t *testing.T) {
// test that each diff in `diffs` yields the expected string
test := func(expected string, diffs ...time.Duration) {
- ago := i18n.Tr("en", "tool.ago")
- fromNow := i18n.Tr("en", "tool.from_now")
for _, diff := range diffs {
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")
- 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)
@@ -212,13 +210,13 @@ func TestTimeSince(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 := 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, "future", timeSincePro(BaseDate.Add(diff), BaseDate))
+ assert.Equal(t, "future", timeSincePro(BaseDate.Add(diff), BaseDate, "en"))
}
test("1 second", time.Second)
test("2 seconds", 2*time.Second)