Reza Ghorbani
Reza Ghorbani

Reputation: 537

how to change <Html "lang"/> with next-i18next when language changed in nextjs?

I'm using next-i18next for multi language website and for all components works well but I dont know how to change the language of html tag in _document.js file?

Upvotes: 1

Views: 2728

Answers (1)

bcjohn
bcjohn

Reputation: 2523

Here is my solution.

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>
    ) 
  } 
}

Upvotes: 3

Related Questions