Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example for a multi-language app with vue-router #50

Closed
stephanedemotte opened this issue Feb 10, 2016 · 2 comments
Closed

An example for a multi-language app with vue-router #50

stephanedemotte opened this issue Feb 10, 2016 · 2 comments

Comments

@stephanedemotte
Copy link

Hi !

I see a lot of comments talking about multi-language / vue-router and vuex.
#1 #2 #3

Internationalisation is missing in Vue.
And I think, it should be in the core system.

We have some good plugins like i18n
But it's not mature to be used in production see

I can't figure it out, what is the best way to do this?
Shouldn't this useful example be in the roadmap ?

To resume, we need

  • navigation change the language at the runtime (update history, and link)
  • different named route for the same page
  • possibility to load and insert data via ajax
  • possibility to access from anywhere to the hardcoded/dynamique data
  • possibility to inject data from a child component

This is my first approach but i know i'm wrong (using vue-router and vue-i18n).

route.js

import Vue from 'vue'
import p404 from './pages/404/404.vue'
import Home from './pages/home/Home.vue'
import About from './pages/about/About.vue'
import Work from './pages/work/Work.vue'
import Contact from './pages/contact/Contact.vue'

module.exports = function(router){

    router.map({
        '*': { component: p404 }, 

        '/': { name: 'home-en', component: Home, lang: 'en' }, 
        '/about': { name: 'about-en', component: About, lang: 'en' }, 
        '/work': { name: 'work-en', component: Work, lang: 'en' }, 
        '/contact-us': { name: 'contact-en', component: Contact, lang: 'en' },

        '/accueil': { name: 'home-fr', component: Home, lang: 'fr' }, 
        '/a-propos': { name: 'about-fr', component: About, lang: 'fr' }, 
        '/travaux': { name: 'work-fr', component: Work, lang: 'fr' }, 
        '/contactez-nous': { name: 'contact-fr', component: Contact, lang: 'fr' }
    })

  router.beforeEach(function (transition) {
    Vue.config.lang = transition.to.lang
    transition.next()
  })

}

navigation.vue

<template>
  <div class="Navigation">
    <div class="Navigation__link-wrapper">
      <a class="Navigation__link" 
        v-for="btn in menu" 
        v-link="{ name: btn.route, activeClass: 'Navigation__link--active' }">
          {{ $t(btn.name) }}
      </a>
      <a href="#" @click.prevent="changeLang">
        toggle lang
      </a>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue'

  export default {
    data() {
      return {
        lang: Vue.config.lang
      }
    },
    computed: {
      menu() {
        return [
          { route: 'about-' + this.lang, name: "navigation.about" },
          { route: 'work-' + this.lang, name: 'navigation.work' },
          { route: 'contact-' + this.lang, name: 'navigation.contact' },
        ]
      }
    },
    methods: {
      changeLang() {
        this.lang = (this.lang == 'fr') ? 'en' : 'fr'
      },
    }
  }
</script>

Any help will be appreciate !

@stephanedemotte stephanedemotte changed the title An example for a multi-language app and vue-router An example for a multi-language app with vue-router Feb 10, 2016
@stephanedemotte
Copy link
Author

ok i got something cool !

I will commit a example in 1 or 2 days :)

Thanks !

@blake-newman
Copy link
Member

Please place these type of requests within the Vue forums

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants