How do I dynamically import locales? #3460
Unanswered
SleeplessOne1917
asked this question in
Q&A
Replies: 2 comments 1 reply
-
On v2 it works like this for me: import { setDefaultOptions } from 'date-fns';
...
export const loadDateFnsLocale = async (locale: Locale) => {
return await import(
`date-fns/locale/${localeToDateFnsLocale[locale]}/index.js`
);
};
export const useDateFnsLocale = () => {
const {data} = loadDateFnsLocale('en-US'); // Example
if (data) {
setDefaultOptions({ locale: data.default });
}
...
}; So basically seems you missed the index.js on your import |
Beta Was this translation helpful? Give feedback.
1 reply
-
const checkLocale = async (lang: string) => {
const {[lang]: locale} = await import(`date-fns/locale`);
if (locale) {
registerLocale(lang, locale)
setDefaultLocale(lang);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
See this issue for context.
I set up the formatter like so:
You can see the error in the attached PR, and I can provide more information if needed.
Beta Was this translation helpful? Give feedback.
All reactions