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

Respect user's locale when rendering the date range in the repo activity page #21410

Merged
merged 7 commits into from
Oct 12, 2022
4 changes: 2 additions & 2 deletions routers/web/repo/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func Activity(ctx *context.Context) {
ctx.Data["Period"] = "weekly"
timeFrom = timeUntil.Add(-time.Hour * 168)
}
ctx.Data["DateFrom"] = timeFrom.Format("2006-01-02")
ctx.Data["DateUntil"] = timeUntil.Format("2006-01-02")
ctx.Data["DateFrom"] = timeFrom.UTC().Format(time.RFC3339)
ctx.Data["DateUntil"] = timeUntil.UTC().Format(time.RFC3339)
ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))

var err error
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/activity.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="page-content repository commits">
{{template "repo/header" .}}
<div class="ui container">
<h2 class="ui header">{{.DateFrom}} - {{.DateUntil}}
<h2 class="ui header"><time data-format="date" datetime="{{.DateFrom}}">{{.DateFrom}}</time> - <time data-format="date" datetime="{{.DateUntil}}">{{.DateUntil}}</time>
<div class="ui right">
<!-- Period -->
<div class="ui floating dropdown jump filter">
Expand Down
7 changes: 7 additions & 0 deletions web_src/js/features/formatting.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {prettyNumber} from '../utils.js';

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

export function initFormattingReplacements() {
// replace english formatted numbers with locale-specific separators
Expand All @@ -11,4 +12,10 @@ export function initFormattingReplacements() {
el.textContent = formatted;
}
}

// for each <time></time> tag, if it has the data-format="date" attribute, format
// the text according to the user's chosen locale
for (const timeElement of document.querySelectorAll('time[data-format="date"]')) {
timeElement.textContent = dateFormatter.format(new Date(timeElement.dateTime));
}
}