Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback date-fns locale import #1729

Merged
merged 1 commit into from
Jul 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/shared/utils/app/setup-date-fns.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
});
Expand Down