From 515f5f5af3e2c1498d4e928ff5dfbe4baae11025 Mon Sep 17 00:00:00 2001
From: sunaurus <sander@saarend.com>
Date: Sat, 1 Jul 2023 18:57:19 +0300
Subject: [PATCH] Add fallback date-fns locale import

---
 src/shared/utils/app/setup-date-fns.ts | 30 ++++++++++++++++++++------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/shared/utils/app/setup-date-fns.ts b/src/shared/utils/app/setup-date-fns.ts
index 44f27acfe..97e0c2ebd 100644
--- a/src/shared/utils/app/setup-date-fns.ts
+++ b/src/shared/utils/app/setup-date-fns.ts
@@ -1,10 +1,12 @@
 import setDefaultOptions from "date-fns/setDefaultOptions";
 import { I18NextService } from "../../services";
 
+const EN_US = "en-US";
+
 export default async function () {
   let lang = I18NextService.i18n.language;
   if (lang === "en") {
-    lang = "en-US";
+    lang = EN_US;
   }
 
   // if lang and country are the same, then date-fns expects only the lang
@@ -17,12 +19,26 @@ export default async function () {
     }
   }
 
-  const locale = (
-    await import(
-      /* webpackExclude: /\.js\.flow$/ */
-      `date-fns/locale/${lang}`
-    )
-  ).default;
+  let locale;
+
+  try {
+    locale = (
+      await import(
+        /* webpackExclude: /\.js\.flow$/ */
+        `date-fns/locale/${lang}`
+      )
+    ).default;
+  } catch (e) {
+    console.log(
+      `Could not load locale ${lang} from date-fns, falling back to ${EN_US}`
+    );
+    locale = (
+      await import(
+        /* webpackExclude: /\.js\.flow$/ */
+        `date-fns/locale/${EN_US}`
+      )
+    ).default;
+  }
   setDefaultOptions({
     locale,
   });