build: 设置多语言

This commit is contained in:
陈凯龙
2021-12-08 10:47:33 +08:00
parent 4050d56c19
commit 45e879edee
27 changed files with 502 additions and 1388 deletions

View File

@@ -1,17 +1,26 @@
<script setup lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import HelloWorld from './components/HelloWorld.vue'
</script>
<template>
<div>sss</div>
<div v:bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600"> sssss </div>
<img alt="Vue logo" src="./assets/logo.png" />
<div>sss</div>
<HelloWorld msg="Hello Vue 3 + TypeScript + Vite" />
<div>{{ t('test.about') }}</div>
<div>{{ t('test2.go') }}</div>
<button @click="change">切换语言</button>
</template>
<script setup lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import HelloWorld from './components/HelloWorld.vue'
const { t, availableLocales, locale } = useI18n()
function change() {
const locales = availableLocales
locale.value = locales[(locales.indexOf(locale.value) + 1) % locales.length]
}
</script>
<style scoped>
#app {
margin-top: 60px;

BIN
src/assets/img/404.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
src/assets/img/avatar.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

BIN
src/assets/img/avatar.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

BIN
src/assets/img/login-bg.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 450 KiB

BIN
src/assets/img/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

8
src/locales/en.ts Normal file
View File

@@ -0,0 +1,8 @@
export default {
test: {
about: 'About'
},
test2: {
go: 'Go'
}
}

8
src/locales/zh-CN.ts Normal file
View File

@@ -0,0 +1,8 @@
export default {
test: {
about: '关于'
},
test2: {
go: '去'
}
}

View File

@@ -1,8 +1,14 @@
import { createApp } from 'vue'
import App from './App.vue'
import 'virtual:windi.css'
const app = createApp(App)
import 'virtual:windi-devtools'
// 引入windi css
import '@/plugins/windicss'
createApp(App).mount('#app')
// 引入多语言
import { setupI18n } from '@/plugins/i18n'
setupI18n(app)
app.mount('#app')

17
src/plugins/i18n/index.ts Normal file
View File

@@ -0,0 +1,17 @@
import { createI18n } from 'vue-i18n'
import type { App } from 'vue'
const messages = Object.fromEntries(
Object.entries(import.meta.globEager('../../locales/*.ts')).map(([key, value]) => {
return [key.slice(14, -3), value.default]
})
)
export function setupI18n(app: App<Element>): void {
const i18n = createI18n({
legacy: false,
locale: 'zh-CN',
messages
})
app.use(i18n)
}

View File

@@ -0,0 +1,3 @@
import 'virtual:windi.css'
import 'virtual:windi-devtools'

View File

0
src/types/global.d.ts vendored Normal file
View File

0
src/types/v-components.d.ts vendored Normal file
View File