Cant remove language from router in Next.js using ni18n

j9per5c4  于 2023-08-04  发布在  其他
关注(0)|答案(1)|浏览(65)

我试图在Next.js上使用ni 18 n开发一个网站,但即使我强制删除它,我也无法从URL中删除语言部分。
我想让它看起来像这样。
“http://localhost:3000”
但是当我尝试输入它的网站时,它路由到“localhost:3000/tr”。
知道为什么会这样吗我似乎想不明白。

//_app.tsx
function MyApp({ Component, pageProps }: AppPropsWithLayout) {
  // Use the layout defined at the page level, if available
  const getLayout = Component.getLayout ?? ((page) => page)
  
  if(typeof window !== 'undefined'){
    const locale = window.localStorage.getItem('locale') || 'en'
    useSyncLanguage(locale)
  }
  
  
  return getLayout(
      <ThemeProvider attribute='class'>
        <Component {...pageProps} />
      </ThemeProvider>
  )
}

x

// ni18n.config.ts
import type { Ni18nOptions } from 'ni18n'

export const ni18nConfig: Ni18nOptions = {
  supportedLngs: ['en', 'tr'],
  ns: ['common','navbar'],
}
//18next.d.ts
declare module 'react-i18next' {
  interface CustomTypeOptions {
    resources: {
        common: typeof common,
        navbar: typeof navbar
    }
  }
}
//next.config.js
module.exports = {
    i18n: {
      defaultLocale: 'en',
      locales: ['en', 'tr'],
    },
  }

的数据

6ioyuze2

6ioyuze21#

经过一番苦思冥想,我终于明白了为什么会发生这种情况。这是由于18next中的“localeDetection”设置默认为“true”。只需在配置文件中设置即可修复此问题。

module.exports = {
  i18n: {
    ...
    localeDetection: false,
  },
}

字符串

相关问题