Skip to content

Commit acdb92a

Browse files
yardenshohamGustedtechknowlogicklunny
authored
Localize all timestamps (#21440)
Following * #21410 We are now able to localize all timestamps. Some examples: `short-date` format, French, user profile page: ![image](https://user-images.githubusercontent.com/20454870/195622461-aa0d5b93-f8df-42ad-881c-9c16606bf387.png) `date-time` format, Portuguese, mirror repository settings page: ![image](https://user-images.githubusercontent.com/20454870/195623191-7a37d77c-4a02-4140-846d-f290a65ea21d.png) Signed-off-by: Yarden Shoham <[email protected]> Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: Gusted <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 683c9af commit acdb92a

20 files changed

+49
-27
lines changed

templates/admin/auth/list.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
<td><a href="{{AppSubUrl}}/admin/auths/{{.ID}}">{{.Name}}</a></td>
3030
<td>{{.TypeName}}</td>
3131
<td>{{if .IsActive}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</td>
32-
<td><span class="tooltip" data-content="{{.UpdatedUnix.FormatShort}}">{{.UpdatedUnix.FormatShort}}</span></td>
33-
<td><span class="tooltip" data-content="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</span></td>
32+
<td><span class="tooltip" data-content="{{.UpdatedUnix.FormatShort}}"><time data-format="short-date" datetime="{{.UpdatedUnix.FormatLong}}">{{.UpdatedUnix.FormatShort}}</time></span></td>
33+
<td><span class="tooltip" data-content="{{.CreatedUnix.FormatLong}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
3434
<td><a href="{{AppSubUrl}}/admin/auths/{{.ID}}">{{svg "octicon-pencil"}}</a></td>
3535
</tr>
3636
{{end}}

templates/admin/notice.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<td>{{.ID}}</td>
3030
<td>{{$.locale.Tr .TrStr}}</td>
3131
<td class="view-detail"><span class="notice-description text truncate">{{.Description}}</span></td>
32-
<td><span class="notice-created-time tooltip" data-content="{{.CreatedUnix.AsTime}}">{{.CreatedUnix.FormatShort}}</span></td>
32+
<td><span class="notice-created-time tooltip" data-content="{{.CreatedUnix.AsTime}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
3333
<td><a href="#">{{svg "octicon-note" 16 "view-detail"}}</a></td>
3434
</tr>
3535
{{end}}

templates/admin/org/list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<td>{{.NumTeams}}</td>
4545
<td>{{.NumMembers}}</td>
4646
<td>{{.NumRepos}}</td>
47-
<td><span title="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</span></td>
47+
<td><span title="{{.CreatedUnix.FormatLong}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
4848
<td><a href="{{.OrganisationLink}}/settings">{{svg "octicon-pencil"}}</a></td>
4949
</tr>
5050
{{end}}

templates/admin/packages/list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
{{end}}
7676
</td>
7777
<td>{{FileSize .CalculateBlobSize}}</td>
78-
<td><span title="{{.Version.CreatedUnix.FormatLong}}">{{.Version.CreatedUnix.FormatShort}}</span></td>
78+
<td><span title="{{.Version.CreatedUnix.FormatLong}}"><time data-format="short-date" datetime="{{.Version.CreatedUnix.FormatLong}}">{{.Version.CreatedUnix.FormatShort}}</time></span></td>
7979
<td><a class="delete-button" href="" data-url="{{$.Link}}/delete?page={{$.Page.Paginater.Current}}&sort={{$.SortType}}" data-id="{{.Version.ID}}" data-name="{{.Package.Name}}" data-data-version="{{.Version.Version}}">{{svg "octicon-trash"}}</a></td>
8080
</tr>
8181
{{end}}

templates/admin/repo/list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<td>{{.NumForks}}</td>
8484
<td>{{.NumIssues}}</td>
8585
<td>{{FileSize .Size}}</td>
86-
<td><span title="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</span></td>
86+
<td><span title="{{.CreatedUnix.FormatLong}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
8787
<td><a class="delete-button" href="" data-url="{{$.Link}}/delete?page={{$.Page.Paginater.Current}}&sort={{$.SortType}}" data-id="{{.ID}}" data-name="{{.Name}}">{{svg "octicon-trash"}}</a></td>
8888
</tr>
8989
{{end}}

templates/admin/user/list.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@
9494
<td>{{if .IsRestricted}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</td>
9595
<td>{{if index $.UsersTwoFaStatus .ID}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}}</td>
9696
<td>{{.NumRepos}}</td>
97-
<td><span title="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</span></td>
97+
<td><span title="{{.CreatedUnix.FormatLong}}"><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span></td>
9898
{{if .LastLoginUnix}}
99-
<td><span title="{{.LastLoginUnix.FormatLong}}">{{.LastLoginUnix.FormatShort}}</span></td>
99+
<td><span title="{{.LastLoginUnix.FormatLong}}"><time data-format="short-date" datetime="{{.LastLoginUnix.FormatLong}}">{{.LastLoginUnix.FormatShort}}</time></span></td>
100100
{{else}}
101101
<td><span>{{$.locale.Tr "admin.users.never_login"}}</span></td>
102102
{{end}}

templates/explore/organizations.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{{svg "octicon-link"}}
2424
<a href="{{.Website}}" rel="nofollow">{{.Website}}</a>
2525
{{end}}
26-
{{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}}
26+
{{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} <time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time>
2727
</div>
2828
</div>
2929
</div>

templates/explore/users.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{{svg "octicon-mail"}}
1919
<a href="mailto:{{.Email}}" rel="nofollow">{{.Email}}</a>
2020
{{end}}
21-
{{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}}
21+
{{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} <time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time>
2222
</div>
2323
</div>
2424
</div>

templates/repo/issue/view_content/sidebar.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@
413413
<div class="df sb ac">
414414
<div class="due-date tooltip {{if .Issue.IsOverdue}}text red{{end}}" {{if .Issue.IsOverdue}}data-content="{{.locale.Tr "repo.issues.due_date_overdue"}}"{{end}}>
415415
{{svg "octicon-calendar" 16 "mr-3"}}
416-
{{.Issue.DeadlineUnix.FormatDate}}
416+
<time data-format="date" datetime="{{.Issue.DeadlineUnix.FormatLong}}">{{.Issue.DeadlineUnix.FormatDate}}</time>
417417
</div>
418418
<div>
419419
{{if and .HasIssuesOrPullsWritePermission (not .Repository.IsArchived)}}

templates/repo/settings/deploy_keys.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
{{.Fingerprint}}
6565
</div>
6666
<div class="activity meta">
67-
<i>{{$.locale.Tr "settings.add_on"}} <span>{{.CreatedUnix.FormatShort}}</span> — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}>{{.UpdatedUnix.FormatShort}}</span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}} - <span>{{$.locale.Tr "settings.can_read_info"}}{{if not .IsReadOnly}} / {{$.locale.Tr "settings.can_write_info"}} {{end}}</span></i>
67+
<i>{{$.locale.Tr "settings.add_on"}} <span><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span> — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}><time data-format="short-date" datetime="{{.UpdatedUnix.FormatLong}}">{{.UpdatedUnix.FormatShort}}</time></span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}} - <span>{{$.locale.Tr "settings.can_read_info"}}{{if not .IsReadOnly}} / {{$.locale.Tr "settings.can_write_info"}} {{end}}</span></i>
6868
</div>
6969
</div>
7070
</div>

