From f354132d96601faa775734e402df2750fbaa058f Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 13 Oct 2022 14:13:31 +0000 Subject: [PATCH] Localize all timestamps Signed-off-by: Yarden Shoham --- 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 | 36 ++++++++++++++++--- 20 files changed, 55 insertions(+), 27 deletions(-) diff --git a/templates/admin/auth/list.tmpl b/templates/admin/auth/list.tmpl index 3ce138449d3b4..c43287ee1ae0d 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 b831f50c850d8..2777741efbe9d 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 aec3f2c8419cb..11dc23c60eb90 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 06d6163476a9f..b62e788799153 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 b26ec2eb78658..837802f0d0ce7 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 061e66385021d..56f6eaa3ad710 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 2073b8c69c697..36267baf33954 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 94a21d9959583..5fb79c0dd027a 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 5b95d3d8e56e8..eb95674b13d2e 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 2f8a4c6c1e25b..44c916eefbdb1 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 9030235e7c120..3c5996e903a73 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 eec31d8b5013d..efc85308203bc 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 0e5879c6126cb..2dd01184aa71b 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 a4ac65db6aed5..3b776bc16d5e4 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 9125f4bd00f6a..e05cebb0e4e80 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 e67fd2d22257f..878c258dd32a0 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 58835081796f3..fe6c0bbeb1f79 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 56992144686f1..b0cacbe54c519 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 bcab968d599fb..09339539096ef 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 5f4633bba2120..719fdb9ac65ca 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,34 @@ 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-forma attribute, format + // the text according to the user's chosen locale and formatter + formatAllTimeElements(); +} + +function formatAllTimeElements() { + const formats = ['date', 'short-date', 'date-time']; + for (const f of formats) { + formatTimeElements(f); + } +} + +function formatTimeElements(format) { + let formatter; + switch (format) { + case 'date': + formatter = dateFormatter; + break; + case 'short-date': + formatter = shortDateFormatter; + break; + case 'date-time': + formatter = dateTimeFormatter; + break; + default: + throw new Error('Unknown format'); + } + for (const timeElement of document.querySelectorAll(`time[data-format="${format}"]`)) { + timeElement.textContent = formatter.format(new Date(timeElement.dateTime)); } }