From d8e73a5ee1f61dba195a54974d0a378010fb00d5 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 14 Apr 2023 23:38:37 +0800 Subject: [PATCH] fix lint --- modules/timeutil/since.go | 22 +++++++++++++++++----- options/locale/locale_en-US.ini | 1 + templates/devtest/gitea-ui.tmpl | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/modules/timeutil/since.go b/modules/timeutil/since.go index e6a2519d2120b..3b113a1c937b6 100644 --- a/modules/timeutil/since.go +++ b/modules/timeutil/since.go @@ -114,7 +114,7 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string { return strings.TrimPrefix(timeStr, ", ") } -func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML { +func timeSinceUnix(then, now time.Time, lang translation.Locale, attrsMap ...map[string]any) template.HTML { friendlyText := then.Format("2006-01-02 15:04:05 +07:00") // document: https://github.com/github/relative-time-element @@ -124,18 +124,30 @@ func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML { attrs = `tense="future"` } + transOnDate := "" + if len(attrsMap) == 1 { + m := attrsMap[0] + if m["tense"] == "auto" { + attrs = `tense="auto"` // "relative-time" doesn't support i18n of "prefix", so we use our translation + transOnDate = lang.Tr("tool.on_date") + } + } + // declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip htm := fmt.Sprintf(`%s`, attrs, then.Format(time.RFC3339), friendlyText) + if transOnDate != "" { + htm = fmt.Sprintf(transOnDate, htm) + } return template.HTML(htm) } // TimeSince renders relative time HTML given a time.Time -func TimeSince(then time.Time, lang translation.Locale) template.HTML { - return timeSinceUnix(then, time.Now(), lang) +func TimeSince(then time.Time, lang translation.Locale, attrs ...map[string]any) template.HTML { + return timeSinceUnix(then, time.Now(), lang, attrs...) } // TimeSinceUnix renders relative time HTML given a TimeStamp -func TimeSinceUnix(then TimeStamp, lang translation.Locale) template.HTML { - return TimeSince(then.AsLocalTime(), lang) +func TimeSinceUnix(then TimeStamp, lang translation.Locale, extra ...map[string]any) template.HTML { + return TimeSince(then.AsLocalTime(), lang, extra...) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index c2c8f1e120c1f..4930f4eb407b6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3127,6 +3127,7 @@ starred_repo = starred %[2]s watched_repo = started watching %[2]s [tool] +on_date = on %s now = now future = future 1s = 1 second diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index 1ab9ae7b7c7aa..102e75c4c1f37 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -33,6 +33,8 @@
2m future: {{TimeSince .TimeFuture2m $.locale}}
1y past: {{TimeSince .TimePast1y $.locale}}
1y future: {{TimeSince .TimeFuture1y $.locale}}
+
1y past (tense=auto): {{TimeSince .TimePast1y $.locale (dict "tense" "auto")}}
+
1y future (tense=auto): {{TimeSince .TimeFuture1y $.locale (dict "tense" "auto")}}