Skip to content

Commit b4e9525

Browse files
Remove untranslatable on_date key (#24106)
- Follows #23988 - Fixes: #24074 by removing this key GitHub's `relative-time` elements allow us to force their rendering to `auto`, `past`, or `future` tense. We will never show an absolute date `on ...` in `TimeSince` ## Before ![image](https://user-images.githubusercontent.com/20454870/231735872-048c7bf3-6aa1-4113-929d-75a985c9922c.png) ## After ![image](https://user-images.githubusercontent.com/20454870/231736116-6ad47b63-77f4-4d3f-82a2-ee9a46ba2bd1.png) --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 35e562d commit b4e9525

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

modules/timeutil/since.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,25 @@ func timeSincePro(then, now time.Time, lang translation.Locale) string {
114114
return strings.TrimPrefix(timeStr, ", ")
115115
}
116116

117+
func timeSinceUnix(then, now time.Time, lang translation.Locale) template.HTML {
118+
friendlyText := then.Format("2006-01-02 15:04:05 +07:00")
119+
120+
// document: https://github.com/github/relative-time-element
121+
attrs := `tense="past"`
122+
isFuture := now.Before(then)
123+
if isFuture {
124+
attrs = `tense="future"`
125+
}
126+
127+
// declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip
128+
htm := fmt.Sprintf(`<relative-time class="time-since" prefix="" %s datetime="%s" data-tooltip-content data-tooltip-interactive="true">%s</relative-time>`,
129+
attrs, then.Format(time.RFC3339), friendlyText)
130+
return template.HTML(htm)
131+
}
132+
117133
// TimeSince renders relative time HTML given a time.Time
118134
func TimeSince(then time.Time, lang translation.Locale) template.HTML {
119-
timestamp := then.UTC().Format(time.RFC3339)
120-
// declare data-tooltip-content attribute to switch from "title" tooltip to "tippy" tooltip
121-
return template.HTML(fmt.Sprintf(`<relative-time class="time-since" prefix="%s" datetime="%s" data-tooltip-content data-tooltip-interactive="true">%s</relative-time>`, lang.Tr("on_date"), timestamp, timestamp))
135+
return timeSinceUnix(then, time.Now(), lang)
122136
}
123137

124138
// TimeSinceUnix renders relative time HTML given a TimeStamp

options/locale/locale_en-US.ini

-4
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ never = Never
112112

113113
rss_feed = RSS Feed
114114

115-
on_date = on
116-
117115
[aria]
118116
navbar = Navigation Bar
119117
footer = Footer
@@ -3129,8 +3127,6 @@ starred_repo = starred <a href="%[1]s">%[2]s</a>
31293127
watched_repo = started watching <a href="%[1]s">%[2]s</a>
31303128
31313129
[tool]
3132-
ago = %s ago
3133-
from_now = %s from now
31343130
now = now
31353131
future = future
31363132
1s = 1 second

routers/web/devtest/devtest.go

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"path"
99
"strings"
10+
"time"
1011

1112
"code.gitea.io/gitea/modules/base"
1213
"code.gitea.io/gitea/modules/context"
@@ -32,5 +33,14 @@ func List(ctx *context.Context) {
3233
}
3334

3435
func Tmpl(ctx *context.Context) {
36+
now := time.Now()
37+
ctx.Data["TimeNow"] = now
38+
ctx.Data["TimePast5s"] = now.Add(-5 * time.Second)
39+
ctx.Data["TimeFuture5s"] = now.Add(5 * time.Second)
40+
ctx.Data["TimePast2m"] = now.Add(-2 * time.Minute)
41+
ctx.Data["TimeFuture2m"] = now.Add(2 * time.Minute)
42+
ctx.Data["TimePast1y"] = now.Add(-1 * 366 * 86400 * time.Second)
43+
ctx.Data["TimeFuture1y"] = now.Add(1 * 366 * 86400 * time.Second)
44+
3545
ctx.HTML(http.StatusOK, base.TplName("devtest"+path.Clean("/"+ctx.Params("sub"))))
3646
}

templates/devtest/gitea-ui.tmpl

+44-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
{{template "base/head" .}}
2-
<div class="page-content devtest">
2+
<div class="page-content devtest ui container">
3+
34
<div>
4-
<gitea-origin-url data-url="test/url"></gitea-origin-url>
5-
<gitea-origin-url data-url="/test/url"></gitea-origin-url>
5+
<h1>Tooltip</h1>
6+
<div><span data-tooltip-content="test tooltip">text with tooltip</span></div>
7+
<div><span data-tooltip-content="test tooltip" data-tooltip-interactive="true">text with interactive tooltip</span></div>
68
</div>
9+
710
<div>
8-
<span data-tooltip-content="test tooltip">text with tooltip</span>
11+
<h1>GiteaOriginUrl</h1>
12+
<div><gitea-origin-url data-url="test/url"></gitea-origin-url></div>
13+
<div><gitea-origin-url data-url="/test/url"></gitea-origin-url></div>
914
</div>
10-
{{template "shared/combomarkdowneditor" .}}
15+
16+
<div>
17+
<h1>LocaleNumber</h1>
18+
<div>{{LocaleNumber 1}}</div>
19+
<div>{{LocaleNumber 12}}</div>
20+
<div>{{LocaleNumber 123}}</div>
21+
<div>{{LocaleNumber 1234}}</div>
22+
<div>{{LocaleNumber 12345}}</div>
23+
<div>{{LocaleNumber 123456}}</div>
24+
<div>{{LocaleNumber 1234567}}</div>
25+
</div>
26+
27+
<div>
28+
<h1>TimeSince</h1>
29+
<div>Now: {{TimeSince .TimeNow $.locale}}</div>
30+
<div>5s past: {{TimeSince .TimePast5s $.locale}}</div>
31+
<div>5s future: {{TimeSince .TimeFuture5s $.locale}}</div>
32+
<div>2m past: {{TimeSince .TimePast2m $.locale}}</div>
33+
<div>2m future: {{TimeSince .TimeFuture2m $.locale}}</div>
34+
<div>1y past: {{TimeSince .TimePast1y $.locale}}</div>
35+
<div>1y future: {{TimeSince .TimeFuture1y $.locale}}</div>
36+
</div>
37+
38+
<div>
39+
<h1>ComboMarkdownEditor</h1>
40+
<div>ps: no JS code attached, so just a layout</div>
41+
{{template "shared/combomarkdowneditor" .}}
42+
</div>
43+
44+
<style>
45+
h1 {
46+
margin: 0;
47+
padding: 10px 0;
48+
}
49+
</style>
1150
</div>
1251
{{template "base/footer" .}}

templates/package/view.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@
8484
<a class="ui right" href="{{$.PackageDescriptor.PackageWebLink}}/versions">{{.locale.Tr "packages.versions.view_all"}}</a>
8585
<div class="ui relaxed list">
8686
{{range .LatestVersions}}
87-
<div class="item">
88-
<a href="{{$.PackageDescriptor.PackageWebLink}}/{{PathEscape .LowerVersion}}">{{.Version}}</a>
89-
<span class="text small">{{$.locale.Tr "on_date"}} {{.CreatedUnix.FormatDate}}</span>
87+
<div class="item gt-df">
88+
<a class="gt-f1" href="{{$.PackageDescriptor.PackageWebLink}}/{{PathEscape .LowerVersion}}">{{.Version}}</a>
89+
<span class="text small">{{template "shared/datetime/short" (dict "Datetime" (.CreatedUnix.FormatDate) "Fallback" (.CreatedUnix.FormatDate))}}</span>
9090
</div>
9191
{{end}}
9292
</div>

0 commit comments

Comments
 (0)