首页 > 解决方案 > 在 nextjs 中更改语言时如何使用 next-i18next 进行更改?

问题描述

我正在将 next-i18next 用于多语言网站,并且所有组件都运行良好,但我不知道如何更改 _document.js 文件中 html 标记的语言?

标签: next.jsi18nextnext-i18next

解决方案


这是我的解决方案。

class MyDocument extends Document {
  static async getInitialProps(ctx) {
     const initialProps = await Document.getInitialProps(ctx)
     const language = ctx.req.language
     return { ...initialProps, language }
  }

  render() {
    return (
       <Html lang={this.props.language}>
         <Head />
         <body>
           <Main />
           <NextScript />
         </body>
       </Html>
    ) 
  } 
}

推荐阅读