Reputation: 1053
I use dayjs, and I want to change the locale with the current languages of i18next.language
but its not working, I get only the english format.
...
import dayjs from 'dayjs';
const { t, i18n } = useTranslation();
dayjs.locale(i18n.language);
...
Upvotes: 2
Views: 10435
Reputation: 1
Next-Intl currently has the hook useLocale() for client-side rendering. Be sure to import the locales from dayjs fist as well at the top of the component.
import "dayjs/locale/fr";
import "dayjs/locale/en";
import dayjs from "dayjs";
import { useLocale, useTranslations } from "next-intl";
const locale = useLocale();
dayjs().locale(locale).format("DD MMMM YYYY")
Upvotes: 0
Reputation: 1
import it from 'dayjs/locale/it' like so
import dayjs from 'dayjs';
And use it like so
dayjs.locale({...it});
Upvotes: 0
Reputation: 669
You should also import the required locale. It won't work unless:
import dayjs from 'dayjs';
import('dayjs/locale/en') // path must match with `i18n.language`
const { t, i18n } = useTranslation();
dayjs.locale(i18n.language);
Upvotes: 4
Reputation: 3168
Try to use i18next.resolvedLanguage
Something like this:
...
import dayjs from 'dayjs';
const { t, i18n } = useTranslation();
dayjs.locale(i18n.resolvedLanguage);
...
btw: it might be you're accessing i18n too early, alternatively try to check the ready flag:
...
import dayjs from 'dayjs';
const { t, i18n, ready } = useTranslation();
if (!ready) return 'loading translations...'
dayjs.locale(i18n.resolvedLanguage);
...
or alternatively subscribe to the language changed event (somewhere where you init i18next):
...
i18next.on('languageChanged', function(lng) {
import(`dayjs/locale/${lng}`).then(() => { // make sure you load the appropriate dayjs translations...
dayjs.locale(lng);
});
})
...
Upvotes: 1