Skip to content

Commit

Permalink
chore: 增加i18n支持
Browse files Browse the repository at this point in the history
  • Loading branch information
sdmu-gaoqi committed Jan 19, 2024
1 parent b108da0 commit b5da694
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 7 deletions.
7 changes: 6 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"recommendations": ["Vue.volar", "Vue.vscode-typescript-vue-plugin"]
"recommendations": [
"Vue.volar",
"Vue.vscode-typescript-vue-plugin",
"wa.wa-xlsxToTs",
"wa.wa-menus"
]
}
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,12 @@ export type WAMenu = (ItemType & {
package里有不同环境的打包方式
## 关于mock
mock预设为 test 与 prod(根据import.meta.env.MODE 决定 prod模式时 使用的是prod文件下的mock 其他的都是用的test下的mock)
需要增加更多环境支持可以在 mock/index.ts修改
需要增加更多环境支持可以在 mock/index.ts修改
## 关于插件
内设了几款插件
wa-menu 右键 src/locales/lang.xlsx可以 执行xlsx自动转ts的脚本
wa-language-tip 丰富i18n 在编写时的提示能力
10 changes: 10 additions & 0 deletions mock/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ const testMock: MockMethod = [
data: ['perm-1']
}
}
},
{
url: '/api/logout',
method: 'get',
timeout: 1000,
response: () => {
return {
code: 0
}
}
}
]

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
"config": {
"commitizen": {
"path": "node_modules/cz-git"
},
"wa-menus": {
"xlsxDefaultLan": "zh-CN",
"xlsxTransformPath": "",
"xlsxTransformType": ""
}
},
"dependencies": {
Expand All @@ -63,6 +68,7 @@
"vite-plugin-mkcert": "^1.16.0",
"vue": "^3.3.4",
"vue-hooks-plus": "^1.8.6",
"vue-i18n": "^9.9.0",
"vue-router": "^4.2.4",
"vuex": "^4.1.0",
"wa-utils": "^0.2.6"
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<script setup lang="ts">
import zhCN from 'ant-design-vue/es/locale/zh_CN'
// DatePicker ui组件配置中文依然是英文展示
// @ts-ignore
zhCN.DatePicker.lang = {
...zhCN.DatePicker?.lang,
Expand Down
18 changes: 16 additions & 2 deletions src/components/layout/layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@
@click="(v: any) => handleChangeApp(v?.key)"
v-if="appList.length > 1"
></a-menu>
<div class="ml-auto w-[50px]">
<div class="ml-auto w-[100px]">
<a-dropdown>
<template #overlay>
<a-menu @click="(v) => changeLang(v.key)">
<a-menu-item :key="i.value" v-for="i in langMap">
<a>{{ i.label }}</a>
</a-menu-item>
</a-menu>
</template>
<a>{{ locale }}</a>
</a-dropdown>
<a-dropdown>
<template #overlay>
<a-menu @click="systemLogout">
<a-menu-item key="1"> 退出登录 </a-menu-item>
</a-menu>
</template>
<Avatar class="avatar"></Avatar
<a><Avatar class="avatar ml-[10px]"></Avatar></a
></a-dropdown>
</div>
</a-layout-header>
Expand Down Expand Up @@ -93,7 +103,11 @@ import { computed } from 'vue'
import { RouterView } from 'vue-router'
import { Avatar } from 'ant-design-vue'
import { systemLogout } from '@/utils'
import { useI18n } from 'vue-i18n'
import { langMap } from '@/constant'
import { changeLang } from '@/utils/lang'
const store = useStore()
const { locale } = useI18n()
const appList = computed(() => {
return store.state.common.apps?.map((item) => ({
Expand Down
11 changes: 11 additions & 0 deletions src/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ export const adminPerm = '*:*:*'

export const mode = import.meta.env.MODE as unknown as Env
export const env = import.meta.env

export const langMap = [
{
label: '中文',
value: 'zh-CN'
},
{
label: 'english',
value: 'en'
}
]
4 changes: 4 additions & 0 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
message: "hello",
test: "测试",
}
20 changes: 20 additions & 0 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// import { globSync } from 'glob'
import zhCN from './zh-CN'
import en from './en'
import { createI18n } from 'vue-i18n'
import { Lang } from '@/types'
import { local } from '@/utils/storage'

const getInitLang = (): Lang => {
return local.baseGet('lang') || 'zh-CN'
}

export const setupI18n = createI18n({
legacy: false,
locale: getInitLang(),
globalInjection: true,
messages: {
'zh-CN': zhCN,
en
}
})
Binary file added src/locales/lang.xlsx
Binary file not shown.
4 changes: 4 additions & 0 deletions src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
message: "你好",
test: "测试",
}
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import Antd from 'ant-design-vue'
import { store, key } from './store/store'
import './styles/global.scss'
import './styles/tailwind.global.scss'
import { setupI18n } from './locales'

document.title = ''

createApp(App).use(Antd).use(route).use(store, key).mount('#app')
createApp(App).use(setupI18n).use(Antd).use(route).use(store, key).mount('#app')
4 changes: 3 additions & 1 deletion src/pages/workbench/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { defineComponent } from 'vue'
import { useI18n } from 'vue-i18n'

const Workbench = defineComponent({
name: 'Workbench',
setup() {
const { t, locale } = useI18n()
return () => {
return <div>workbench</div>
return <div>{t('message')}</div>
}
}
})
Expand Down
7 changes: 7 additions & 0 deletions src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export const loginService = () => {
method: 'post'
})
}

export const logoutService = () => {
return request.request({
url: '/api/logout',
method: 'get'
})
}
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export type WAMenu = (ItemType & {
})[]

export type Env = 'development' | 'test' | 'staging' | 'production'

export type Lang = 'zh-CN' | 'en'
4 changes: 3 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logoutService } from '@/services/user'
import { local } from './storage'

export const isLogin = () => {
Expand All @@ -13,7 +14,8 @@ export const systemLogin = (token: string) => {
location.reload()
}

export const systemLogout = () => {
export const systemLogout = async () => {
await logoutService()
local.remove('token')
location.reload()
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/lang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Lang } from '@/types'
import { local } from './storage'

export const changeLang = (lang: Lang) => {
local.baseSet('lang', lang)
location.reload()
}

0 comments on commit b5da694

Please sign in to comment.