Skip to content

Commit 396eb03

Browse files
glitzmahappy760690pany-ang
authored
feat: support i18n (#5)
Co-authored-by: glitzma <glitteringma@gmail.com> Co-authored-by: pany <939630029@qq.com>
1 parent 0cd94e2 commit 396eb03

File tree

7 files changed

+113
-9
lines changed

7 files changed

+113
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"vant": "4.9.17",
2727
"vconsole": "3.15.1",
2828
"vue": "3.5.13",
29+
"vue-i18n": "11.1.1",
2930
"vue-router": "4.5.0"
3031
},
3132
"devDependencies": {

pnpm-lock.yaml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pages/demo/i18n.vue

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1-
<script setup lang="ts"></script>
1+
<script setup lang="ts">
2+
import { Locale } from "vant"
3+
import enUS from "vant/es/locale/lang/en-US"
4+
import { useI18n } from "vue-i18n"
5+
import NoticeBar from "./components/NoticeBar.vue"
6+
7+
const { t, locale } = useI18n()
8+
9+
const checked = ref<string>(locale.value)
10+
11+
function onChange(name: string) {
12+
locale.value = name
13+
name === "en" ? Locale.use("en-US", enUS) : Locale.use("zh-CN")
14+
}
15+
</script>
216

317
<template>
4-
<van-empty description="作者正在努力,点个 Star 为作者加速!" />
18+
<div un-mb-20px>
19+
<NoticeBar :text="t('text')" />
20+
<van-radio-group v-model="checked" @change="onChange">
21+
<van-cell-group :title="t('title')" inset>
22+
<van-cell title="中文" @click="checked = 'zh-CN'">
23+
<template #right-icon>
24+
<van-radio name="zh-CN" />
25+
</template>
26+
</van-cell>
27+
<van-cell title="English" @click="checked = 'en'">
28+
<template #right-icon>
29+
<van-radio name="en" />
30+
</template>
31+
</van-cell>
32+
</van-cell-group>
33+
</van-radio-group>
34+
<van-calendar
35+
:poppable="false"
36+
:show-confirm="false"
37+
un-h-500px
38+
un-mx-16px
39+
un-my-20px
40+
un-rounded-8px
41+
un-overflow-hidden
42+
/>
43+
</div>
544
</template>

src/plugins/i18n.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { App } from "vue"
2+
import { createI18n } from "vue-i18n"
3+
4+
const i18n = createI18n({
5+
locale: "zh-CN",
6+
fallbackLocale: "zh-CN",
7+
messages: {
8+
"zh-CN": {
9+
text: "现在语言环境为中文",
10+
title: "切换语言"
11+
},
12+
"en": {
13+
text: "The current language is English",
14+
title: "Switch language"
15+
}
16+
}
17+
})
18+
19+
export function installI18n(app: App) {
20+
app.use(i18n)
21+
}

src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { App } from "vue"
22
import { installConsole } from "./console"
3+
import { installI18n } from "./i18n"
34
import { installPermissionDirective } from "./permission-directive"
45

56
export function installPlugins(app: App) {
67
installPermissionDirective(app)
78
installConsole()
9+
installI18n(app)
810
}

src/router/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,25 +143,25 @@ export const demoRoutes: RouteRecordRaw[] = [
143143
}
144144
}
145145
}
146-
}
147-
]
148-
149-
/** 空示例页面 */
150-
export const emptyDemoRoutes: RouteRecordRaw[] = [
146+
},
151147
{
152148
path: "/i18n",
153149
component: () => import("@/pages/demo/i18n.vue"),
154150
name: "I18n",
155151
meta: {
156-
title: "国际化 / 多语言",
152+
title: "国际化 / i18n",
157153
layout: {
158154
navBar: {
159155
showNavBar: true,
160156
showLeftArrow: true
161157
}
162158
}
163159
}
164-
},
160+
}
161+
]
162+
163+
/** 空示例页面 */
164+
export const emptyDemoRoutes: RouteRecordRaw[] = [
165165
{
166166
path: "/markdown",
167167
component: () => import("@/pages/demo/markdown.vue"),

types/auto/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare module 'vue' {
1111
RouterLink: typeof import('vue-router')['RouterLink']
1212
RouterView: typeof import('vue-router')['RouterView']
1313
VanButton: typeof import('vant/es')['Button']
14+
VanCalendar: typeof import('vant/es')['Calendar']
1415
VanCell: typeof import('vant/es')['Cell']
1516
VanCellGroup: typeof import('vant/es')['CellGroup']
1617
VanConfigProvider: typeof import('vant/es')['ConfigProvider']

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy