Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Apr 14, 2023
1 parent 5bf5d41 commit d8e73a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
22 changes: 17 additions & 5 deletions modules/timeutil/since.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions templates/devtest/gitea-ui.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit d8e73a5

Please sign in to comment.