question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect browser language when using `nuxt generate` (in 'universal' mode)

See original GitHub issue

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))
      }
    }
  }
<div align="right">This bug report is available on Nuxt community (#c185)</div>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:9

github_iconTop GitHub Comments

3reactions
Sopamocommented, May 15, 2019

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.

0reactions
Tommyttocommented, Jan 17, 2021

@rchl you are right, browser detection works, but it was really strange. When I’ve deployed it on Netlify it didn’t work, but on Firebase hosting everything works properly

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detect browser language when using nuxt generate ... - GitHub
Start a static server on a project built using nuxt generate in 'universal' mode. The test case provided does exactly that with an...
Read more >
Nuxt.js' SSR/Universal Mode: What Is It and How to Host It
Hosting. So, to get started with the hosting itself, create a Nuxt project using npx create-nuxt-app my-project command with the config ...
Read more >
Nuxt.js i18n localization doesn't work in SPA mode
I ended up detecting the locale myself, and updated the internal i18n locale for the rest of the code (issue #1067): async asyncData({$axios,...
Read more >
Nuxt - The Intuitive Vue Framework
Build your next Vue.js application with confidence using Nuxt. An open source framework making web development simple and powerful.
Read more >
The complete guide to building a multilanguage ... - Storyblok
This the ultimate Nuxt.js guide for beginners and professionals who want to build a full-blown multilanguage website using Nuxt, TailwindCSS & Storyblok.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found