feat(cli): support locales

This commit is contained in:
陈嘉涵
2019-12-06 15:51:20 +08:00
parent 6bd858fac5
commit dc6cc6c5af
16 changed files with 336 additions and 135 deletions

View File

@@ -1,29 +1,86 @@
import Vue from 'vue';
import VueRouter from 'vue-router';
import decamelize from 'decamelize';
import DemoHome from './components/DemoHome';
import { demos } from 'site-mobile-shared';
import { demos, config } from 'site-mobile-shared';
import { getLang, setDefaultLang } from '../common/locales';
import '../common/iframe-router';
const routes = [];
const names = Object.keys(demos);
const { locales, defaultLang } = config.site;
routes.push({
path: '/home',
component: DemoHome
});
setDefaultLang(defaultLang);
routes.push({
path: '*',
redirect: '/home'
});
function getRoutes() {
const routes = [];
const names = Object.keys(demos);
const langs = locales ? Object.keys(locales) : [];
names.forEach(name => {
routes.push({
name,
component: demos[name],
path: `/${decamelize(name, '-')}`,
meta: {
name
if (langs.length) {
routes.push({
path: '*',
redirect: () => `/${getLang()}/`
});
langs.forEach(lang => {
routes.push({
path: `/${lang}`,
component: DemoHome,
meta: { lang }
});
});
} else {
routes.push({
path: '*',
redirect: () => '/'
});
routes.push({
path: '',
component: DemoHome
});
}
names.forEach(name => {
const component = decamelize(name, '-');
if (langs.length) {
langs.forEach(lang => {
routes.push({
name: `${lang}/${component}`,
path: `/${lang}/${component}`,
component: demos[name],
meta: {
name
}
});
});
} else {
routes.push({
name,
path: `/${component}`,
component: demos[name],
meta: {
name
}
});
}
});
return routes;
}
Vue.use(VueRouter);
export const router = new VueRouter({
mode: 'hash',
routes: getRoutes(),
scrollBehavior: (to, from, savedPosition) => savedPosition || { x: 0, y: 0 }
});
export { routes };
router.afterEach(() => {
if (!router.currentRoute.redirectedFrom) {
Vue.nextTick(window.syncPath);
}
});
window.vueRouter = router;