I18n-module: Detect browser language when using `nuxt generate` (in 'universal' mode)

Created on 4 Jan 2019  路  6Comments  路  Source: nuxt-community/i18n-module

Version

v5.4.4

Reproduction link

https://github.com/tex0l/nuxt-i18n-repro-case

Steps to reproduce

Start a static server on a project built using nuxt generate in 'universal' mode. The test case provided does exactly that with an nginx server started with the result of the nuxt generate with docker.

What is expected ?

The detectBrowserLanguage feature should be used, just as in 'spa' mode.

What is actually happening?

The detectBrowserLanguage feature isn't used, considering it should be done server-side, but since there's no server, it doesn't word at all.

Additional comments?

The problem lies in the fact that it is done in a middleware: https://github.com/nuxt-community/nuxt-i18n/blob/master/src/templates/middleware.js#L101

When using nuxt generate, the middlewares are executed at build-time, not at run-time.

I worked around the issue by putting this in my layout's mounted method:

    if (process.browser) {
      if (this.$cookie.get('i18n_redirected') !== '1') {
        const lang = navigator.language.toLocaleLowerCase().substring(0, 2)
        this.$cookie.set('i18n_redirected', '1')
        this.$router.push(this.switchLocalePath(lang))
      }
    }
  }
This bug report is available on Nuxt community (#c185)
wontfix

Most helpful comment

While the above solution might work in specific cases, the middleware is doing a lot more in the meantime. Imo this issue should be fixed in nuxt-i18n, instead of just mentioning it in the documentation.

All 6 comments

Same problem here

Above solution works, but this should be mentioned in the documentation.

While the above solution might work in specific cases, the middleware is doing a lot more in the meantime. Imo this issue should be fixed in nuxt-i18n, instead of just mentioning it in the documentation.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I agree on this, it's a pity not to have the detection on generated websites (also trying desperately to make the bot reopen it 馃槵 )

Also agree. The above solution works but shows the default/fallback language translations before - almost immediately - switching to the detected language which, of course, isn't a wanted behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ebeloded picture ebeloded  路  15Comments

javialon26 picture javialon26  路  20Comments

koteezy picture koteezy  路  16Comments

varna picture varna  路  14Comments

NadhirBoukhenifra picture NadhirBoukhenifra  路  30Comments