Search code examples
next.jsi18nextnext-i18next

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?


Solution

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