From: sunaurus Date: Sat, 1 Jul 2023 15:57:19 +0000 (+0300) Subject: Add fallback date-fns locale import X-Git-Url: http://these/git/%7Bcv.comment.ap_id%7D?a=commitdiff_plain;h=515f5f5af3e2c1498d4e928ff5dfbe4baae11025;p=lemmy-ui.git Add fallback date-fns locale import --- diff --git a/src/shared/utils/app/setup-date-fns.ts b/src/shared/utils/app/setup-date-fns.ts index 44f27ac..97e0c2e 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, });