vue3项目国际化,你还不了解吗?

简介: vue3项目国际化,你还不了解吗?

vue3使用的国际化库为:i18n

安装方式:

npm install vue-i18n@next

安装完成后在src文件夹下新建lang文件夹

lang文件夹下新建需要语言转换的文件夹,这里以中文zh英文en举例,在这两个文件夹下新建需要转换的语言

zhindex.ts中写好我们需要转换的语言

enindex.ts也是一样的

lang文件夹下新建index.ts文件,在该文件下引入刚才的建立的语言包,配置需要使用的默认语言

// lang -> index.ts
import { createI18n } from 'vue-i18n'
import zh from './zh' 
import en from './en'
/* 这里必须是messages名称 */
const messages = {
    'zh-US' : zh,
    'en-US' : en
}
const i18n = createI18n({
    legacy: false,         // 使用 Composition API 模式,则需要将其设置为false
    globalInjection: true, //全局生效$t
    locale: 'zh-US',          // 默认使用的语言
    messages,  // 使用数据源
})
export { i18n }

main.ts文件下导入并挂载i18n

import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";
import router from "./router/index";
// 引入国际化
import { i18n } from './lang/index'
const app = createApp(App)
/* globalProperties一个用于注册能够被应用内所有组件实例访问到的全局属性的对象。 */
app.config.globalProperties.$i18n = i18n; 
app
.use(router)
.use(i18n)  // 挂载i18n
.mount("#app")

main文件设置好后,我们就可以在页面使用了,页面的使用方法:

<div>
  <el-switch
      v-model="lang"
      inline-prompt
      style="--el-switch-on-color: #13ce66; --el-switch-off-color: #97a0ff"
      active-text="中"
      inactive-text="英"
      @change="languageChange"
  />
  <!-- 在页面中使用,这里对应的是语言包中的对象,此处可以使用模板字符串 -->
  <span>{{  t('navUtil.loginText')  }}</span>
</div>
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const lang = ref(true);  // 开关,语言状态切换
/* getCurrentInstance()可以用来获取当前组件实例 */
let current = getCurrentInstance()?.appContext.config.globalProperties as any;
// 根据状态,切换国际化
const languageChange = (val : boolean) => {
  // false英 true中,此处的zh-US就是lang -> index.ts -> messages对象的键名
  val ? current.$i18n.locale = 'zh-US' : current.$i18n.locale = 'en-US'
}


如果是后台返回的菜单数据,我们需要根据菜单数据的键做语言处理,例如这样的

首先需要在lang对应的中英文文件夹下新增语言配置,由于后台返回的键名都是一样的,我们可以使用 键+id 的方法让这些键保持独立,例如:

菜单一般都是遍历出来的,在页面中用模板字符串拼接id即可

<span>{{ t(`menu.authName${item.id}`) }}</span>

如果觉得这篇文章对你有帮助,欢迎点赞👍、收藏💖、转发✨哦~

目录
相关文章
|
9天前
vue3【实战】语义化首页布局
vue3【实战】语义化首页布局
28 2
|
9天前
|
存储 容器
vue3【实战】来回拖拽放置图片
vue3【实战】来回拖拽放置图片
19 2
|
9天前
|
JavaScript 开发工具 开发者
vue3【提效】使用 VueUse 高效开发(工具库 @vueuse/core + 新增的组件库 @vueuse/components)
vue3【提效】使用 VueUse 高效开发(工具库 @vueuse/core + 新增的组件库 @vueuse/components)
32 1
|
9天前
|
API
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
19 1
|
9天前
|
JavaScript
vue3 【提效】自动注册组件 unplugin-vue-components 实用教程
vue3 【提效】自动注册组件 unplugin-vue-components 实用教程
17 1
|
10天前
|
JavaScript API
vue3【实用教程】组件(含父子组件传值 defineProps,自定义事件 defineEmits,defineProps,插槽 slot,动态组件 :is 等)
vue3【实用教程】组件(含父子组件传值 defineProps,自定义事件 defineEmits,defineProps,插槽 slot,动态组件 :is 等)
19 1
|
9天前
|
JavaScript 网络架构
vue3 【提效】自动路由(含自定义路由) unplugin-vue-router 实用教程
vue3 【提效】自动路由(含自定义路由) unplugin-vue-router 实用教程
48 0
vue3 【提效】自动路由(含自定义路由) unplugin-vue-router 实用教程
|
4天前
【vue3】Argumnt of type ‘history:RouterHistory;}is not assignable to paraeter of type ‘RouterOptions‘.
【vue3】Argumnt of type ‘history:RouterHistory;}is not assignable to paraeter of type ‘RouterOptions‘.
6 0
|
4天前
|
JavaScript
【vue3】vue3中路由hash与History的设置
【vue3】vue3中路由hash与History的设置
9 0
|
4天前
|
编解码 前端开发
【Vue3】解决电脑分辨率125%、150%及缩放导致页面变形的问题
【Vue3】解决电脑分辨率125%、150%及缩放导致页面变形的问题
11 0