i18next Undefined不是一个对象(评估' _i18next.default.services.formatter.add')

发布于 2025-01-17 18:43:31 字数 1313 浏览 1 评论 0 原文

为什么我会收到此错误消息:

undefined is not an object (evaluating '_i18next.default.services.formatter.add')

代码:

import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';
import { DateTime } from 'luxon';

i18next
  .use(HttpApi)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    react: {
      useSuspense: true
    },
    fallbackLng: 'en',
    compatibilityJSON: 'v3',
    lng: 'de',
    debug: process.env.NODE_ENV === 'development',
    backend: {
      loadPath: `http://192.168.0.249:4000/public/locales/{{lng}}/translation.json`,
    },
    interpolation: {
      escapeValue: false,
    },
    keySeparator: '.'
  });

// new usage
i18next.services.formatter.add('DATE_HUGE', (value, lng, options) => {
  return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime.DATE_HUGE)
});

export default i18next;

有人可以帮助我解决这个问题吗?

我从那里得到它:

why I get this error message:

undefined is not an object (evaluating '_i18next.default.services.formatter.add')

Code:

import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';
import { DateTime } from 'luxon';

i18next
  .use(HttpApi)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    react: {
      useSuspense: true
    },
    fallbackLng: 'en',
    compatibilityJSON: 'v3',
    lng: 'de',
    debug: process.env.NODE_ENV === 'development',
    backend: {
      loadPath: `http://192.168.0.249:4000/public/locales/{{lng}}/translation.json`,
    },
    interpolation: {
      escapeValue: false,
    },
    keySeparator: '.'
  });

// new usage
i18next.services.formatter.add('DATE_HUGE', (value, lng, options) => {
  return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime.DATE_HUGE)
});

export default i18next;

Can anyone help me to solve this issue ?

I get it from there:

https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb#formatting

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

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

发布评论

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

评论(1

枯叶蝶 2025-01-24 18:43:31

您可能不会使用i18next> = 21.3.0

在这种情况下,请使用旧格式格式选项: https://www.i18next.com/translation-function/formatting#legacy-format-function-format-function-i18next-ibles-ch-les--less-ems-than-than-21.3.0

interpolation: {
  escapeValue: false, // not needed for react as it escapes by default
  format: (value, format, lng) => { // legacy usage
    if (value instanceof Date) {
      return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime[format])
    }
    return value;
  }
},

You're probably not using i18next >= 21.3.0

in that case use the legacy formatting option: https://www.i18next.com/translation-function/formatting#legacy-format-function-i18next-less-than-21.3.0

interpolation: {
  escapeValue: false, // not needed for react as it escapes by default
  format: (value, format, lng) => { // legacy usage
    if (value instanceof Date) {
      return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime[format])
    }
    return value;
  }
},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文