首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >您将需要使用i18next传递一个initReactI18next实例

您将需要使用i18next传递一个initReactI18next实例
EN

Stack Overflow用户
提问于 2022-05-18 10:29:18
回答 1查看 2.6K关注 0票数 0

我试图添加翻译到我的网站默认教程,

my /i18n.js

代码语言:javascript
复制
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
// don't want to use this?
// have a look at the Quick start guide 
// for passing in lng and translations on init

i18n
  // load translation using http -> see /public/locales (i.e. https://github.com/i18next/react-i18next/tree/master/example/react/public/locales)
  // learn more: https://github.com/i18next/i18next-http-backend
  // want your translations to be loaded from a professional CDN? => https://github.com/locize/react-tutorial#step-2---use-the-locize-cdn
  .use(Backend)
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    fallbackLng: 'en',
    debug: true,

    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    }
  });


export default i18n;

/page/_document.js

代码语言:javascript
复制
import { Html, Head, Main, NextScript } from 'next/document';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
export default function Document(){
  return(
    <Html>
      <Head>
        <body>
          <Main/>
          <NextScript/>
        </body>
      </Head>
    </Html>
  );
}

export async function getStaticProps({ locale }) {
  return {
    props: {
      ...(await serverSideTranslations(locale, ['common', 'footer'])),
      // Will be passed to the page component as props
    },
  };
}

和/component/Navbar.js中的字符串转换的navbar

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

const Nav = () => {

    const { t, i18n } = useTranslation('navbar');

    
    return (
        <div>
        {t('home')}
        </div>
    )
}

export default Nav;

/page/_app.js

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

function MyApp({ Component, pageProps }) {
  return (

  <Component {...pageProps} />

  )
}

export default appWithTranslation(MyApp);

但是翻译仍然不起作用,我试图添加异步函数getStaticProps,以便将其转换为Navbar组件,但是出现了错误“无法解析'fs'”--因为您已经在运行客户端代码的某个地方导入了serverSideTranslations

EN

回答 1

Stack Overflow用户

发布于 2022-11-22 09:55:30

我知道这是一个迟来的答案,但希望它能帮助到其他人:

您得到了这个错误"Can't resolve 'fs'" - because you have imported serverSideTranslations in some place where client side code is being run --因为getStaticPropsgetServerSideProps只应该在\pages目录中使用。

要使用\pages之外的转换,只需对useTranslation()对象const { t } = useTranslation('navbar');进行import { useTranslation } from 'next-i18next';和析构

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

https://stackoverflow.com/questions/72287454

复制
相关文章

相似问题

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