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
+89 -19
View File
@@ -1,28 +1,98 @@
import Vue from 'vue';
import VueRouter from 'vue-router';
import decamelize from 'decamelize';
import { documents } from 'site-desktop-shared';
import { isMobile } from '../common';
import { config, documents } from 'site-desktop-shared';
import { getLang, setDefaultLang } from '../common/locales';
import '../common/iframe-router';
const routes = [];
const names = Object.keys(documents);
if (isMobile) {
location.replace('mobile.html' + location.hash);
}
routes.push({
path: '/home',
component: documents.Home
});
const { locales, defaultLang } = config.site;
routes.push({
path: '*',
redirect: '/home'
});
setDefaultLang(defaultLang);
names.forEach(name => {
routes.push({
name,
component: documents[name],
path: `/${decamelize(name, '-')}`,
meta: {
name
function parseName(name) {
if (name.indexOf('_') !== -1) {
const pairs = name.split('_');
const component = pairs.shift();
return {
component: `${decamelize(component, '-')}`,
lang: pairs.join('-')
};
}
return {
component: `${decamelize(name, '-')}`,
lang: ''
};
}
function getRoutes() {
const routes = [];
const names = Object.keys(documents);
if (locales) {
routes.push({
path: '*',
redirect: `/${getLang()}/`
});
} else {
routes.push({
path: '*',
redirect: '/'
});
}
function addHomeRoute(Home, lang) {
routes.push({
name: lang,
path: `/${lang || ''}`,
component: Home,
meta: { lang }
});
}
names.forEach(name => {
const { component, lang } = parseName(name);
if (component === 'home') {
addHomeRoute(documents[name], lang);
}
routes.push({
name: `${lang}/${component}`,
path: `/${lang}/${component}`,
component: documents[name],
meta: {
lang,
name: component
}
});
});
return routes;
}
Vue.use(VueRouter);
export const router = new VueRouter({
mode: 'hash',
routes: getRoutes(),
scrollBehavior(to) {
if (to.hash) {
return { selector: to.hash };
}
return { x: 0, y: 0 };
}
});
export { routes };
router.afterEach(() => {
Vue.nextTick(() => window.syncPath());
});
window.vueRouter = router;