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

next-i18next调用HTTP接口获取翻译资源未生效如何解决

next-i18next 远程接口拉取翻译不生效排查修正

你的代码存在3个直接导致翻译失效的核心问题:

  • getStaticProps 不支持在_app.ts中使用。Next.js 仅在页面级路由文件中识别该生命周期,你写在App组件里的拉取逻辑根本不会执行,翻译资源自然无法注入到上下文。
  • serverSideTranslations 参数传参错误。该方法第二个参数是要加载的命名空间列表,你将动态获取的语言标识同时传入第一、第二个参数,会导致框架默认去本地public/locales目录查找对应语言文件,和你自定义注入的接口资源冲突。
  • 重复定义i18n基础配置。serverSideTranslations 会自动读取全局配置,你在方法内硬编码defaultLocale、locales等配置,很容易和全局初始化逻辑冲突,导致语言匹配失败。

修正步骤

  1. 先在项目根目录创建全局配置文件next-i18next.config.js,统一定义i18n基础规则,避免重复配置:
module.exports = {
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'zh'], // 替换为你实际支持的语言列表
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
  },
};

记得在next.config.js中引入该配置,保证Next.js路由层的多语言规则和i18n配置一致:

// next.config.js
const { i18n } = require('./next-i18next.config');
module.exports = {
  i18n,
  // 其他next配置
};
  1. 清理_app.ts中的冗余逻辑,仅保留App组件和appWithTranslation包裹即可,删除你写在这里的getStaticProps:
// pages/_app.tsx
import { appWithTranslation } from 'next-i18next';
import type { AppProps } from 'next/app';

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />;
}

export default appWithTranslation(MyApp);
  1. 将接口拉取翻译的逻辑移到页面级文件的getStaticProps(或getServerSideProps)中,只注入远程拉取的资源,不要重复覆盖基础配置:
// 示例:pages/index.tsx
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { useTranslation } from 'next-i18next';
import api from '你的api请求实例路径';

export default function HomePage() {
  // 对应你注入的translation命名空间
  const { t } = useTranslation('translation');
  // 直接调用t方法取翻译值即可,比如t('page.title')
  return <div>{t('page.title')}</div>;
}

export const getStaticProps = async ({ locale }) => {
  const res = await api.get('/translation');
  const remoteLocale = res.data.name;
  const remoteTransConfig = res.data.config;

  return {
    props: {
      ...(await serverSideTranslations(
        // 当前激活语言,优先用路由匹配的locale,兜底用接口返回的语言
        locale ?? remoteLocale,
        // 要加载的命名空间列表
        ['translation'],
        // 引入全局i18n配置
        require('../next-i18next.config.js'),
        // 仅注入远程拉取的翻译资源
        {
          resources: {
            [remoteLocale]: {
              translation: remoteTransConfig,
            },
          },
        }
      )),
    },
  };
};

如果需要全站复用远程翻译逻辑,可以把上述getStaticProps里的逻辑封装成公共方法,在每个页面引入调用即可。


快速排查清单

如果改完还是不生效,按以下顺序逐一核对:

  • 打开浏览器控制台,过滤i18next相关日志,看是否有namespace not loaded、unsupported locale类报错,直接根据报错定位是命名空间还是语言匹配问题
  • 在页面组件里打印useTranslation()返回的i18n对象,确认i18n.language是你预期的语言值,i18n.store.data下能找到你接口返回的翻译字段
  • 检查t()函数传入的key和接口返回的JSON结构完全一致,嵌套字段需要写全路径,比如接口返回{ home: { title: "首页" } },对应调用t('home.title')
  • 确认所有用到翻译的页面,都在对应的服务端数据获取方法里调用了serverSideTranslations注入资源,缺省的页面拿不到翻译上下文

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 02:06:32