From 603c181ad27109327617a78e5f672ebf68edc267 Mon Sep 17 00:00:00 2001
From: Mara-Li <lili.simonetti@outlook.fr>
Date: Mon, 29 Jan 2024 06:13:59 +0000
Subject: [PATCH] feat: allow to config a translation for date (#739)

---
 quartz/components/RecentNotes.tsx  |    2 +-
 quartz/components/PageList.tsx     |    2 +-
 quartz/plugins/transformers/ofm.ts |    2 +-
 quartz/cfg.ts                      |    6 ++++++
 quartz/components/ContentMeta.tsx  |    2 +-
 quartz/components/Date.tsx         |    9 +++++----
 6 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/quartz/cfg.ts b/quartz/cfg.ts
index 7f0f206..153bbe2 100644
--- a/quartz/cfg.ts
+++ b/quartz/cfg.ts
@@ -35,6 +35,12 @@
    */
   baseUrl?: string
   theme: Theme
+  /**
+   * The locale to use for date formatting. Default to "en-US"
+   * Allow to translate the date in the language of your choice.
+   * Need to be formated following the IETF language tag format (https://en.wikipedia.org/wiki/IETF_language_tag)
+   */
+  locale?: string
 }
 
 export interface QuartzConfig {
diff --git a/quartz/components/ContentMeta.tsx b/quartz/components/ContentMeta.tsx
index 254c5d4..5337fee 100644
--- a/quartz/components/ContentMeta.tsx
+++ b/quartz/components/ContentMeta.tsx
@@ -24,7 +24,7 @@
       const segments: string[] = []
 
       if (fileData.dates) {
-        segments.push(formatDate(getDate(cfg, fileData)!))
+        segments.push(formatDate(getDate(cfg, fileData)!, cfg.locale))
       }
 
       // Display reading time if enabled
diff --git a/quartz/components/Date.tsx b/quartz/components/Date.tsx
index 8713cfd..6feac17 100644
--- a/quartz/components/Date.tsx
+++ b/quartz/components/Date.tsx
@@ -3,6 +3,7 @@
 
 interface Props {
   date: Date
+  locale?: string
 }
 
 export type ValidDateType = keyof Required<QuartzPluginData>["dates"]
@@ -16,14 +17,14 @@
   return data.dates?.[cfg.defaultDateType]
 }
 
-export function formatDate(d: Date): string {
-  return d.toLocaleDateString("en-US", {
+export function formatDate(d: Date, locale = "en-US"): string {
+  return d.toLocaleDateString(locale, {
     year: "numeric",
     month: "short",
     day: "2-digit",
   })
 }
 
-export function Date({ date }: Props) {
-  return <>{formatDate(date)}</>
+export function Date({ date, locale }: Props) {
+  return <>{formatDate(date, locale)}</>
 }
diff --git a/quartz/components/PageList.tsx b/quartz/components/PageList.tsx
index eb34f02..644e354 100644
--- a/quartz/components/PageList.tsx
+++ b/quartz/components/PageList.tsx
@@ -46,7 +46,7 @@
             <div class="section">
               {page.dates && (
                 <p class="meta">
-                  <Date date={getDate(cfg, page)!} />
+                  <Date date={getDate(cfg, page)!} locale={cfg.locale} />
                 </p>
               )}
               <div class="desc">
diff --git a/quartz/components/RecentNotes.tsx b/quartz/components/RecentNotes.tsx
index 63f040d..9a53ad7 100644
--- a/quartz/components/RecentNotes.tsx
+++ b/quartz/components/RecentNotes.tsx
@@ -47,7 +47,7 @@
                   </div>
                   {page.dates && (
                     <p class="meta">
-                      <Date date={getDate(cfg, page)!} />
+                      <Date date={getDate(cfg, page)!} locale={cfg.locale} />
                     </p>
                   )}
                   <ul class="tags">
diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index 7c9b33b..1eeb783 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -441,7 +441,7 @@
                   value: `<div
                   class="callout-title"
                 >
-                  <div class="callout-icon">${callouts[calloutType] ?? callouts.note}</div> 
+                  <div class="callout-icon">${callouts[calloutType] ?? callouts.note}</div>
                   <div class="callout-title-inner">${title}</div>
                   ${collapse ? toggleIcon : ""}
                 </div>`,

--
Gitblit v1.10.0