Skip to content

Instantly share code, notes, and snippets.

@ManzDev
Created March 28, 2019 14:01
Show Gist options
  • Select an option

  • Save ManzDev/2a865a197e0e4e53e5108354df83dfb2 to your computer and use it in GitHub Desktop.

Select an option

Save ManzDev/2a865a197e0e4e53e5108354df83dfb2 to your computer and use it in GitHub Desktop.
VueRouter & VueI18n example
{
"title": "¡Hola!",
"text": "Esto es un ejemplo"
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script defer src="./index.js"></script>
<title>Document</title>
</head>
<body>
<div id="app">
<p>
<router-link to="/es">Spanish</router-link>
<router-link to="/en">English</router-link>
</p>
<router-view></router-view>
</div>
</body>
</html>
import Vue from 'vue/dist/vue.esm';
import VueI18n from 'vue-i18n';
import VueRouter from 'vue-router';
Vue.use(VueI18n);
Vue.use(VueRouter);
import messages from './lang/*.json';
const Comp = {
props: ['lang'],
template: `<div>
<h2>{{ $t('title') }}</h2>
<p>{{ $t('text') }}</p>
</div>`,
watch: {
lang: {
handler(current, old) {
i18n.locale = current;
},
immediate: true
}
}
}
const routes = [
{ path: '/es', component: Comp, props: { lang: 'es' } },
{ path: '/en', component: Comp, props: { lang: 'en' } }
];
const i18n = new VueI18n({
locale: 'es',
fallbackLocale: 'en',
messages
});
const app = new Vue({
el: '#app',
router: new VueRouter({ routes }),
mounted() {
routes.push({ path: '/fr', component: Comp, params: { lang: 'fr' } });
},
i18n
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment