我使用dayjs,我想用i18next.language的当前语言更改语言环境
但它不起作用,我只得到英文格式。
...
import dayjs from 'dayjs';
const { t, i18n } = useTranslation();
dayjs.locale(i18n.language);
...
您还应该导入所需的区域设置。它不会工作,除非:
import dayjs from 'dayjs';
import('dayjs/locale/en') // path must match with `i18n.language`
const { t, i18n } = useTranslation();
dayjs.locale(i18n.language);
类似这样的:
...
import dayjs from 'dayjs';
const { t, i18n } = useTranslation();
dayjs.locale(i18n.resolvedLanguage);
...
btw:可能是您太早访问 i18n,或者尝试检查 ready 标志:
...
import dayjs from 'dayjs';
const { t, i18n, ready } = useTranslation();
if (!ready) return 'loading translations...'
dayjs.locale(i18n.resolvedLanguage);
...
或者订阅 语言更改事件(在您初始化 i18next 的地方):
...
i18next.on('languageChanged', function(lng) {
import(`dayjs/locale/${lng}`).then(() => { // make sure you load the appropriate dayjs translations...
dayjs.locale(lng);
});
})
...
从“dayjs”导入dayjs; 从 'dayjs/locale/it' 导入它;
dayjs.locale({...it});