templates/repo/settings/options.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<tr>
9494
<td>{{(MirrorRemoteAddress $.Context .Repository .Mirror.GetRemoteName false).Address}}</td>
9595
<td>{{$.locale.Tr "repo.settings.mirror_settings.direction.pull"}}</td>
96-
<td>{{.Mirror.UpdatedUnix.AsTime}}</td>
96+
<td><time data-format="date-time" datetime="{{.Mirror.UpdatedUnix.FormatLong}}">{{.Mirror.UpdatedUnix.AsTime}}</time></td>
9797
<td class="right aligned">
9898
<form method="post" style="display: inline-block">
9999
{{.CsrfTokenHtml}}
@@ -171,7 +171,7 @@
171171
{{$address := MirrorRemoteAddress $.Context $.Repository .GetRemoteName true}}
172172
<td>{{$address.Address}}</td>
173173
<td>{{$.locale.Tr "repo.settings.mirror_settings.direction.push"}}</td>
174-
<td>{{if .LastUpdateUnix}}{{.LastUpdateUnix.AsTime}}{{else}}{{$.locale.Tr "never"}}{{end}} {{if .LastError}}<div class="ui red label tooltip" data-content="{{.LastError}}">{{$.locale.Tr "error"}}</div>{{end}}</td>
174+
<td>{{if .LastUpdateUnix}}<time data-format="date-time" datetime="{{.LastUpdateUnix.FormatLong}}">{{.LastUpdateUnix.AsTime}}</time>{{else}}{{$.locale.Tr "never"}}{{end}} {{if .LastError}}<div class="ui red label tooltip" data-content="{{.LastError}}">{{$.locale.Tr "error"}}</div>{{end}}</td>
175175
<td class="right aligned">
176176
<form method="post" style="display: inline-block">
177177
{{$.CsrfTokenHtml}}

templates/repo/user_cards.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{{else if .Location}}
1919
{{svg "octicon-location"}} {{.Location}}
2020
{{else}}
21-
{{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}}
21+
{{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} <time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time>
2222
{{end}}
2323
</div>
2424
</li>

templates/shared/issuelist.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<span class="due-date tooltip" data-content="{{$.locale.Tr "repo.issues.due_date"}}" data-position="right center">
110110
<span{{if .IsOverdue}} class="overdue"{{end}}>
111111
{{svg "octicon-calendar" 14 "mr-2"}}
112-
{{.DeadlineUnix.FormatShort}}
112+
<time data-format="short-date" datetime="{{.DeadlineUnix.FormatLong}}">{{.DeadlineUnix.FormatShort}}</time>
113113
</span>
114114
</span>
115115
{{end}}

templates/user/profile.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
</li>
5454
{{end}}
5555
{{end}}
56-
<li>{{svg "octicon-clock"}} {{.locale.Tr "user.join_on"}} {{.Owner.CreatedUnix.FormatShort}}</li>
56+
<li>{{svg "octicon-clock"}} {{.locale.Tr "user.join_on"}} <time data-format="short-date" datetime="{{.Owner.CreatedUnix.FormatLong}}">{{.Owner.CreatedUnix.FormatShort}}</time></li>
5757
{{if and .Orgs .HasOrgsVisible}}
5858
<li>
5959
<ul class="user-orgs">

templates/user/settings/applications.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<div class="content">
2424
<strong>{{.Name}}</strong>
2525
<div class="activity meta">
26-
<i>{{$.locale.Tr "settings.add_on"}} <span>{{.CreatedUnix.FormatShort}}</span> — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}>{{.UpdatedUnix.FormatShort}}</span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}</i>
26+
<i>{{$.locale.Tr "settings.add_on"}} <span><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span> — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}><time data-format="short-date" datetime="{{.UpdatedUnix.FormatLong}}">{{.UpdatedUnix.FormatShort}}</time></span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}</i>
2727
</div>
2828
</div>
2929
</div>

templates/user/settings/grants_oauth2.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<div class="content">
2121
<strong>{{$grant.Application.Name}}</strong>
2222
<div class="activity meta">
23-
<i>{{$.locale.Tr "settings.add_on"}} <span>{{$grant.CreatedUnix.FormatShort}}</span></i>
23+
<i>{{$.locale.Tr "settings.add_on"}} <span><time data-format="short-date" datetime="{{$grant.CreatedUnix.FormatLong}}">{{$grant.CreatedUnix.FormatShort}}</time></span></i>
2424
</div>
2525
</div>
2626
</div>

