adbrosaci / vue-lang-router

Vue language routing with (optional) localized URLs.
MIT License
66 stars 8 forks source link

Force redirect to defaultLocale #16

Open biroplane opened 3 years ago

biroplane commented 3 years ago

I'm using this amazing plugin, but i can't figure out how to force / to go to /it/ path if none provided...

thanks.

radek-altof commented 3 years ago

Hi @biroplane, force redirect isn't currently implemented. I guess it can be added as a new feature.

What you mean is having an option to turn URL localization "always on", right? In such a case you wouldn't be able to access http://your-website.com/ at all, but it would always redirect you to some language, in your case http://your-website.com/it/.

Btw, defaultLanguage tells the app which language to use, in case it can't detect it from URL, last browsing session, or browser's preferences. It doesn't have any effect on URL localization right now.

biroplane commented 3 years ago

as far as i noticed. it just need to set defaultLocale to empty String and it does the job 👍

radek-altof commented 3 years ago

Interestingly, this can work for you, but it's not intentional and will fail eventually, with bunch of errors in the console along the way. Keep in mind that defaultLanguage is required parameter in order for the Vue Language Router to work properly.

If you don't specify the language in the path, the Vue Language Router will try to get the previously used language from local storage, where it is stored as VueAppLanguage. If it doesn't find it there, it tries to get preferred language from the browser. Tha last resort is using defaultLanguage, which should always be available in your app.

When determining language, if a preferred language is found, but your app doesn't support it, it cannot be used - then the defaultLanguage comes to play again.

What's likely happening in your case is that when the app obtains preferred language, it coincides with one of the translations you have in the app. Try doing this though:

  1. Go to browser settings and set your preferred language to some which you don't have translations for.
  2. Delete VueAppLanguage from local storage.
  3. Try loading the app without the language in the path, e.g. http://localhost:8080.

It will fail horribly. Keep using defaultLanguage 😉 Force redirect should be added as a new feature.

biroplane commented 3 years ago

i've just solved with a defaultLocale set to an empty string. it does the trick ;)

qxygene commented 2 years ago

can you give an example ?

biroplane commented 2 years ago

defaultLanguage:''

nothing more than this...