Skip to content

Commit 6a6dc97

Browse files
Respect user's locale when rendering the date range in the repo activity page (#21410)
# Description Previously, to make the date range understood by all, we used the format "2006-01-02" for the dates as it's locale-generic. This commit changes the rendering logic. Instead of rendering the date on the server, we send a formatted computer-readable timestamp. The client's javascript then renders it according to the user's locale. This approach is reusable across the codebase, any `<time></time>` tag with the data-format="date" attribute would get rendered according to the user's chosen locale. ## Previous View ![image](https://user-images.githubusercontent.com/20454870/195099143-e1c5df86-282a-42f1-898f-a36bb5fe7c2f.png) ## New View ### English ![image](https://user-images.githubusercontent.com/20454870/195099301-5cda4eab-4012-49d5-97e5-b1f9cada9c06.png) ### French ![image](https://user-images.githubusercontent.com/20454870/195099434-ce23e394-8d65-4c4c-8ac8-8b96bc9044f3.png) ### Portuguese ![image](https://user-images.githubusercontent.com/20454870/195099559-9a7aed28-944a-45ec-bedb-64403e3faede.png) ### Italian ![image](https://user-images.githubusercontent.com/20454870/195099661-17758d55-3fe0-4797-879b-d45de0ee8ba3.png) # References * #21380 * #21387 * #21396 Inspiration: I think either differentiating by class, or probably better by a custom attribute such as `data-format` or similar, is the best course of action. _Originally posted by @delvh in #21396 (comment) Resolves #21380 Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: silverwind <[email protected]>
1 parent ac3a61e commit 6a6dc97

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

routers/web/repo/activity.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func Activity(ctx *context.Context) {
4747
ctx.Data["Period"] = "weekly"
4848
timeFrom = timeUntil.Add(-time.Hour * 168)
4949
}
50-
ctx.Data["DateFrom"] = timeFrom.Format("2006-01-02")
51-
ctx.Data["DateUntil"] = timeUntil.Format("2006-01-02")
50+
ctx.Data["DateFrom"] = timeFrom.UTC().Format(time.RFC3339)
51+
ctx.Data["DateUntil"] = timeUntil.UTC().Format(time.RFC3339)
5252
ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))
5353

5454
var err error

templates/repo/activity.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="page-content repository commits">
33
{{template "repo/header" .}}
44
<div class="ui container">
5-
<h2 class="ui header">{{.DateFrom}} - {{.DateUntil}}
5+
<h2 class="ui header"><time data-format="date" datetime="{{.DateFrom}}">{{.DateFrom}}</time> - <time data-format="date" datetime="{{.DateUntil}}">{{.DateUntil}}</time>
66
<div class="ui right">
77
<!-- Period -->
88
<div class="ui floating dropdown jump filter">

web_src/js/features/formatting.js

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

33
const {lang} = document.documentElement;
4+
const dateFormatter = new Intl.DateTimeFormat(lang, {year: 'numeric', month: 'long', day: 'numeric'});
45

56
export function initFormattingReplacements() {
67
// replace english formatted numbers with locale-specific separators
@@ -11,4 +12,10 @@ export function initFormattingReplacements() {
1112
el.textContent = formatted;
1213
}
1314
}
15+
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));
20+
}
1421
}

0 commit comments

Comments
 (0)