I got this fine idea about translating routes paths, which doesn't sound so clever any more :), once I have encountered a problem. I hope you guys will see/find a solution.
This is my routes.js file, where routes are defined
export default [
{
path: '/:lang',
component: {
template: '<router-view></router-view>'
},
children: [
{
path: '',
name: 'Home',
component: load('Home')
},
{
path: translatePath('contact'),
name: 'Contact',
component: load('Contact')
},
{
path: translatePath('cookiePolicy'),
name: 'CookiePolicy',
component: load('CookiePolicy')
},
]
},
]
// and my simple function for translating paths
function translatePath(path) {
let lang = Cookie.get('locale');
let pathTranslations = {
en: {
contact: 'contact',
cookiePolicy: 'cookie-policy',
},
sl: {
contact: 'kontakt',
cookiePolicy: 'piskotki',
}
};
return pathTranslations[lang][path];
}
And this is my change language functionality in my component
setLocale(locale) {
let selectedLanguage = locale.toLowerCase();
this.$my.locale.setLocale(selectedLanguage); // update cookie locale
console.log(this.$route.name);
this.$router.replace({ name: this.$route.name, params: { lang: selectedLanguage } });
location.reload();
},
The problem is following. When user executes the change language functionality I successfully change lang param, but the this.$route.name
keeps the same in old language. Is there a way to "reload" routes, so there will be new routes paths, which will include proper language?
If you need any additional informations, please let me know and I will provide. Thank you!
Check this basic example, you can see the path that change according to the selected language. This can be improved using some translation plugin like vue-i18n and can also be wrapped into a mixin for more reusability.
const Home = {
template: '<div>Home</div>'
}
const Contact = {
template: '<div>CONTACT ==> {{$route.path}}</div>'
}
const Cookie = {
template: '<div>COOKIE ==> {{$route.path}}</div>'
}
const router = new VueRouter({
routes: [{
name: 'home',
path: '/',
component: Home
},
{
name: 'contact',
path: '/:pathTranslation',
component: Contact
},
{
name: 'cookies',
path: '/:pathTranslation',
component: Cookie
}
]
})
new Vue({
router,
el: '#app',
data: {
lang: 'IT',
translations: {
IT: {
contact: 'Contatti',
cookies: 'Cookies IT',
},
EN: {
contact: 'Contacts',
cookies: 'Cookies EN',
}
}
},
watch: {
lang: function(newLang) {
this.goto(this.$route.name, newLang);
}
},
methods: {
goto(path, lang=null) {
this.$router.replace({
name: path,
params: {
pathTranslation: this.translations[lang || this.lang][path]
}
});
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script>
<div id="app">
<select v-model="lang">
<option value="IT">IT</option>
<option value="EN">EN</option>
</select>
<button @click="goto('contact')">To contact</button>
<button @click="goto('cookies')">To cookies</button>
<router-view></router-view>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With