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

next-i18next查询字符串检测Locale失效,配置缺失项咨询

问题分析与解决方案

你的配置存在两个关键问题,导致查询字符串检测Locale无效:

1. 查询字符串参数名不匹配

你期望通过lang参数(如?lang=en)检测Locale,但配置中lookupQuerystring设置的是lng,两者不一致,导致无法识别参数。

2. Next.js默认Locale检测逻辑覆盖自定义规则

Next.js默认启用的localeDetection会优先采用路径式Locale,并且会覆盖next-i18next的自定义检测逻辑,需要手动关闭该选项。


修改后的next-i18next.config.js配置

module.exports = {
  i18n: {
    defaultLocale: 'en',
    locales: ['ko', 'en', 'ja', 'zh'],
    localeDetection: false, // 关闭Next.js默认的Locale检测
  },
  detection: {
    order: ['querystring', 'cookie', 'localStorage'],
    lookupQuerystring: 'lang', // 改为你使用的参数名
  },
};

同步配置到next.config.js

确保Next.js的配置文件中引入并使用上述i18n配置,否则自定义检测规则无法生效:

const { i18n } = require('./next-i18next.config');

module.exports = {
  i18n,
  // 其他Next.js配置项
};

完成以上修改后,访问www.example.com/detail?lang=en即可正确检测到Locale为英文,同时路径式Locale(如www.example.com/en/detail)仍可正常使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 18:48:24