【问题标题】:how to remove the /[lang tag] from url using nextjs?如何使用 nextjs 从 url 中删除 /[lang 标签]?
【发布时间】:2021-07-01 02:38:38
【问题描述】:

我想从网址中删除 /en/it。我不希望它明确地将它放在 url 中。 i18next 默认情况下会这样做,我不知道如何删除它。我只想在背景中制作它。

我必须添加任何选项来删除它还是我无能为力?

我的代码如下:

import i18n from 'i18next'
import Backend from 'i18next-http-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
import { initReactI18next } from 'react-i18next'

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: 'en',
    debug: true,

    interpolation: {
      escapeValue: false,
    },
    react: {
      useSuspense: false,
    },
  })

export default i18n

我在我要翻译的页面中导入这个

import { useTranslation } from 'react-i18next';

function MyComponent: ReactElement {
    const [isEnglish, setIsEnglish] = useState(true);
    const { t, i18n } = useTranslation();
    const handleClick = () => {
        setIsEnglish(!isEnglish);
        i18n.changeLanguage(!isEnglish ? 'en' : 'it');
    }
    return <>My stuff</>
 }

正如我所说,它添加到我的路线/[语言代码]。有没有办法删除它并在“背景”中设置它?

非常感谢!

【问题讨论】:

  • 你为什么使用use(Backend)?我认为这就是问题所在。您可能需要查看github.com/isaachinman/next-i18next 之类的内容,以便通过服务器端区域设置检测正确设置国际化。

标签: javascript reactjs internationalization next.js i18next


【解决方案1】:

在你的next.config.js

i18n: {
  localeDetection: false,
}

这为我解决了一个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2018-05-16
    • 2019-11-29
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多