首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有next-i18next的NextJS生成错误文本内容不匹配

带有next-i18next的NextJS生成错误文本内容不匹配
EN

Stack Overflow用户
提问于 2021-06-21 20:41:27
回答 2查看 205关注 0票数 0

我想我已经完全按照他们的文档指定的方式设置了next-i18next,但是我收到了错误消息“text content not matching.server:"Testing ZH HANT”Client:"Testing EN“-前端只显示英文文本。我肯定遗漏了一些东西,但我终究看不到是什么。

以下是这些文件:

next-18next.config.js:

代码语言:javascript
复制
module.exports = {
    i18n: {
        locales: ['en-US', 'zh-hant', 'zh-hans'],
        defaultLocale: 'en-US',
        localeDetection: false
    },
};

next.config.js:

代码语言:javascript
复制
const { i18n } = require('./next-i18next.config');

module.exports = {
    images: {
        domains: ['mydomain.com'],
    },
    i18n,
    target: 'serverless'
}

_app.js:

代码语言:javascript
复制
import '../styles/globals.scss'
import Head from "next/head";
import { appWithTranslation } from 'next-i18next';

function MyApp({ Component, pageProps, mainMenu, footerMenu }) {
  return(
      <Component {...pageProps} />
  )
}

export default appWithTranslation(MyApp);

Article.js:

代码语言:javascript
复制
import { useTranslation } from 'next-i18next';

export default function Article(props){

    const node = props.node;
    const { t } = useTranslation(['common']);
return(
        <div>{t('Testing')}</div>
)}

export async function getStaticProps(context) {
    const { alias } = context.params;
    const lang = context.locale;
    const node = await restGet(`/endpoint?_format=json&slug=${alias.join('/')}&lang=${lang}`);
    const globals = await getGlobals(lang);
    const props = {
        ...{node: node},
        ...globals,
        ...(await serverSideTranslations(lang, ['common']))
    };

    return {
        props,
        revalidate: 5
    }
}

public/locales/zh-hant/common.json:

代码语言:javascript
复制
{
  "Testing": "Testing ZH HANS"
}
EN

回答 2

Stack Overflow用户

发布于 2021-06-21 21:02:50

您是否尝试过在i18next配置文件中指定localePath

代码语言:javascript
复制
const path = require('path')

module.exports = {
  ...
  localePath: path.resolve('./public/locales')
}
票数 0
EN

Stack Overflow用户

发布于 2021-06-21 22:05:23

事实证明,zh-hant实际上应该是zh-Hant -它总是细节:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68068135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档