Skip to content

Commit

Permalink
perf: 简化 i18n 模块逻辑,使用 defu 替换 lodash/merge
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Dec 23, 2023
1 parent 6cc8418 commit 71e792c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"c8": "^8.0.1",
"changelogen": "^0.5.5",
"cross-env": "^7.0.3",
"defu": "^6.1.3",
"echarts": "^5.4.3",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -56,7 +57,6 @@
"lightningcss": "^1.22.1",
"lint-staged": "^15.2.0",
"local-pkg": "^0.5.0",
"lodash": "^4.17.21",
"markdown-it-prism": "^2.3.0",
"mockjs": "^1.1.0",
"nprogress": "^0.2.0",
Expand Down
127 changes: 57 additions & 70 deletions pnpm-lock.yaml

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

26 changes: 13 additions & 13 deletions src/plugins/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { defu } from 'defu'
import { createI18n } from 'vue-i18n'
import _merge from 'lodash/merge'

const languages = Object.entries(
import.meta.glob('../../locales/*/**.y(a)?ml', {
eager: true,
}),
).map(([key, value]) => {
const yamls = import.meta.glob('../../locales/*/**.y(a)?ml', {
eager: true,
})

const languages = Object.entries(yamls).map(([key, value]) => {
const yaml = key.endsWith('.yaml')
key = key.slice(14, yaml ? -5 : -4)
// 如果有子模块,则分割
if (key.includes('/')) {
key = key.split('/')[0]
}
// @ts-ignore
return [key.slice(14, yaml ? -5 : -4), value.default]
return { [key]: value.default }
})

const messages = {}

for (const [key, value] of languages) {
const mergerKey = key.split('/')[0]
_merge(messages, { [`${mergerKey}`]: value })
}
const messages = defu({}, ...languages)

// localStorage 中的 locale,第二个参数为默认值
// https://vueuse.org/core/useStorage/#usestorage
Expand Down

0 comments on commit 71e792c

Please sign in to comment.