From 0b17931c62e9071827790d5c0e4dae56b7c74263 Mon Sep 17 00:00:00 2001 From: zuiidea Date: Tue, 16 Oct 2018 16:20:48 +0800 Subject: [PATCH] Menu supports multiple languages. --- mock/route.js | 16 ++++++++++++++-- src/components/Layout/Sider.less | 2 +- src/layouts/PrimaryLayout.js | 20 ++++++++++++++++---- src/locales/en/messages.json | 2 +- src/models/app.js | 6 ++++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/mock/route.js b/mock/route.js index 00535ba..1a6e00f 100644 --- a/mock/route.js +++ b/mock/route.js @@ -6,12 +6,14 @@ const database = [ id: '1', icon: 'dashboard', name: 'Dashboard', + zhName: '仪表盘', route: '/dashboard', }, { id: '2', breadcrumbParentId: '1', name: 'Users', + zhName: '用户管理', icon: 'user', route: '/user', }, @@ -19,6 +21,7 @@ const database = [ id: '7', breadcrumbParentId: '1', name: 'Posts', + zhName: '文章管理', icon: 'shopping-cart', route: '/post', }, @@ -27,12 +30,14 @@ const database = [ menuParentId: '-1', breadcrumbParentId: '2', name: 'User Detail', + zhName: '用户详情', route: '/user/:id', }, { id: '3', breadcrumbParentId: '1', name: 'Request', + zhName: 'Request', icon: 'api', route: '/request', }, @@ -40,6 +45,7 @@ const database = [ id: '4', breadcrumbParentId: '1', name: 'UI Element', + zhName: 'UI组件', icon: 'camera-o', }, { @@ -47,6 +53,7 @@ const database = [ breadcrumbParentId: '4', menuParentId: '4', name: 'DropOption', + zhName: 'DropOption', icon: 'bars', route: '/UIElement/dropOption', }, @@ -55,6 +62,7 @@ const database = [ breadcrumbParentId: '4', menuParentId: '4', name: 'Editor', + zhName: 'Editor', icon: 'edit', route: '/UIElement/editor', }, @@ -62,6 +70,7 @@ const database = [ id: '5', breadcrumbParentId: '1', name: 'Charts', + zhName: 'Charts', icon: 'code-o', }, { @@ -69,6 +78,7 @@ const database = [ breadcrumbParentId: '5', menuParentId: '5', name: 'ECharts', + zhName: 'ECharts', icon: 'line-chart', route: '/chart/ECharts', }, @@ -76,7 +86,8 @@ const database = [ id: '52', breadcrumbParentId: '5', menuParentId: '5', - name: 'highCharts', + name: 'HighCharts', + zhName: 'HighCharts', icon: 'bar-chart', route: '/chart/highCharts', }, @@ -85,9 +96,10 @@ const database = [ breadcrumbParentId: '5', menuParentId: '5', name: 'Rechartst', + zhName: 'Rechartst', icon: 'area-chart', route: '/chart/Recharts', - } + }, ] module.exports = { diff --git a/src/components/Layout/Sider.less b/src/components/Layout/Sider.less index 52b23c9..dce326d 100644 --- a/src/components/Layout/Sider.less +++ b/src/components/Layout/Sider.less @@ -17,7 +17,7 @@ height: 72px; display: flex; align-items: center; - justify-content: space-between; + justify-content: center; padding: 0 24px; box-shadow: 0 1px 9px -3px rgba(0, 0, 0, 0.2); .logo { diff --git a/src/layouts/PrimaryLayout.js b/src/layouts/PrimaryLayout.js index 5cee61b..0afe49b 100644 --- a/src/layouts/PrimaryLayout.js +++ b/src/layouts/PrimaryLayout.js @@ -8,7 +8,7 @@ import { MyLayout, ScrollBar } from 'components' import { BackTop, Layout, Drawer } from 'antd' import { GlobalFooter } from 'ant-design-pro' import { enquireScreen, unenquireScreen } from 'enquire-js' -import { config, pathMatchRegexp } from 'utils' +import { config, pathMatchRegexp, langFromPath } from 'utils' import Error from '../pages/404' import styles from './PrimaryLayout.less' @@ -57,8 +57,20 @@ class PrimaryLayout extends PureComponent { const { isMobile } = this.state const { onCollapseChange } = this + // Localized route name. + const newRouteList = + langFromPath(location.pathname) === 'zh' + ? routeList.map(item => { + const { zhName, ...other } = item + return { + ...other, + name: zhName, + } + }) + : routeList + // Find a route that matches the pathname. - const currentRoute = routeList.find( + const currentRoute = newRouteList.find( _ => _.route && pathMatchRegexp(_.route, location.pathname) ) @@ -68,7 +80,7 @@ class PrimaryLayout extends PureComponent { : false // MenuParentId is equal to -1 is not a available menu. - const menus = routeList.filter(_ => _.menuParentId !== '-1') + const menus = newRouteList.filter(_ => _.menuParentId !== '-1') const headerProps = { menus, @@ -129,7 +141,7 @@ class PrimaryLayout extends PureComponent { >
- + {hasPermission ? children : }