mardi 26 juin 2018

Switching language in laravel 5 / vue app2

In my laravel 5.6/vue.js 2.5.7 / I use vue-i18n 7.8 and laravel-vue-i18n-generator for multi language support:

and in resources/assets/js/app.js file I do like:

...
let lang = 'fr';
const i18n = new VueI18n({    // https://github.com/caouecs/Laravel-lang - Additive langs
    locale: lang, // set locale
    messages : Locale, // set locale messages
})

new Vue({ router, i18n,
    data:{
...

In my vue file wnen I need to read my current locale I can use like :

        created() {
            this.current_locale = this.$i18n;
        ...

To switch to other locale locale I can use a function like:

switchLocale () {
  if (this.$i18n.locale === 'fr') {
    this.$i18n.locale = 'en'
  } else {
    this.$i18n.locale = 'fr'
  }
}

The question is where to save current language when I moving to other page? I mean

let lang = 'fr';

at top of my code that is some default locale, but if locale has been changed, where from to read it? Some simple solution please. To generate prefix in url seems very complicated.

Thanks!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2lDq2dI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire