Skip to content

Commit 6aeebba

Browse files
author
cole
committed
feat: 多语言
1 parent 0ad972c commit 6aeebba

File tree

15 files changed

+88
-50
lines changed

15 files changed

+88
-50
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getCurrentInstance } from 'vue'
2+
3+
function useGlobalProperties () {
4+
const instance = getCurrentInstance()
5+
if (!instance) {
6+
throw new Error(`no instance found`)
7+
}
8+
return instance.appContext.config.globalProperties
9+
}
10+
11+
export default useGlobalProperties

packages/form/layouts/drawer-form/index.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export default defineComponent({
7575
...attrs,
7676
...pick(props, Object.keys(Drawer.props)),
7777
...extraProps,
78-
// headerStyle: { paddingBlock: '14px' },
7978
open: unref(sOpen),
8079
onClose: onCancel,
8180
onAfterOpenChange: onAfterClose

packages/table/components/density/index.jsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { defineComponent, unref } from 'vue'
2-
import { Menu } from 'ant-design-vue'
2+
import { Menu, theme } from 'ant-design-vue'
33
import { useSharedContext } from '../../hooks/useSharedContext'
44
import { useLocaleReceiver } from '../../../locale-provider'
55

66
export default defineComponent({
77
inheritAttrs: false,
88
props: { ...Menu.props },
99
setup (props, { attrs }) {
10+
const { token } = theme.useToken()
11+
1012
const { t } = useLocaleReceiver(['Table', 'toolbar'])
1113
const { tableSize, setTableSize } = useSharedContext()
1214

@@ -18,10 +20,12 @@ export default defineComponent({
1820
}
1921

2022
return () => {
23+
const { fontSize } = unref(token)
24+
2125
const menuProps = {
2226
...attrs,
2327
...props,
24-
style: { width: '88px' },
28+
style: { minWidth: `${fontSize * 7}px` },
2529
selectedKeys: [unref(tableSize)],
2630
onClick: onMenuClick
2731
}

src/App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export default defineComponent({
3131
})
3232

3333
return () => {
34-
const { antd, comps } = unref(localeMessage)
34+
const { antd, packages } = unref(localeMessage)
3535
const { darkAlgorithm, compactAlgorithm } = theme
3636

3737
return (
3838
<ConfigProvider theme={{ algorithm: [] }} locale={antd}>
39-
<LocaleProvider locale={comps}>
39+
<LocaleProvider locale={packages}>
4040
<RouterView/>
4141
</LocaleProvider>
4242
</ConfigProvider>

src/config/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export const HOME_NAME = 'HomeIndex'
33
// 登录页 (路由的拦截)
44
export const LOGIN_NAME = 'Login'
55

6-
76
// iconfont 的在线地址 src/components/icon/IconFont.jsx
87
export const scriptUrl = ''
98

src/css/mixin.scss

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $font-size: 14px;
3737
// 圆角
3838
$border-radius: 4px;
3939

40-
// 设置宽高 $width $height=$width
40+
// 设置宽高 $width $height:$width
4141
@mixin size($width, $height: $width) {
4242
width: $width;
4343
height: $height;
@@ -65,12 +65,12 @@ $border-radius: 4px;
6565
// flex $justify-content: null, $align-items: null
6666
@mixin flex($align-items: null, $justify-content: null) {
6767
display: flex;
68-
@if ($justify-content != "" and $justify-content != null) {
69-
justify-content: $justify-content;
70-
}
7168
@if ($align-items != "" and $align-items != null) {
7269
align-items: $align-items;
7370
}
71+
@if ($justify-content != "" and $justify-content != null) {
72+
justify-content: $justify-content;
73+
}
7474
}
7575

7676
// 绝对定位 $position, $top: null, $right: null, $bottom: null, $left: null
@@ -90,7 +90,7 @@ $border-radius: 4px;
9090
}
9191
}
9292

93-
// 扩展点击区域
93+
// 扩展点击区域 $v: 10px
9494
@mixin extend-click($v: 10px) {
9595
position: relative;
9696
&:before {
@@ -105,7 +105,7 @@ $border-radius: 4px;
105105
-webkit-overflow-scrolling: touch;
106106
}
107107

108-
// translate 未知宽高 水平垂直居中
108+
// translate 未知宽高水平垂直居中 $direction: both
109109
@mixin center-translate($direction: both) {
110110
position: absolute;
111111
@if ($direction == both) {
@@ -124,7 +124,7 @@ $border-radius: 4px;
124124
}
125125
}
126126

127-
// 多行超出设置点点点
127+
// 多行超出设置点点点 $v: 1
128128
@mixin box-clamp($v: 1) {
129129
overflow: hidden;
130130
display: -webkit-box;
@@ -140,7 +140,7 @@ $border-radius: 4px;
140140
overflow: hidden;
141141
}
142142

143-
// 圆形
143+
// 圆形 $size: 12px
144144
@mixin circle($size: 12px) {
145145
border-radius: 50%;
146146
width: $size;

src/layout/compatible/tags/index.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { CloseCircleOutlined, LeftOutlined, RightOutlined } from '@ant-design/ic
44
import TagNode from './Node'
55
import useShowTitle from '../../hooks/useShowTitle'
66
import { omitNil } from '@utils/util'
7-
import { isString } from 'lodash-es'
7+
import useGlobalProperties from '@utils/hooks/useGlobalProperties'
88
import { useConfigInject } from '@utils/extend'
99
import useStyle from './style'
10+
import { isString } from 'lodash-es'
1011

1112
export default defineComponent({
1213
props: {
@@ -36,6 +37,7 @@ export default defineComponent({
3637
const { prefixCls } = useConfigInject('pro-layout-tags', props)
3738
const [wrapSSR, hashId] = useStyle(prefixCls)
3839
const { token } = theme.useToken()
40+
const { $t } = useGlobalProperties()
3941

4042
const popupContainer = ref(null)
4143
let tagRefsMap = {}
@@ -194,12 +196,12 @@ export default defineComponent({
194196
const dropdownSlots = {
195197
overlay: () => {
196198
return (
197-
<Menu selectedKeys={[]}>
199+
<Menu class={`${prefixCls.value}-menu`} selectedKeys={[]}>
198200
<Menu.Item onClick={onMenuClose.bind(null, 'all')}>
199-
关闭所有
201+
{$t ? $t('layout.tags.closeAll') : '关闭所有'}
200202
</Menu.Item>
201203
<Menu.Item onClick={onMenuClose.bind(null, 'others')}>
202-
关闭其他
204+
{$t ? $t('layout.tags.closeOthers') : '关闭其他'}
203205
</Menu.Item>
204206
</Menu>
205207
)

src/layout/compatible/tags/style/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { genComponentStyleHook, mergeToken } from '@utils/extend'
22

33
function genBaseStyle (token) {
4-
const { componentCls, iconCls, tagsHeight, tagsButtonWidth, tagsCloseRight } = token
4+
const { componentCls, iconCls, tagsHeight, tagsButtonWidth, tagsCloseRight, tagsMenuMinWidth } = token
55
return {
66
[componentCls]: {
77
position: 'relative',
@@ -76,6 +76,10 @@ function genBaseStyle (token) {
7676
transition: 'left .3s ease'
7777
}
7878
}
79+
},
80+
[`${componentCls}-menu`]: {
81+
minWidth: tagsMenuMinWidth,
82+
whiteSpace: 'nowrap'
7983
}
8084
}
8185
}
@@ -85,11 +89,13 @@ export default genComponentStyleHook('ProLayoutTags', (token) => {
8589
const tagsHeight = token.controlHeight + token.sizeXXS * 2
8690
const tagsButtonWidth = Math.ceil(tagsHeight * 0.65)
8791
const tagsCloseRight = 6
92+
const tagsMenuMinWidth = token.fontSize * 7
8893

8994
const tagsToken = mergeToken(token, {
9095
tagsHeight,
9196
tagsButtonWidth,
92-
tagsCloseRight
97+
tagsCloseRight,
98+
tagsMenuMinWidth
9399
})
94100
return [genBaseStyle(tagsToken)]
95101
})

src/layout/components/avatar/index.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineComponent, unref } from 'vue'
22
import { Avatar, Dropdown, Menu, theme } from 'ant-design-vue'
33
import { LoginOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons-vue'
44
import { useAppInstance } from '@/useAppInstance'
5+
import useGlobalProperties from '@utils/hooks/useGlobalProperties'
56
import { useConfigInject } from '@utils/extend'
67
import useStyle from './style'
78

@@ -11,6 +12,7 @@ export default defineComponent({
1112
const { prefixCls } = useConfigInject('pro-avatar', props)
1213
const [wrapSSR, hashId] = useStyle(prefixCls)
1314
const { token } = theme.useToken()
15+
const { $t } = useGlobalProperties()
1416

1517
const { onLogout } = useAppInstance()
1618

@@ -33,21 +35,21 @@ export default defineComponent({
3335
key={'center'}
3436
v-slots={{ icon: () => <UserOutlined/> }}
3537
>
36-
个人中心
38+
{$t ? $t('layout.navbar.profile') : '个人中心'}
3739
</Menu.Item>
3840
<Menu.Item
3941
key={'settings'}
4042
v-slots={{ icon: () => <SettingOutlined/> }}
4143
>
42-
个人设置
44+
{$t ? $t('layout.navbar.settings') : '个人设置'}
4345
</Menu.Item>
4446
<Menu.Divider/>
4547
<Menu.Item
4648
key={'logout'}
4749
onClick={handleLogout}
4850
v-slots={{ icon: () => <LoginOutlined/> }}
4951
>
50-
退出登录
52+
{$t ? $t('layout.navbar.logout') : '退出登录'}
5153
</Menu.Item>
5254
</Menu>
5355
)

src/layout/components/language/index.jsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
1-
import { defineComponent, getCurrentInstance } from 'vue'
1+
import { defineComponent } from 'vue'
22
import { Dropdown, Menu } from 'ant-design-vue'
33
import { GlobalOutlined } from '@ant-design/icons-vue'
44
import { useAppInstance } from '@/useAppInstance'
5-
import { map } from 'lodash-es'
65
import { localCache, LOCALE__LOCAL } from '@/utils/storage'
6+
import useGlobalProperties from '@utils/hooks/useGlobalProperties'
77
import { useConfigInject } from '@utils/extend'
88
import useStyle from './style'
9+
import { map } from 'lodash-es'
910

1011
export default defineComponent({
1112
inheritAttrs: false,
1213
setup (props, { attrs }) {
1314
const { prefixCls } = useConfigInject('pro-language', props)
1415
const [wrapSSR, hashId] = useStyle(prefixCls)
1516

16-
const { setLocale } = useAppInstance()
1717
// 需要修改为不需要依赖 useI18n 的方式
1818
// 并且不干涉 layout 的逻辑
19-
const { appContext } = getCurrentInstance()
20-
const { globalProperties } = appContext ? appContext.config : {}
21-
const { $i18n = { locale: 'zh-CN' } } = globalProperties || {}
19+
const { $i18n = { locale: 'zh-CN' } } = useGlobalProperties()
20+
const { setLocale } = useAppInstance()
2221

2322
const language = navigator.language
2423
const localeLang = (language === 'zh-CN' || language === 'en-US') ? language : false
2524
const lang = localCache.get(LOCALE__LOCAL) || localeLang || 'zh-CN'
2625

27-
const langList = {
28-
'zh-CN': '语言',
29-
'en-US': 'Lang'
30-
}
31-
3226
const localeList = {
3327
'zh-CN': '中文简体',
3428
'en-US': 'English'

src/layout/hooks/useShowTitle.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
import { getCurrentInstance } from 'vue'
2-
import { isFunction } from 'lodash-es'
1+
import useGlobalProperties from '@utils/hooks/useGlobalProperties'
32

43
function useShowTitle () {
5-
/**
6-
* 判断有没有安装 i18n
7-
* 即使不使用 i18n ,也不用修改这部分代码了
8-
*/
9-
const { appContext } = getCurrentInstance()
10-
const { globalProperties } = appContext ? appContext.config : {}
11-
const { $t, $te } = globalProperties || {}
4+
const { $t, $te } = useGlobalProperties()
125

136
const prefix = 'routes.'
147

158
function showTitle (route) {
169
const { title } = route.meta || {}
17-
if (isFunction($t) && isFunction($te) && route.name) {
10+
if ($t && $te && route.name) {
1811
const key = prefix + route.name
1912
return $te(key) ? $t(key) : (title || route.name)
2013
}

src/locale/en-US/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import antd from 'ant-design-vue/es/locale/en_US'
2+
import packages from '@packages/locale-provider/lang/en-US'
23
import dayjs from 'dayjs/locale/en'
3-
import comps from '@packages/locale-provider/lang/en-US'
4+
// --
45
import routes from './routes'
6+
import layout from './layout'
57

68
export default {
79
locale: 'en-US',
810
antd: antd,
11+
packages: packages,
912
dayjs: dayjs,
10-
comps: comps,
11-
routes: routes
13+
routes: routes,
14+
layout: layout
1215
}

src/locale/en-US/layout.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
navbar: {
3+
profile: 'Profile',
4+
settings: 'Settings',
5+
logout: 'Sign out'
6+
},
7+
tags: {
8+
closeAll: 'Close All',
9+
closeOthers: 'Close Other'
10+
}
11+
}

src/locale/zh-CN/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import antd from 'ant-design-vue/es/locale/zh_CN'
2+
import packages from '@packages/locale-provider/lang/zh-CN'
23
import dayjs from 'dayjs/locale/zh-cn'
3-
import comps from '@packages/locale-provider/lang/zh-CN'
4+
// --
45
import routes from './routes'
6+
import layout from './layout'
57

68
export default {
79
locale: 'zh-CN',
810
antd: antd,
11+
packages: packages,
912
dayjs: dayjs,
10-
comps: comps,
11-
routes: routes
13+
routes: routes,
14+
layout: layout
1215
}

src/locale/zh-CN/layout.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
navbar: {
3+
profile: '个人中心',
4+
settings: '个人设置',
5+
logout: '退出登录'
6+
},
7+
tags: {
8+
closeAll: '关闭所有',
9+
closeOthers: '关闭其他'
10+
}
11+
}

0 commit comments

Comments
 (0)