Git Product home page Git Product logo

Comments (6)

neslinesli93 avatar neslinesli93 commented on May 13, 2024 1

I'm exporting the next project using static html, so it shouldn't be an issue for this use case. Thanks anyway!

from next-translate.

aralroca avatar aralroca commented on May 13, 2024 1

@neslinesli93 on 0.17.0-canary.1 is available the helper:

import Document, { Html, Head, Main, NextScript } from 'next/document'
import documentLang from 'next-translate/documentLang'

export default class MyDocument extends Document {
  render() {
    return (
      <Html lang={documentLang(this.props)}>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

from next-translate.

aralroca avatar aralroca commented on May 13, 2024

There isn't any helper of this library right now for this. (Maybe we can consider to add it 🙂).

Meanwhile, you can handle this with this:

import Document, { Html, Head, Main, NextScript } from 'next/document'
import i18nConfig from '../i18n.json'

function documentLang({ __NEXT_DATA__}) {
  const { page } = __NEXT_DATA__
  const [,langQuery] = page.split('/')
  const lang = i18nConfig.allLanguages.find(l => l === langQuery)

  return lang || i18nConfig.defaultLanguage
}

export default class MyDocument extends Document {
  render() {
    return (
      <Html lang={documentLang(this.props)}>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

from next-translate.

neslinesli93 avatar neslinesli93 commented on May 13, 2024

Thanks for the quick feedback. I was wondering, why it's not possible to use clientSideLang? It seems easier and more straightforward...

from next-translate.

aralroca avatar aralroca commented on May 13, 2024

The reason is that clientSideLang is a global variable. In the browser this is not a problem but in node.js it can have conflicts with concurrent users visiting the same page.

from next-translate.

aralroca avatar aralroca commented on May 13, 2024

In this case, is not a problem and you can use clientSideLang directly.

However, using SSR on some page (getServerSideProps) you can have conflicts and the only way is using documentLang, that this works in all cases.

from next-translate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.