Merge pull request #1729 from sunaurus/locale_fallback

Add fallback date-fns locale import
This commit is contained in:
SleeplessOne1917 2023-07-01 22:39:22 +00:00 committed by GitHub
commit d00a4fa546
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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