From 59b35084c617d41b1d13c6f7eb43bd5553c6738b Mon Sep 17 00:00:00 2001 From: arash16 Date: Sun, 25 Dec 2022 22:18:48 +0330 Subject: [PATCH 1/2] localize post dates --- assets/js/datelocale.ts | 21 +++++++++++++++++++++ layouts/partials/head.html | 13 +++++++++++++ layouts/partials/post_meta.html | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 assets/js/datelocale.ts diff --git a/assets/js/datelocale.ts b/assets/js/datelocale.ts new file mode 100644 index 0000000..e0adfd3 --- /dev/null +++ b/assets/js/datelocale.ts @@ -0,0 +1,21 @@ +function formatDate(date: string | null) { + if (!date) return date; + return new Intl.DateTimeFormat(process.env.lang_code, { + year: 'numeric', + month: 'long', + day: 'numeric', + // hour:'2-digit', + // minute: '2-digit', + }).format(new Date(date)); +} + +if ('Intl' in window) { + window.addEventListener('load', () => { + for(const span of [...document.querySelectorAll('.localizable-date')]) { + var persianDate = formatDate(span.getAttribute('title')); + if (persianDate) { + span.innerHTML = persianDate; + } + } + }); +} \ No newline at end of file diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 35257fc..4d3996e 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -89,6 +89,19 @@ {{- end }} {{- end -}} +{{- /* localize dates */}} +{{- if (not site.Params.disableDateLocalization) }} +{{ $defines := dict "process.env.lang_code" (printf `"%s"` site.LanguageCode) }} +{{ $opts := dict "targetPath" "datelocale.ts" "defines" $defines }} +{{- if not site.Params.assets.disableFingerprinting }} +{{ $built := resources.Get "js/datelocale.ts" | js.Build $opts | fingerprint }} + +{{- else }} +{{ $built := resources.Get "js/datelocale.js" | js.Build $opts }} + +{{- end }} +{{- end }} + {{- /* Highlight.js */}} {{- $isHLJSdisabled := (site.Params.assets.disableHLJS | default .Params.disableHLJS ) }} {{- if (and (eq .Kind "page") (ne .Layout "archives") (ne .Layout "search") (not $isHLJSdisabled)) }} diff --git a/layouts/partials/post_meta.html b/layouts/partials/post_meta.html index 15b8b1b..947fd0e 100644 --- a/layouts/partials/post_meta.html +++ b/layouts/partials/post_meta.html @@ -1,7 +1,7 @@ {{- $scratch := newScratch }} {{- if not .Date.IsZero -}} -{{- $scratch.Add "meta" (slice (printf "%s" (.Date) (.Date | time.Format (default "January 2, 2006" site.Params.DateFormat)))) }} +{{- $scratch.Add "meta" (slice (printf "%s" (.Date) (.Date | time.Format (default "January 2, 2006" site.Params.DateFormat)))) }} {{- end }} {{- if (.Param "ShowReadingTime") -}} From 97b0c53bc8f4677177e9c46793270cb5f525cf6d Mon Sep 17 00:00:00 2001 From: arash16 Date: Sun, 25 Dec 2022 22:26:03 +0330 Subject: [PATCH 2/2] cleanup namings --- assets/js/datelocale.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/assets/js/datelocale.ts b/assets/js/datelocale.ts index e0adfd3..f26a0fe 100644 --- a/assets/js/datelocale.ts +++ b/assets/js/datelocale.ts @@ -4,18 +4,16 @@ function formatDate(date: string | null) { year: 'numeric', month: 'long', day: 'numeric', - // hour:'2-digit', - // minute: '2-digit', }).format(new Date(date)); } if ('Intl' in window) { window.addEventListener('load', () => { for(const span of [...document.querySelectorAll('.localizable-date')]) { - var persianDate = formatDate(span.getAttribute('title')); - if (persianDate) { - span.innerHTML = persianDate; + const localizedDate = formatDate(span.getAttribute('title')); + if (localizedDate) { + span.innerHTML = localizedDate; } } }); -} \ No newline at end of file +}