vue3+ts+vite+pinia+element plus项目使用语言国际化详解

 更新时间:2025年11月22日 15:17:08   作者:活宝小娜  
文章介绍了如何在Vue 3 + TypeScript + Vite + Pinia + Element Plus项目中实现语言国际化,主要步骤包括安装vue-i18n、创建语言文件、创建i18n实例、全局引入、类型声明(可选)、创建语言切换组件、动态加载语言文件以及在组件和PiniaStore中使用国际化

vue3+ts+vite+pinia+element plus项目使用语言国际化

1、安装vue-i18n@9

npm install vue-i18n@9

2、创建语言文件en.json和zh-CN.json,路径在src/i18n/

en.json和zh-CN.json文件内容要一致,以便在切换语言时生效,需要传入的内容可用{}

en.json内容:

{
    "common": {
      "login": "Login",
      "welcome": "Welcome, {name}!",
      "button": {
        "confirm": "Confirm",
        "cancel": "Cancel"
      }
    },
    "author": {
       "name": "name"
    }
}

zh-CN.json内容

{
    "common": {
      "login": "登录",
      "welcome": "欢迎,{name}!",
      "button": {
        "confirm": "确定",
        "cancel": "取消"
      }
    },
    "author": {
       "name": "姓名"
    }
}

3、创建 i18n 实例

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', // 默认语言
  fallbackLocale: 'en', // 回退语言
  globalInjection: true, // 全局注入 $t
  messages
})

// 导出类型增强
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、创建语言切换组件

路径: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、创建动态加载语言文件

路径: 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)
  }
}

在切换语言时调用

// 传递的参数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 }
})

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue实现点击按钮下载文件的操作代码(后端Java)

    Vue实现点击按钮下载文件的操作代码(后端Java)

    最近项目中需要实现点击按钮下载文件的需求,前端用的vue后端使用的java代码,今天通过本文给大家分享vue点击按钮下载文件的实现代码,需要的朋友参考下吧
    2021-08-08
  • 前端vue3搭建超详细指南(快速上手搭建)

    前端vue3搭建超详细指南(快速上手搭建)

    这篇文章主要介绍了使用Vue 3和Element-plus搭建前端项目的过程,包括项目搭建、Element-plus安装和引入、axios安装、sass使用、app.vue配置、组件创新、路由配置以及使用辅助插件来提高开发效率,需要的朋友可以参考下
    2025-02-02
  • vue移动端模态框(可传参)的实现

    vue移动端模态框(可传参)的实现

    这篇文章主要介绍了vue移动端模态框(可传参)的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Vue动态获取数据后控件不可编辑问题

    Vue动态获取数据后控件不可编辑问题

    这篇文章主要介绍了Vue动态获取数据后控件不可编辑问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • Vue3新特性Suspense和Teleport应用场景分析

    Vue3新特性Suspense和Teleport应用场景分析

    本文介绍了Vue2和Vue3中的Suspense用于处理异步请求的加载提示,以及如何在组件间实现动态加载,同时,Teleport技术展示了如何在DOM中灵活地控制组件的渲染位置,解决布局问题,感兴趣的朋友跟随小编一起看看吧
    2024-07-07
  • vue2的todolist入门小项目的详细解析

    vue2的todolist入门小项目的详细解析

    本篇文章主要介绍了vue2的todolist入门小项目的详细解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • 前端Vue自定义地址展示地址选择地址管理组件的示例详解

    前端Vue自定义地址展示地址选择地址管理组件的示例详解

    这篇文章主要介绍了前端Vue自定义地址展示地址选择地址管理组件的示例详解,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06
  • vscode 配置vue+vetur+eslint+prettier自动格式化功能

    vscode 配置vue+vetur+eslint+prettier自动格式化功能

    这篇文章主要介绍了vscode 配置vue+vetur+eslint+prettier自动格式化功能,本文通过实例代码图文的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • vue+element 实现商城主题开发的示例代码

    vue+element 实现商城主题开发的示例代码

    这篇文章主要介绍了vue+element 实现商城主题开发的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • vue实现修改标签中的内容:id class style

    vue实现修改标签中的内容:id class style

    这篇文章主要介绍了vue实现修改标签中的内容:id class style,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07

最新评论