I'm trying to create a website with all content shown at example.com/en (or /fr, /es, /it etc.) So basically when you go to example.com, I want to redirect to /en /fr /es.. based on certain parameters, such as cookies and the browser locale.
While trying to figure out a way to do this, the first step was skipping defining a defaultLocale. That way, all my languages are under their respective /locale/ url (it is also necessary to include the trailing slash, otherwise you get the error shown below).
However, when navigating to a non-existent URL without a defaultLocale, you get:
{ TypeError: Cannot set property 'error' of undefined
at Object.error (.nuxt/index.js:91:0)
at module.exports.__webpack_exports__.a (.nuxt/i18n.plugin.js:20:0)
at createApp (.nuxt/index.js:159:0)
at <anonymous> statusCode: 500, name: 'TypeError' }
Instead of the usual 404 not found that you get with defaultLocale defined.
So what I'm asking is more or less; is this expected behaviour? Are you supposed to always use a defaultLocale? And do you have any suggestion as to how I can achieve what I am trying to achieve?
Ps. Thank you for a truly superb module, the localePath() feature has helped me loads!
Hi @bobcat1
I did some tests and I think the issue might be caused by some changes in Nuxt v1 stable release, I'll need more time to test this better.
The defaultLocale is not required but I always assumed it would be set, I wonder if the module should have more options/features to handle automatic locale selection or if it should be up to the user to create a middleware to do this.
From my point of view (biased because of my use case) it would be great if it had an option that let you redirect to a specific /prefix, if defaultLocale is undefined. Or perhaps add an option to defaultLocale that if set to true, will also make the default language get a prefix, which it will then redirect to if someone tries to access the site without a prefix. Something like
defaultLocale: {
locale: 'en',
prefix: true
}
Just an idea, I don't know how much work that would take and I'd be happy with any solution as long as it's working :)
Upon further consideration of my specific use case outlined above, I might be shooting myself in the foot with the redirect suggestions I gave you, since I don't necessarily want to redirect to the same prefix every single time, but rather base the redirect on certain parameters (cookies, browser locale..).
The way I see it, there should be a simple redirect option that would simply redirect / to the default locale's homepage, but it also should be possible to pass a custom handler to make further checks and redirect to a specific language.
That sounds like a great idea :)
@bobcat1 thank you for asking the question. I am too looking to be able to set the language based on a cookie and or locale based on a geolocation service I'm using.
@paulgv
I'm wondering if there was a way to run switchLocalePath from within a plugin therefore the language is set before any of the component/pages are rendered. Or something equivalent to that
@bobcat1 The TypeError issue has been fixed in Nuxt 1.2.0 https://github.com/nuxt/nuxt.js/releases/tag/v1.2.0
@andrade1379 That's currently not possible since switchLocalePath is declared in a Vue mixin but it would be nice to have this option! I'll think about it.
@bobcat1 just release v2.1.0 which comes with 2 new options noPrefixDefaultLocale and redirectRootToLocale: https://github.com/paulgv/nuxt-i18n#options
You can use these 2 options to force URL prefixes for languages as well as achieving simple redirections from / to a given locale. Hope it helps a little, we still need to have an option for more complex redirections but it's a start :)
@paulgv Just had the chance to try it now, it works great! Thanks for the superquick updates :)
Most helpful comment
@bobcat1 just release v2.1.0 which comes with 2 new options
noPrefixDefaultLocaleandredirectRootToLocale: https://github.com/paulgv/nuxt-i18n#optionsYou can use these 2 options to force URL prefixes for languages as well as achieving simple redirections from
/to a given locale. Hope it helps a little, we still need to have an option for more complex redirections but it's a start :)