如何使用 nextjs 从 url 中删除 /[lang 标签]?

how to remove the /[lang tag] from url using nextjs?

我想从 url 中删除 /en/it。我不希望将它明确地放在 url 中。 i18next 默认执行此操作,我不知道如何删除它。我只想在 background.

中制作它

我是否必须添加任何选项才能删除它,否则我无能为力?

我的代码如下:

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</>
 }

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

非常感谢!

在你的next.config.js.

i18n: {
  localeDetection: false,
}

这解决了我的一个问题。