templates/user/settings/keys_gpg.tmpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
<b>{{$.locale.Tr "settings.subkeys"}}:</b> {{range .SubsKey}} {{.PaddedKeyID}} {{end}}
6969
</div>
7070
<div class="activity meta">
71-
<i>{{$.locale.Tr "settings.add_on"}} <span>{{.AddedUnix.FormatShort}}</span></i>
71+
<i>{{$.locale.Tr "settings.add_on"}} <span><time data-format="short-date" datetime="{{.AddedUnix.FormatLong}}">{{.AddedUnix.FormatShort}}</time></span></i>
7272
-
73-
<i>{{if not .ExpiredUnix.IsZero}}{{$.locale.Tr "settings.valid_until"}} <span>{{.ExpiredUnix.FormatShort}}</span>{{else}}{{$.locale.Tr "settings.valid_forever"}}{{end}}</i>
73+
<i>{{if not .ExpiredUnix.IsZero}}{{$.locale.Tr "settings.valid_until"}} <span><time data-format="short-date" datetime="{{.ExpiredUnix.FormatLong}}">{{.ExpiredUnix.FormatShort}}</time></span>{{else}}{{$.locale.Tr "settings.valid_forever"}}{{end}}</i>
7474
</div>
7575
</div>
7676
</div>

templates/user/settings/keys_principal.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<div class="content">
2626
<strong>{{.Name}}</strong>
2727
<div class="activity meta">
28-
<i>{{$.locale.Tr "settings.add_on"}} <span>{{.CreatedUnix.FormatShort}}</span> — {{svg "octicon-info" 16}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}>{{.UpdatedUnix.FormatShort}}</span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}</i>
28+
<i>{{$.locale.Tr "settings.add_on"}} <span><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span> — {{svg "octicon-info" 16}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}><time data-format="short-date" datetime="{{.UpdatedUnix.FormatLong}}">{{.UpdatedUnix.FormatShort}}</time></span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}</i>
2929
</div>
3030
</div>
3131
</div>

templates/user/settings/keys_ssh.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
{{.Fingerprint}}
5959
</div>
6060
<div class="activity meta">
61-
<i>{{$.locale.Tr "settings.add_on"}} <span>{{.CreatedUnix.FormatShort}}</span> — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}>{{.UpdatedUnix.FormatShort}}</span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}</i>
61+
<i>{{$.locale.Tr "settings.add_on"}} <span><time data-format="short-date" datetime="{{.CreatedUnix.FormatLong}}">{{.CreatedUnix.FormatShort}}</time></span> — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} <span {{if .HasRecentActivity}}class="green"{{end}}><time data-format="short-date" datetime="{{.UpdatedUnix.FormatLong}}">{{.UpdatedUnix.FormatShort}}</time></span>{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}</i>
6262
</div>
6363
</div>
6464
</div>

web_src/js/features/formatting.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {prettyNumber} from '../utils.js';
22

33
const {lang} = document.documentElement;
4+
45
const dateFormatter = new Intl.DateTimeFormat(lang, {year: 'numeric', month: 'long', day: 'numeric'});
6+
const shortDateFormatter = new Intl.DateTimeFormat(lang, {year: 'numeric', month: 'short', day: 'numeric'});
7+
const dateTimeFormatter = new Intl.DateTimeFormat(lang, {year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric'});
58

69
export function initFormattingReplacements() {
710
// replace english formatted numbers with locale-specific separators
@@ -13,9 +16,28 @@ export function initFormattingReplacements() {
1316
}
1417
}
1518

16-
// for each <time></time> tag, if it has the data-format="date" attribute, format
17-
// the text according to the user's chosen locale
18-
for (const timeElement of document.querySelectorAll('time[data-format="date"]')) {
19-
timeElement.textContent = dateFormatter.format(new Date(timeElement.dateTime));
19+
// for each <time></time> tag, if it has the data-format attribute, format
20+
// the text according to the user's chosen locale and formatter.
21+
formatAllTimeElements();
22+
}
23+
24+
function formatAllTimeElements() {
25+
const timeElements = document.querySelectorAll('time[data-format]');
26+
for (const timeElement of timeElements) {
27+
const formatter = getFormatter(timeElement.dataset.format);
28+
timeElement.textContent = formatter.format(new Date(timeElement.dateTime));
29+
}
30+
}
31+
32+
function getFormatter(format) {
33+
switch (format) {
34+
case 'date':
35+
return dateFormatter;
36+
case 'short-date':
37+
return shortDateFormatter;
38+
case 'date-time':
39+
return dateTimeFormatter;
40+
default:
41+
throw new Error('Unknown format');
2042
}
2143
}

0 commit comments

Comments
 (0)