微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NextJS 语言环境更改与动态路由

如何解决NextJS 语言环境更改与动态路由

语言环境更改在我的 next.js 应用程序中运行良好,除了动态路由。在浏览器地址栏中,我确实得到了从 http://localhost:3000/client/home/profilehttp://localhost:3000/de/client/home/profile 的转换,但是页面给出了 404 错误...

我的动态页面[tab].tsx

import router from 'next/router'
import dynamic from 'next/dynamic'
import styled from 'styled-components'
import {useTranslation,i18n} from 'next-i18next'
import {useEffect,useState,useContext} from 'react'
import {serverSideTranslations} from 'next-i18next/serverSideTranslations'

import Layout from 'layouts'
import {DB,PATH} from 'utils/constants'
import {GlobalContext} from 'utils/contexts'
import {Tabs,LanguageSelector} from 'components/ui'
import {ChartData,ChartDataPoint,UsageStats} from 'utils/types'
import {getData,toTitleCase,isClient,emailVerified} from 'utils/helpers'

const Docs = dynamic(() => import('components/client/docs'))
const Stats = dynamic(() => import('components/client/stats'))
const Profile = dynamic(() => import('components/client/profile'))

const Tab = ({tab}) => {

  const {t} = useTranslation()

  return (
    <Layout>
      <LanguageSelector />
      <Tabs
        basePath={'/client/home'}
        tab={tab}
        tabs={[
          {
            slug: 'stats',label: t('client.home.stats'),component: <Stats data={data} />
          },{
            slug: 'profile',label: t('client.home.profile'),component: <Profile client={client} />
          },{
            slug: 'docs',label: t('client.home.docs'),component: <Docs />
          }
        ]}
      />
    </Layout>
  )
}

export const getStaticProps = async ({params,locale}) => ({
  props: {
    tab: params.tab,...await serverSideTranslations(locale)
  }
})

export const getStaticPaths = async () => {
  return {
    paths: [
      {params: {tab: 'stats'}},{params: {tab: 'profile'}},{params: {tab: 'docs'}}
    ],fallback: false
  }
}

export default Tab

我在 LanguageSelector.tsx 中进行区域设置切换:

import router from 'next/router'
import {i18n} from 'next-i18next'
import {useState,useEffect} from 'react'

import {Select} from '.'
import {LANGUAGES} from 'utils/constants'


export const LanguageSelector = () => {

  const [locale,setLocale] = useState(i18n.language)

  useEffect(() => {
    const {pathname,asPath,query} = router
    router.push({pathname,query},{locale})
  },[locale])

  return (
    <Select 
      borderless
      isSearchable={false}
      value={i18n.language}
      options={LANGUAGES.filter(language => language !== i18n.language)}
      onValueChange={setLocale}
    />
  )
}

知道我的错误在哪里吗?

解决方法

如果您将 getStaticPaths 与内置 Next.js i18n 路由一起使用,那么您还需要返回带有路径的区域设置键,如下所示:

export const getStaticPaths = ({ locales }) => {
  return {
    paths: [
      { params: { slug: 'post-1' },locale: 'en-US' },{ params: { slug: 'post-1' },locale: 'fr' },],fallback: true,}
}

More info in the docs

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?