vue3+ts+vite+pinia+element?plus項(xiàng)目使用語言國際化詳解
vue3+ts+vite+pinia+element plus項(xiàng)目使用語言國際化
1、安裝vue-i18n@9
npm install vue-i18n@9
2、創(chuàng)建語言文件en.json和zh-CN.json,路徑在src/i18n/
en.json和zh-CN.json文件內(nèi)容要一致,以便在切換語言時生效,需要傳入的內(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)語言
fallbackLocale: 'en', // 回退語言
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)建語言切換組件
路徑: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)建動態(tài)加載語言文件
路徑: 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)
}
}
在切換語言時調(diào)用
// 傳遞的參數(shù)newLocal 為定義的語言,例如:en,zh-CN等
const switchLocale = async (newLocale: string) => {
await loadLocaleMessages(newLocale)
locale.value = newLocale
}
8、在組件中使用國際化
8.1組合式 API 寫法
<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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue實(shí)現(xiàn)點(diǎn)擊按鈕下載文件的操作代碼(后端Java)
最近項(xiàng)目中需要實(shí)現(xiàn)點(diǎn)擊按鈕下載文件的需求,前端用的vue后端使用的java代碼,今天通過本文給大家分享vue點(diǎn)擊按鈕下載文件的實(shí)現(xiàn)代碼,需要的朋友參考下吧2021-08-08
vue移動端模態(tài)框(可傳參)的實(shí)現(xiàn)
這篇文章主要介紹了vue移動端模態(tài)框(可傳參)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Vue動態(tài)獲取數(shù)據(jù)后控件不可編輯問題
這篇文章主要介紹了Vue動態(tài)獲取數(shù)據(jù)后控件不可編輯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue3新特性Suspense和Teleport應(yīng)用場景分析
本文介紹了Vue2和Vue3中的Suspense用于處理異步請求的加載提示,以及如何在組件間實(shí)現(xiàn)動態(tài)加載,同時,Teleport技術(shù)展示了如何在DOM中靈活地控制組件的渲染位置,解決布局問題,感興趣的朋友跟隨小編一起看看吧2024-07-07
vue2的todolist入門小項(xiàng)目的詳細(xì)解析
本篇文章主要介紹了vue2的todolist入門小項(xiàng)目的詳細(xì)解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
vscode 配置vue+vetur+eslint+prettier自動格式化功能
這篇文章主要介紹了vscode 配置vue+vetur+eslint+prettier自動格式化功能,本文通過實(shí)例代碼圖文的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
vue+element 實(shí)現(xiàn)商城主題開發(fā)的示例代碼
這篇文章主要介紹了vue+element 實(shí)現(xiàn)商城主題開發(fā)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue實(shí)現(xiàn)修改標(biāo)簽中的內(nèi)容:id class style
這篇文章主要介紹了vue實(shí)現(xiàn)修改標(biāo)簽中的內(nèi)容:id class style,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

