Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove untranslatable on_date key #24106

Merged
merged 16 commits into from
Apr 15, 2023
Prev Previous commit
Next Next commit
fix lint
wxiaoguang committed Apr 14, 2023
commit d8e73a5ee1f61dba195a54974d0a378010fb00d5
22 changes: 17 additions & 5 deletions modules/timeutil/since.go
Original file line number Diff line number Diff line change
@@ -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(`<relative-time class="time-since" prefix="" %s datetime="%s" data-tooltip-content data-tooltip-interactive="true">%s</relative-time>`,
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...)
}
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
@@ -3127,6 +3127,7 @@ starred_repo = starred <a href="%[1]s">%[2]s</a>
watched_repo = started watching <a href="%[1]s">%[2]s</a>
[tool]
on_date = on %s
now = now
future = future
1s = 1 second
2 changes: 2 additions & 0 deletions templates/devtest/gitea-ui.tmpl
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@
<div>2m future: {{TimeSince .TimeFuture2m $.locale}}</div>
<div>1y past: {{TimeSince .TimePast1y $.locale}}</div>
<div>1y future: {{TimeSince .TimeFuture1y $.locale}}</div>
<div>1y past (tense=auto): {{TimeSince .TimePast1y $.locale (dict "tense" "auto")}}</div>
<div>1y future (tense=auto): {{TimeSince .TimeFuture1y $.locale (dict "tense" "auto")}}</div>
</div>

<div>