vue3+ts+vite+pinia+element?plus項(xiàng)目使用語(yǔ)言國(guó)際化詳解
vue3+ts+vite+pinia+element plus項(xiàng)目使用語(yǔ)言國(guó)際化
1、安裝vue-i18n@9
npm install vue-i18n@9
2、創(chuàng)建語(yǔ)言文件en.json和zh-CN.json,路徑在src/i18n/
en.json和zh-CN.json文件內(nèi)容要一致,以便在切換語(yǔ)言時(shí)生效,需要傳入的內(nèi)容可用{}
en.json內(nèi)容:
{
"common": {
"login": "Login",
"welcome": "Welcome, {name}!",
"button": {
"confirm": "Confirm",
"cancel": "Cancel"
}
},
"author": {
"name": "name"
}
}
zh-CN.json內(nèi)容
{
"common": {
"login": "登錄",
"welcome": "歡迎,{name}!",
"button": {
"confirm": "確定",
"cancel": "取消"
}
},
"author": {
"name": "姓名"
}
}
3、創(chuàng)建 i18n 實(shí)例
src/i18n/index.ts
import { createI18n } from 'vue-i18n'
import en from './en.json'
import zhCN from './zh-CN.json'
type MessageSchema = typeof en
const messages = {
en,
'zh-CN': zhCN
}
export const i18n = createI18n<[MessageSchema], 'en' | 'zh-CN'>({
legacy: false, // 使用 Composition API 模式
locale: 'en', // 默認(rèn)語(yǔ)言
fallbackLocale: 'en', // 回退語(yǔ)言
globalInjection: true, // 全局注入 $t
messages
})
// 導(dǎo)出類型增強(qiáng)
export type I18nType = typeof i18n
4、 全局引入,修改 main.ts
import { createApp } from 'vue'
import App from './App.vue'
import { i18n } from './i18n'
import { createPinia } from 'pinia'
const app = createApp(App)
const pinia = createPinia()
app.use(i18n)
app.use(pinia)
app.mount('#app')
5、類型聲明(可選)
src/shims-vue-i18n.d.ts
import { I18nType } from './i18n'
declare module 'vue-i18n' {
export interface Composer extends I18nType.global.Composer {}
}
6、創(chuàng)建語(yǔ)言切換組件
路徑:src/components/LangSwitcher/index.vue
<template>
<select v-model="locale">
<option value="en">English</option>
<option value="zh-CN">中文</option>
</select>
</template>
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
const { locale } = useI18n()
</script>
7、創(chuàng)建動(dòng)態(tài)加載語(yǔ)言文件
路徑: src/i18n/loader.ts
import { i18n } from "@/i18n/index";
import { nextTick } from "vue";
export async function loadLocaleMessages(locale: string) {
try {
const messages = await import(`./locales/${locale}.json`)
i18n.global.setLocaleMessage(locale, messages.default)
return nextTick()
} catch (error) {
console.error('Failed to load locale:', error)
}
}
在切換語(yǔ)言時(shí)調(diào)用
// 傳遞的參數(shù)newLocal 為定義的語(yǔ)言,例如:en,zh-CN等
const switchLocale = async (newLocale: string) => {
await loadLocaleMessages(newLocale)
locale.value = newLocale
}
8、在組件中使用國(guó)際化
8.1組合式 API 寫(xiě)法
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const message = t('common.welcome', { name: 'John' })
</script>
8.2模板中使用:
<template>
<div>
<h1>{{ $t('common.login') }}</h1>
<p>{{ $t('common.welcome', { name: 'Alice' }) }}</p>
<button>{{ $t('common.button.confirm') }}</button>
<span>{{ $t('common.profile') }}</span>
<span>{{ $t('author.name') }}</span>
</div>
</template>
8.3在 Pinia Store 中使用
// stores/user.ts
import { defineStore } from 'pinia'
import { useI18n } from 'vue-i18n'
export const useUserStore = defineStore('user', () => {
const { t } = useI18n()
const login = () => {
console.log(t('common.login'))
}
return { login }
})
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue 在methods中調(diào)用mounted的實(shí)現(xiàn)操作
這篇文章主要介紹了vue 在methods中調(diào)用mounted的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
nginx部署多個(gè)vue項(xiàng)目的方法示例
這篇文章主要介紹了nginx部署多個(gè)vue項(xiàng)目的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Element基于el-input數(shù)字范圍輸入框的實(shí)現(xiàn)
本文主要介紹了?Element基于el-input數(shù)字范圍輸入框的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue3.0 CLI - 3.2 路由的初級(jí)使用教程
這篇文章主要介紹了vue3.0 CLI - 3.2 - 路由的初級(jí)使用教程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Vee-validate 父組件獲取子組件表單校驗(yàn)結(jié)果的實(shí)例代碼
vee-validate 是為 Vue.js 量身打造的表單校驗(yàn)框架,允許您校驗(yàn)輸入的內(nèi)容并顯示對(duì)應(yīng)的錯(cuò)誤提示信息。這篇文章主要介紹了Vee-validate 父組件獲取子組件表單校驗(yàn)結(jié)果 ,需要的朋友可以參考下2019-05-05
element表格組件實(shí)現(xiàn)右鍵菜單的功能
本文主要介紹了element表格組件實(shí)現(xiàn)右鍵菜單的功能,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue3中輕松實(shí)現(xiàn)switch功能組件的全過(guò)程
這篇文章主要給大家介紹了關(guān)于vue3中輕松實(shí)現(xiàn)switch功能組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

