Created
January 10, 2018 10:20
-
-
Save exophunk/c3ced7c9c267f9b030eeb4c3f29a6c13 to your computer and use it in GitHub Desktop.
Vue Router + Vue i18n lang switch with lazy loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Vue from 'vue'; | |
| import VueI18n from 'vue-i18n'; | |
| //import messagesDe from 'lang/de'; | |
| // import messagesFr from 'lang/fr'; | |
| // import messagesIt from 'lang/it'; | |
| Vue.use(VueI18n); | |
| const loadedLanguages = []; | |
| const i18n = new VueI18n({ | |
| locale: null, | |
| fallbackLocale: 'de', | |
| // messages: { | |
| // //de: messagesDe, | |
| // // fr: messagesFr, | |
| // // it: messagesIt, | |
| // }, | |
| }); | |
| export default i18n; | |
| export function setI18nLanguage(lang) { | |
| i18n.locale = lang; | |
| document.querySelector('html').setAttribute('lang', lang); | |
| return lang; | |
| } | |
| export function loadLanguageAsync(lang) { | |
| if (i18n.locale !== lang) { | |
| if (!loadedLanguages.includes(lang)) { | |
| return import(/* webpackChunkName: "lang-[request]" */ `lang/${lang}`).then(msgs => { | |
| i18n.setLocaleMessage(lang, msgs.default); | |
| loadedLanguages.push(lang); | |
| return setI18nLanguage(lang); | |
| }) | |
| } | |
| return Promise.resolve(setI18nLanguage(lang)); | |
| } | |
| return Promise.resolve(lang); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Vue from 'vue'; | |
| import VueRouter from 'vue-router'; | |
| import {loadLanguageAsync} from 'i18n'; | |
| import PageLanding from 'components/pages/PageLanding.vue'; | |
| Vue.use(VueRouter); | |
| const routes = [ | |
| { path: '/', redirect: { name: 'index', params: { langSlug: 'mut' } }}, | |
| { | |
| name: 'index', | |
| path: '/:langSlug', | |
| component: PageLanding, | |
| }, | |
| ]; | |
| const router = new VueRouter({ | |
| routes: routes, | |
| mode: 'history', | |
| }); | |
| router.beforeEach((to, from, next) => { | |
| const lang = mapSlugToLang(to.params.langSlug); | |
| loadLanguageAsync(lang).then(() => next()); | |
| }) | |
| function mapSlugToLang(slug) { | |
| switch(slug) { | |
| case 'mut': | |
| return 'de'; | |
| case 'courage': | |
| return 'fr'; | |
| case 'coraggio': | |
| return 'it'; | |
| } | |
| } | |
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| webpack: { | |
| ... | |
| output: { | |
| chunkFilename: 'build/js/[name].bundle.js', | |
| } | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| mix | |
| .setPublicPath('public') | |
| .... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment