dayjs 与 i18next 设置区域设置不起作用

发布于 2025-01-17 17:46:12 字数 201 浏览 1 评论 0原文

我使用 dayjs,我想用 i18next.language 的当前语言更改语言环境

,但它不起作用,我只得到英语格式。

...
import dayjs from 'dayjs';
const { t, i18n } = useTranslation();
dayjs.locale(i18n.language);
...

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);
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

亢潮 2025-01-24 17:46:12

您还应该导入所需的区域设置。它不会起作用,除非:

import dayjs from 'dayjs';
import('dayjs/locale/en') // path must match with `i18n.language`

const { t, i18n } = useTranslation();
dayjs.locale(i18n.language);

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);
梦中的蝴蝶 2025-01-24 17:46:12

尝试使用 i18next.resolvedLanguage

像这样:

...
import dayjs from 'dayjs';
const { t, i18n } = useTranslation();
dayjs.locale(i18n.resolvedLanguage);
...

顺便说一句:这可能是你太早访问 i18n,或者尝试检查 就绪标志

...
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);
  });
})
...

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);
  });
})
...
东北女汉子 2025-01-24 17:46:12

从“ dayjs/locale/it it”中导入它

import dayjs from 'dayjs';

,然后像这样使用

dayjs.locale({...it});

import it from 'dayjs/locale/it' like so

import dayjs from 'dayjs';

And use it like so

dayjs.locale({...it});
忘东忘西忘不掉你 2025-01-24 17:46:12

Next-Intl当前具有用于客户端渲染的Hook Uselocale()。
请确保从Dayjs拳头以及组件的顶部进口各个地方。

   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")
              

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")
              
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文