Skip to content

Commit 0cd5304

Browse files
authored
feat: Support vue i18n (#76)
1 parent 9785af5 commit 0cd5304

File tree

19 files changed

+310
-49
lines changed

19 files changed

+310
-49
lines changed

.vscode/extensions.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
{
2-
"recommendations": [
3-
"Vue.volar",
4-
"antfu.unocss",
5-
"simonhe.common-intellisense"
6-
]
7-
}
1+
{
2+
"recommendations": [
3+
"Vue.volar",
4+
"antfu.unocss",
5+
"simonhe.common-intellisense",
6+
"lokalise.i18n-ally"
7+
]
8+
}

.vscode/settings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,13 @@
4949
"common-intellisense.showSlots": false,
5050
"common-intellisense.ui": [
5151
"vant4"
52-
]
52+
],
53+
54+
// Configuration of i18n i18n-ally
55+
"i18n-ally.enabledParsers": ["json"],
56+
"i18n-ally.displayLanguage": "zh-CN",
57+
"i18n-ally.localesPaths": [
58+
"src/locales"
59+
],
60+
"i18n-ally.keystyle": "nested"
5361
}

build/vite/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { dirname, resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
13
import { unheadVueComposablesImports } from '@unhead/vue'
24
import legacy from '@vitejs/plugin-legacy'
35
import vue from '@vitejs/plugin-vue'
@@ -12,6 +14,7 @@ import { VitePWA } from 'vite-plugin-pwa'
1214
import Sitemap from 'vite-plugin-sitemap'
1315
import VueDevTools from 'vite-plugin-vue-devtools'
1416
import Layouts from 'vite-plugin-vue-layouts'
17+
import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
1518
import { createViteVConsole } from './vconsole'
1619

1720
export function createVitePlugins() {
@@ -56,6 +59,8 @@ export function createVitePlugins() {
5659
VueRouterAutoImports,
5760
{
5861
'vue-router/auto': ['useLink'],
62+
'@/utils/i18n': ['i18n', 'locale'],
63+
'vue-i18n': ['useI18n'],
5964
},
6065
unheadVueComposablesImports,
6166
],
@@ -65,6 +70,12 @@ export function createVitePlugins() {
6570
],
6671
}),
6772

73+
// https://github.com/intlify/bundle-tools/tree/main/packages/unplugin-vue-i18n
74+
VueI18nPlugin({
75+
// locale messages resource pre-compile option
76+
include: resolve(dirname(fileURLToPath(import.meta.url)), '../../src/locales/**'),
77+
}),
78+
6879
legacy({
6980
targets: ['defaults', 'not IE 11'],
7081
}),

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
"vant": "^4.8.5",
3333
"vconsole": "^3.15.1",
3434
"vue": "^3.4.21",
35+
"vue-i18n": "^9.10.2",
3536
"vue-router": "^4.3.0"
3637
},
3738
"devDependencies": {
3839
"@antfu/eslint-config": "2.8.0",
40+
"@intlify/unplugin-vue-i18n": "^4.0.0",
3941
"@types/lodash-es": "^4.17.12",
4042
"@types/node": "^20.11.25",
4143
"@types/nprogress": "^0.2.3",

pnpm-lock.yaml

Lines changed: 110 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auto-imports.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ declare global {
4646
const getCurrentInstance: typeof import('vue')['getCurrentInstance']
4747
const getCurrentScope: typeof import('vue')['getCurrentScope']
4848
const h: typeof import('vue')['h']
49+
const i18n: typeof import('@/utils/i18n')['i18n']
4950
const ignorableWatch: typeof import('@vueuse/core')['ignorableWatch']
5051
const inject: typeof import('vue')['inject']
5152
const injectHead: typeof import('@unhead/vue')['injectHead']
@@ -57,6 +58,7 @@ declare global {
5758
const isReadonly: typeof import('vue')['isReadonly']
5859
const isRef: typeof import('vue')['isRef']
5960
const it: typeof import('vitest')['it']
61+
const locale: typeof import('@/utils/i18n')['locale']
6062
const makeDestructurable: typeof import('@vueuse/core')['makeDestructurable']
6163
const markRaw: typeof import('vue')['markRaw']
6264
const nextTick: typeof import('vue')['nextTick']
@@ -193,6 +195,7 @@ declare global {
193195
const useGeolocation: typeof import('@vueuse/core')['useGeolocation']
194196
const useHead: typeof import('@unhead/vue')['useHead']
195197
const useHeadSafe: typeof import('@unhead/vue')['useHeadSafe']
198+
const useI18n: typeof import('vue-i18n')['useI18n']
196199
const useIdle: typeof import('@vueuse/core')['useIdle']
197200
const useImage: typeof import('@vueuse/core')['useImage']
198201
const useInfiniteScroll: typeof import('@vueuse/core')['useInfiniteScroll']

src/components.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ declare module 'vue' {
1616
VanCellGroup: typeof import('vant/es')['CellGroup']
1717
VanConfigProvider: typeof import('vant/es')['ConfigProvider']
1818
VanEmpty: typeof import('vant/es')['Empty']
19+
VanField: typeof import('vant/es')['Field']
1920
VanIcon: typeof import('vant/es')['Icon']
2021
VanNavBar: typeof import('vant/es')['NavBar']
22+
VanPicker: typeof import('vant/es')['Picker']
23+
VanPopup: typeof import('vant/es')['Popup']
24+
VanRadio: typeof import('vant/es')['Radio']
2125
VanSpace: typeof import('vant/es')['Space']
2226
VanSwitch: typeof import('vant/es')['Switch']
2327
}

src/components/NavBar.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ function onBack() {
99
router.replace('/')
1010
}
1111
12+
const { t } = useI18n()
13+
1214
const title = computed(() => {
13-
const { title } = (route.meta as { title?: string }) || {}
14-
return title || ''
15+
if (!route.meta || !route.meta.title)
16+
return ''
17+
return route.meta.i18n ? t(route.meta.i18n) : route.meta.title
1518
})
1619
</script>
1720

src/locales/en-US.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"home": {
3+
"darkMode": "🌗 Dark Mode",
4+
"mockGuide": "💿 Mock Guide",
5+
"language": "📚 Language",
6+
"404Demo": "🙅 Page 404 Demo",
7+
"echartsDemo": "📊 Echarts Demo",
8+
"persistPiniaState": "🍍 Persistent Pinia state",
9+
"unocssExample": "🎨 Unocss example"
10+
},
11+
"mock": {
12+
"fromAsyncData": "Data from asynchronous requests",
13+
"noData": "No data",
14+
"pull": "Pull",
15+
"reset": "Reset"
16+
}
17+
}

src/locales/zh-CN.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"home": {
3+
"darkMode": "🌗 暗黑模式",
4+
"mockGuide": "💿 Mock 指南",
5+
"language": "📚 语言",
6+
"echartsDemo": "📊 Echarts 演示",
7+
"persistPiniaState": "🍍 持久化 Pinia 状态",
8+
"404Demo": "🙅 404页 演示",
9+
"unocssExample": "🎨 Unocss 示例"
10+
},
11+
"mock": {
12+
"fromAsyncData": "来自异步请求的数据",
13+
"pull": "请求",
14+
"reset": "清空",
15+
"noData": "暂无数据"
16+
}
17+
}

0 commit comments

Comments
 (0)