localize post dates

This commit is contained in:
arash16 2022-12-25 22:18:48 +03:30
parent d3d90be8a4
commit 59b35084c6
3 changed files with 35 additions and 1 deletions

21
assets/js/datelocale.ts Normal file
View File

@ -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;
}
}
});
}

View File

@ -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 }}
<script defer crossorigin="anonymous" src="{{ $built.Permalink }}" integrity="{{ $built.Data.Integrity }}"></script>
{{- else }}
{{ $built := resources.Get "js/datelocale.js" | js.Build $opts }}
<script defer crossorigin="anonymous" src="{{ $built.Permalink }}"></script>
{{- 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)) }}

View File

@ -1,7 +1,7 @@
{{- $scratch := newScratch }}
{{- if not .Date.IsZero -}}
{{- $scratch.Add "meta" (slice (printf "<span title='%s'>%s</span>" (.Date) (.Date | time.Format (default "January 2, 2006" site.Params.DateFormat)))) }}
{{- $scratch.Add "meta" (slice (printf "<span class='localizable-date' title='%s'>%s</span>" (.Date) (.Date | time.Format (default "January 2, 2006" site.Params.DateFormat)))) }}
{{- end }}
{{- if (.Param "ShowReadingTime") -}}