Added language switching drop-down menu.

This commit is contained in:
zuiidea
2018-10-15 15:08:48 +08:00
parent 66a6e55191
commit f6454e71f0
9 changed files with 209 additions and 55 deletions
+56 -22
View File
@@ -1,19 +1,71 @@
import React, { PureComponent, Fragment } from 'react'
import PropTypes from 'prop-types'
import { Menu, Icon, Layout, Avatar } from 'antd'
import { Trans } from '@lingui/react'
import { Trans, withI18n } from '@lingui/react'
import { setLocale } from 'utils'
import classnames from 'classnames'
import config from 'config'
import styles from './Header.less'
const { SubMenu } = Menu
@withI18n()
class Header extends PureComponent {
handleClickMenu = e => {
e.key === 'SignOut' && this.props.onSignOut()
}
render() {
const { username, avatar, collapsed, onCollapseChange } = this.props
const { username, avatar, collapsed, onCollapseChange, i18n } = this.props
const rightContent = [
<Menu key="user" mode="horizontal" onClick={this.handleClickMenu}>
<SubMenu
title={
<Fragment>
<span style={{ color: '#999', marginRight: 4 }}>
<Trans>Hi,</Trans>
</span>
<span>{username}</span>
<Avatar style={{ marginLeft: 8 }} src={avatar} />
</Fragment>
}
>
<Menu.Item key="SignOut">
<Trans>Sign out</Trans>
</Menu.Item>
</SubMenu>
</Menu>,
]
if (config.i18n) {
const { languages } = config.i18n
const currentLanguage = languages.find(
item => item.key === i18n._language
)
rightContent.unshift(
<Menu
key="language"
selectedKeys={[currentLanguage.key]}
onClick={data => {
setLocale(data.key)
}}
mode="horizontal"
>
<SubMenu title={<Avatar size="small" src={currentLanguage.flag} />}>
{languages.map(item => (
<Menu.Item key={item.key}>
<Avatar
size="small"
style={{ marginRight: 8 }}
src={item.flag}
/>
{item.title}
</Menu.Item>
))}
</SubMenu>
</Menu>
)
}
return (
<Layout.Header className={styles.header}>
<div
@@ -28,25 +80,7 @@ class Header extends PureComponent {
/>
</div>
<div className={styles.rightContainer}>
<Menu mode="horizontal" onClick={this.handleClickMenu}>
<SubMenu
title={
<Fragment>
<span style={{ color: '#999', marginRight: 4 }}>
<Trans>Hi,</Trans>
</span>
<span>{username}</span>
<Avatar style={{ marginLeft: 8 }} src={avatar} />
</Fragment>
}
>
<Menu.Item key="SignOut">
<Trans>Sign out</Trans>
</Menu.Item>
</SubMenu>
</Menu>
</div>
<div className={styles.rightContainer}>{rightContent}</div>
</Layout.Header>
)
}
+1 -1
View File
@@ -123,7 +123,7 @@ class PrimaryLayout extends PureComponent {
</Content>
<BackTop
className={styles.backTop}
target={() => document.getElementById('primaryLayout')}
target={() => document.querySelector('#primaryLayout>div')}
/>
<GlobalFooter
className={styles.footer}
+19 -10
View File
@@ -1,3 +1,5 @@
@import '~themes/vars.less';
@media (max-width: 767px) {
.content {
padding: 12px;
@@ -11,6 +13,19 @@
.backTop {
right: 50px;
:global {
.ant-back-top-content {
background: @primary-color;
opacity: 0.3;
transition: all 0.3s;
box-shadow: 0 0 15px 1px rgba(69, 65, 78, 0.1);
&:hover {
opacity: 1;
}
}
}
}
.content {
@@ -26,14 +41,8 @@
.footer {
background: #fff;
}
:global {
.antd-pro-global-footer-globalFooter {
margin-top: 0;
margin-bottom: 0;
padding-top: 24px;
padding-bottom: 24px;
background: #fff;
}
margin-top: 0;
margin-bottom: 0;
padding-top: 24px;
padding-bottom: 24px;
}
+14 -19
View File
@@ -2,9 +2,9 @@ import React, { PureComponent, Fragment } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'dva'
import { Button, Row, Form, Icon, Input } from 'antd'
import router from 'umi/router'
import { GlobalFooter } from 'ant-design-pro'
import { Trans, withI18n } from '@lingui/react'
import { setLocale } from 'utils'
import config from 'utils/config'
import styles from './index.less'
@@ -26,34 +26,29 @@ class Login extends PureComponent {
}
render() {
const { loading, form, i18n, location } = this.props
const { loading, form, i18n } = this.props
const { getFieldDecorator } = form
const footerLinks = [
let footerLinks = [
{
key: 'github',
title: <Icon type="github" />,
href: 'https://github.com/zuiidea/antd-admin',
blankTarget: true,
},
{
key: 'English',
title: (
<span onClick={() => router.push(`/en/login${location.search}`)}>
English
</span>
),
},
{
key: 'Chinese',
title: (
<span onClick={() => router.push(`/zh/login${location.search}`)}>
中文
</span>
),
},
]
if (config.i18n) {
footerLinks = footerLinks.concat(
config.i18n.languages.map(item => ({
key: item.key,
title: (
<span onClick={setLocale.bind(null, item.key)}>{item.title}</span>
),
}))
)
}
return (
<Fragment>
<div className={styles.form}>
+13 -1
View File
@@ -18,7 +18,19 @@ module.exports = {
/* I18n configuration, `languages` and `defaultLanguage` are required currently. */
i18n: {
languages: ['en', 'zh'],
/* Countrys flags: https://www.flaticon.com/packs/countrys-flags */
languages: [
{
key: 'en',
title: 'English',
flag: '/america.svg',
},
{
key: 'zh',
title: '中文',
flag: '/china.svg',
},
],
defaultLanguage: 'en',
},
}
+16 -1
View File
@@ -8,7 +8,8 @@ export config from './config'
export request from './request'
export { Color } from './theme'
export const { languages, defaultLanguage } = i18n
export const { defaultLanguage } = i18n
export const languages = i18n.languages.map(item => item.key)
/**
* Query objects that specify keys and values in an array where all values are objects.
@@ -237,3 +238,17 @@ export function queryLayout(layouts, pathname) {
return result
}
export function getLocale() {
return langFromPath(window.location.pathname)
}
export function setLocale(language) {
if (getLocale() !== language) {
umiRouter.push({
pathname: `/${language}${deLangPrefix(window.location.pathname)}${
window.location.search
}`,
})
}
}