From acdb92ad425b1f7035b677f430516ab4a9f645c2 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Mon, 17 Oct 2022 07:08:21 +0300 Subject: [PATCH] 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 Signed-off-by: Yarden Shoham Co-authored-by: Gusted Co-authored-by: techknowlogick Co-authored-by: Lunny Xiao --- templates/admin/auth/list.tmpl | 4 +-- templates/admin/notice.tmpl | 2 +- templates/admin/org/list.tmpl | 2 +- templates/admin/packages/list.tmpl | 2 +- templates/admin/repo/list.tmpl | 2 +- templates/admin/user/list.tmpl | 4 +-- templates/explore/organizations.tmpl | 2 +- templates/explore/users.tmpl | 2 +- .../repo/issue/view_content/sidebar.tmpl | 2 +- templates/repo/settings/deploy_keys.tmpl | 2 +- templates/repo/settings/options.tmpl | 4 +-- templates/repo/user_cards.tmpl | 2 +- templates/shared/issuelist.tmpl | 2 +- templates/user/profile.tmpl | 2 +- templates/user/settings/applications.tmpl | 2 +- templates/user/settings/grants_oauth2.tmpl | 2 +- templates/user/settings/keys_gpg.tmpl | 4 +-- templates/user/settings/keys_principal.tmpl | 2 +- templates/user/settings/keys_ssh.tmpl | 2 +- web_src/js/features/formatting.js | 30 ++++++++++++++++--- 20 files changed, 49 insertions(+), 27 deletions(-) diff --git a/templates/admin/auth/list.tmpl b/templates/admin/auth/list.tmpl index 3ce138449..c43287ee1 100644 --- a/templates/admin/auth/list.tmpl +++ b/templates/admin/auth/list.tmpl @@ -29,8 +29,8 @@ {{.Name}} {{.TypeName}} {{if .IsActive}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} - {{.UpdatedUnix.FormatShort}} - {{.CreatedUnix.FormatShort}} + + {{svg "octicon-pencil"}} {{end}} diff --git a/templates/admin/notice.tmpl b/templates/admin/notice.tmpl index b831f50c8..2777741ef 100644 --- a/templates/admin/notice.tmpl +++ b/templates/admin/notice.tmpl @@ -29,7 +29,7 @@ {{.ID}} {{$.locale.Tr .TrStr}} {{.Description}} - {{.CreatedUnix.FormatShort}} + {{svg "octicon-note" 16 "view-detail"}} {{end}} diff --git a/templates/admin/org/list.tmpl b/templates/admin/org/list.tmpl index aec3f2c84..11dc23c60 100644 --- a/templates/admin/org/list.tmpl +++ b/templates/admin/org/list.tmpl @@ -44,7 +44,7 @@ {{.NumTeams}} {{.NumMembers}} {{.NumRepos}} - {{.CreatedUnix.FormatShort}} + {{svg "octicon-pencil"}} {{end}} diff --git a/templates/admin/packages/list.tmpl b/templates/admin/packages/list.tmpl index 06d616347..b62e78879 100644 --- a/templates/admin/packages/list.tmpl +++ b/templates/admin/packages/list.tmpl @@ -75,7 +75,7 @@ {{end}} {{FileSize .CalculateBlobSize}} - {{.Version.CreatedUnix.FormatShort}} + {{svg "octicon-trash"}} {{end}} diff --git a/templates/admin/repo/list.tmpl b/templates/admin/repo/list.tmpl index b26ec2eb7..837802f0d 100644 --- a/templates/admin/repo/list.tmpl +++ b/templates/admin/repo/list.tmpl @@ -83,7 +83,7 @@ {{.NumForks}} {{.NumIssues}} {{FileSize .Size}} - {{.CreatedUnix.FormatShort}} + {{svg "octicon-trash"}} {{end}} diff --git a/templates/admin/user/list.tmpl b/templates/admin/user/list.tmpl index 061e66385..56f6eaa3a 100644 --- a/templates/admin/user/list.tmpl +++ b/templates/admin/user/list.tmpl @@ -94,9 +94,9 @@ {{if .IsRestricted}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} {{if index $.UsersTwoFaStatus .ID}}{{svg "octicon-check"}}{{else}}{{svg "octicon-x"}}{{end}} {{.NumRepos}} - {{.CreatedUnix.FormatShort}} + {{if .LastLoginUnix}} - {{.LastLoginUnix.FormatShort}} + {{else}} {{$.locale.Tr "admin.users.never_login"}} {{end}} diff --git a/templates/explore/organizations.tmpl b/templates/explore/organizations.tmpl index 2073b8c69..36267baf3 100644 --- a/templates/explore/organizations.tmpl +++ b/templates/explore/organizations.tmpl @@ -23,7 +23,7 @@ {{svg "octicon-link"}} {{.Website}} {{end}} - {{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}} + {{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} diff --git a/templates/explore/users.tmpl b/templates/explore/users.tmpl index 94a21d995..5fb79c0dd 100644 --- a/templates/explore/users.tmpl +++ b/templates/explore/users.tmpl @@ -18,7 +18,7 @@ {{svg "octicon-mail"}} {{.Email}} {{end}} - {{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}} + {{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} diff --git a/templates/repo/issue/view_content/sidebar.tmpl b/templates/repo/issue/view_content/sidebar.tmpl index 5b95d3d8e..eb95674b1 100644 --- a/templates/repo/issue/view_content/sidebar.tmpl +++ b/templates/repo/issue/view_content/sidebar.tmpl @@ -413,7 +413,7 @@
{{svg "octicon-calendar" 16 "mr-3"}} - {{.Issue.DeadlineUnix.FormatDate}} +
{{if and .HasIssuesOrPullsWritePermission (not .Repository.IsArchived)}} diff --git a/templates/repo/settings/deploy_keys.tmpl b/templates/repo/settings/deploy_keys.tmpl index 2f8a4c6c1..44c916eef 100644 --- a/templates/repo/settings/deploy_keys.tmpl +++ b/templates/repo/settings/deploy_keys.tmpl @@ -64,7 +64,7 @@ {{.Fingerprint}}
- {{$.locale.Tr "settings.add_on"}} {{.CreatedUnix.FormatShort}} — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{.UpdatedUnix.FormatShort}}{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}} - {{$.locale.Tr "settings.can_read_info"}}{{if not .IsReadOnly}} / {{$.locale.Tr "settings.can_write_info"}} {{end}} + {{$.locale.Tr "settings.add_on"}} — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{else}}{{$.locale.Tr "settings.no_activity"}}{{end}} - {{$.locale.Tr "settings.can_read_info"}}{{if not .IsReadOnly}} / {{$.locale.Tr "settings.can_write_info"}} {{end}}
diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 9030235e7..3c5996e90 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -93,7 +93,7 @@ {{(MirrorRemoteAddress $.Context .Repository .Mirror.GetRemoteName false).Address}} {{$.locale.Tr "repo.settings.mirror_settings.direction.pull"}} - {{.Mirror.UpdatedUnix.AsTime}} +
{{.CsrfTokenHtml}} @@ -171,7 +171,7 @@ {{$address := MirrorRemoteAddress $.Context $.Repository .GetRemoteName true}} {{$address.Address}} {{$.locale.Tr "repo.settings.mirror_settings.direction.push"}} - {{if .LastUpdateUnix}}{{.LastUpdateUnix.AsTime}}{{else}}{{$.locale.Tr "never"}}{{end}} {{if .LastError}}
{{$.locale.Tr "error"}}
{{end}} + {{if .LastUpdateUnix}}{{else}}{{$.locale.Tr "never"}}{{end}} {{if .LastError}}
{{$.locale.Tr "error"}}
{{end}} {{$.CsrfTokenHtml}} diff --git a/templates/repo/user_cards.tmpl b/templates/repo/user_cards.tmpl index eec31d8b5..efc853082 100644 --- a/templates/repo/user_cards.tmpl +++ b/templates/repo/user_cards.tmpl @@ -18,7 +18,7 @@ {{else if .Location}} {{svg "octicon-location"}} {{.Location}} {{else}} - {{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} {{.CreatedUnix.FormatShort}} + {{svg "octicon-clock"}} {{$.locale.Tr "user.join_on"}} {{end}} diff --git a/templates/shared/issuelist.tmpl b/templates/shared/issuelist.tmpl index 066d34fe3..6fce412ff 100644 --- a/templates/shared/issuelist.tmpl +++ b/templates/shared/issuelist.tmpl @@ -109,7 +109,7 @@ {{svg "octicon-calendar" 14 "mr-2"}} - {{.DeadlineUnix.FormatShort}} + {{end}} diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl index a4ac65db6..3b776bc16 100644 --- a/templates/user/profile.tmpl +++ b/templates/user/profile.tmpl @@ -53,7 +53,7 @@ {{end}} {{end}} -
  • {{svg "octicon-clock"}} {{.locale.Tr "user.join_on"}} {{.Owner.CreatedUnix.FormatShort}}
  • +
  • {{svg "octicon-clock"}} {{.locale.Tr "user.join_on"}}
  • {{if and .Orgs .HasOrgsVisible}}
    • diff --git a/templates/user/settings/applications.tmpl b/templates/user/settings/applications.tmpl index 9125f4bd0..e05cebb0e 100644 --- a/templates/user/settings/applications.tmpl +++ b/templates/user/settings/applications.tmpl @@ -23,7 +23,7 @@
      {{.Name}}
      - {{$.locale.Tr "settings.add_on"}} {{.CreatedUnix.FormatShort}} — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{.UpdatedUnix.FormatShort}}{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}} + {{$.locale.Tr "settings.add_on"}} — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}
      diff --git a/templates/user/settings/grants_oauth2.tmpl b/templates/user/settings/grants_oauth2.tmpl index e67fd2d22..878c258dd 100644 --- a/templates/user/settings/grants_oauth2.tmpl +++ b/templates/user/settings/grants_oauth2.tmpl @@ -20,7 +20,7 @@
      {{$grant.Application.Name}}
      - {{$.locale.Tr "settings.add_on"}} {{$grant.CreatedUnix.FormatShort}} + {{$.locale.Tr "settings.add_on"}}
      diff --git a/templates/user/settings/keys_gpg.tmpl b/templates/user/settings/keys_gpg.tmpl index 588350817..fe6c0bbeb 100644 --- a/templates/user/settings/keys_gpg.tmpl +++ b/templates/user/settings/keys_gpg.tmpl @@ -68,9 +68,9 @@ {{$.locale.Tr "settings.subkeys"}}: {{range .SubsKey}} {{.PaddedKeyID}} {{end}}
      - {{$.locale.Tr "settings.add_on"}} {{.AddedUnix.FormatShort}} + {{$.locale.Tr "settings.add_on"}} - - {{if not .ExpiredUnix.IsZero}}{{$.locale.Tr "settings.valid_until"}} {{.ExpiredUnix.FormatShort}}{{else}}{{$.locale.Tr "settings.valid_forever"}}{{end}} + {{if not .ExpiredUnix.IsZero}}{{$.locale.Tr "settings.valid_until"}} {{else}}{{$.locale.Tr "settings.valid_forever"}}{{end}}
      diff --git a/templates/user/settings/keys_principal.tmpl b/templates/user/settings/keys_principal.tmpl index 569921446..b0cacbe54 100644 --- a/templates/user/settings/keys_principal.tmpl +++ b/templates/user/settings/keys_principal.tmpl @@ -25,7 +25,7 @@
      {{.Name}}
      - {{$.locale.Tr "settings.add_on"}} {{.CreatedUnix.FormatShort}} — {{svg "octicon-info" 16}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{.UpdatedUnix.FormatShort}}{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}} + {{$.locale.Tr "settings.add_on"}} — {{svg "octicon-info" 16}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}
      diff --git a/templates/user/settings/keys_ssh.tmpl b/templates/user/settings/keys_ssh.tmpl index bcab968d5..093395390 100644 --- a/templates/user/settings/keys_ssh.tmpl +++ b/templates/user/settings/keys_ssh.tmpl @@ -58,7 +58,7 @@ {{.Fingerprint}}
      - {{$.locale.Tr "settings.add_on"}} {{.CreatedUnix.FormatShort}} — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{.UpdatedUnix.FormatShort}}{{else}}{{$.locale.Tr "settings.no_activity"}}{{end}} + {{$.locale.Tr "settings.add_on"}} — {{svg "octicon-info"}} {{if .HasUsed}}{{$.locale.Tr "settings.last_used"}} {{else}}{{$.locale.Tr "settings.no_activity"}}{{end}}
      diff --git a/web_src/js/features/formatting.js b/web_src/js/features/formatting.js index 5f4633bba..c8f5db9e1 100644 --- a/web_src/js/features/formatting.js +++ b/web_src/js/features/formatting.js @@ -1,7 +1,10 @@ import {prettyNumber} from '../utils.js'; const {lang} = document.documentElement; + const dateFormatter = new Intl.DateTimeFormat(lang, {year: 'numeric', month: 'long', day: 'numeric'}); +const shortDateFormatter = new Intl.DateTimeFormat(lang, {year: 'numeric', month: 'short', day: 'numeric'}); +const dateTimeFormatter = new Intl.DateTimeFormat(lang, {year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric'}); export function initFormattingReplacements() { // replace english formatted numbers with locale-specific separators @@ -13,9 +16,28 @@ export function initFormattingReplacements() { } } - // for each 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)); + // for each tag, if it has the data-format attribute, format + // the text according to the user's chosen locale and formatter. + formatAllTimeElements(); +} + +function formatAllTimeElements() { + const timeElements = document.querySelectorAll('time[data-format]'); + for (const timeElement of timeElements) { + const formatter = getFormatter(timeElement.dataset.format); + timeElement.textContent = formatter.format(new Date(timeElement.dateTime)); + } +} + +function getFormatter(format) { + switch (format) { + case 'date': + return dateFormatter; + case 'short-date': + return shortDateFormatter; + case 'date-time': + return dateTimeFormatter; + default: + throw new Error('Unknown format'); } }