You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

next-translate不显示默认语言,如何强制URL始终携带语言标识?

方案1:修复next-translate官方配置使其生效

你之前使用的showDefaultLocale: true是next-translate原生支持的参数,未生效通常是两个原因:

  • 所用的next-translate版本低于v1.2.0,该参数是这个版本之后新增的,先将包升级到最新稳定版即可
  • 没有在next.config.js中正确引入next-translate插件,正确的配置写法如下:
// next.config.js
const nextTranslate = require('next-translate-plugin')

module.exports = nextTranslate({
  // 其他next原生配置,比如reactStrictMode等
  reactStrictMode: true,
})

保留你现有的i18n.json配置不变,升级版本+正确引入插件后,默认语言ru的前缀/ru就会自动展示在URL中。配置修改完成后需要重启本地开发服务才会生效


方案2:用rewrites手动实现(兼容旧版本场景)

如果不想升级依赖,可以用next.config.js的rewrites和redirects组合实现需求,配置如下:

// next.config.js
module.exports = {
  i18n: {
    locales: ['en', 'ru'],
    defaultLocale: 'ru',
    localeDetection: false, // 关闭自动检测避免逻辑冲突
  },
  async redirects() {
    return [
      // 访问根路径自动跳转到带默认语言前缀的路径
      {
        source: '/',
        destination: '/ru',
        permanent: true,
        locale: false,
      },
      // 所有不带语言前缀的页面都跳转到带/ru前缀的对应路径
      {
        source: '/:path((?!en|ru).*)',
        destination: '/ru/:path',
        permanent: true,
        locale: false,
      }
    ]
  },
  async rewrites() {
    return [
      // 把带/ru前缀的路径映射到默认语言的页面逻辑,避免404
      {
        source: '/ru/:path*',
        destination: '/:path*',
        locale: false,
      }
    ]
  }
}

该配置会强制所有访问路径都带上语言前缀,不会影响原有多语言切换功能和页面逻辑。


内容的提问来源于stack exchange,提问作者Alexander Emelyanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 16:36:03