I am trying to redirect the user through a method when he clicks on specific buttons to change the language of the application.
These buttons are part of the standard layout of which all the pages of the project are part of so the url can differ based on which .vue
page the user is on but the buttons are always the same which means that the redirecting has to be dynamic.
For example the url :
localhost/about
Should be redirected through the method (by pressing the specific button) to :
localhost/bg/about
In the project the pages are inside a folder _locale
and imported also from the .vue files outside of this folder as suggested in the Nuxt documentation for localization with Vue-i18n https://nuxtjs.org/examples/i18n/
The same way are implemented the nuxt.config.js
, i18n.js
middleware, i18n.js
plugin and index.js
store files.
Since it is a Nuxt.js
application and not just a Vue.js
one, just committing the mutation of the store through a page changing the locale
(the language) doesn't also actually change the language and even if it did ( using this.$i18n.locale = 'bg'
for example ) it only changes it for the specific page and with the next navigation it uses again the fallback locale
instead, so I am trying to solve it with redirecting which does use then the correct .json
file of the language based on the url. I am just trying to find a way for this redirecting to be more dynamic instead of nagigating only back to the home
page of each language.
If needed the github repo : https://github.com/alexgil1994/logistics-gls
Steps needed :
npm install
npm run dev
Could it be done in some better way? Is there any example of Regex for such a situation? Is there an easier way than Regex?
All suggestions are welcome.