Vue3多语言实现

devtools/2024/9/22 21:21:50/

1.首先安装i18n 

npm install vue-i18n

2.在项目下创建lang目录并创建en.ts,i18n1.ts,zh.ts

en.ts 

export default {message: {home: 'home',appTitle:'aa 3D Smart Measure'},    Menus: {Measuer: 'Measure',},GlueMeasure: {Title: 'Camera 3D Glue Measure',}}

zh.ts

export default {message: {home: 'home',appTitle:'aa 3D智能测量'},    Menus: {Measuer: '测量',}, GlueMeasure: {Title: '3D相机 涂胶测量',}}

i18n1.ts

import { createI18n } from "vue-i18n";
import en from './en'
import zh from './zh';const i18n = createI18n({locale:localStorage.getItem('language') || 'zh', // 默认是中文fallbackLocale: 'zh',globalInjection:true,//全局配置$tlegacy:false,messages:{en,zh}// 需要做国际化的语种})export default i18n 

3.在main.ts中配置

import i18n from './lang/i18n1'app.use(i18n)

4.在标签中使用 {{ $t('message.appTitle') }}

			<el-text class="mx-1" type="info"><span style="color: white;font-size: xx-large;">{{ $t('message.appTitle') }}</span></el-text>

5.在代码中使用方法 t("Menus.Measuer")

<script setup lang="ts">
import { computed } from 'vue';
import { useSidebarStore } from '../store/sidebar';
import { useRoute } from 'vue-router';
import i18n from '../lang/i18n1';const t = i18n.global.tconst items = [{icon: 'CameraFilled',index: '1',title: t("Menus.Measuer"), permiss: '4',subs: [{index: '/SystemLog',title: 'Log',permiss: '17',},

6.实现语言切换

				<el-dropdown class="user-name" trigger="click" @command="LanguageCommand"><span class="el-dropdown-link">{{ CurrentLanguage }}<el-icon class="el-icon--right"><arrow-down /></el-icon></span><template #dropdown><el-dropdown-menu> <el-dropdown-item command="zh">中文</el-dropdown-item><el-dropdown-item divided command="en">English</el-dropdown-item> </el-dropdown-menu> </template></el-dropdown>const LanguageCommand = (command: string) => {console.log(command)localStorage.removeItem('language')localStorage.setItem('language', command)window.location.reload()switch (command) {case 'zh':CurrentLanguage.value = "中文"break;case 'en':CurrentLanguage.value = "English"break;}
};


http://www.ppmy.cn/devtools/88347.html

相关文章

pygame制作游戏第一天

pygame制作第一天 截个图 首先还是黑屏哈。后面找时间慢慢做地图跟其他角色&#xff0c;还有攻击方式等。 这里先做了一个“炫酷”的雨云召唤技能。 人物可以移动&#xff0c;g键召唤持续10秒的跟随目标的雨云。角色会被雨滴攻击。 思路很重要&#xff0c;不然数据传递就乱了…

【环境部署】

项目 环境部署混合启动前端项目打包&#xff08;引入多模块代码&#xff09;各子模块下的图片资源链接到项目固定地址 环境部署 系统开发过程中部署问题&#xff0c;使用mklink可以创建目录或文件的符号链接或硬链接&#xff0c;其中目录联接&#xff08;使用mklink /J命令&am…

算法板子:树形DP、树的DFS——树的重心

思想&#xff1a; 代码&#xff1a; #include <iostream> #include <cstring> using namespace std;const int N 1e5 10;// vis标记当前节点是否被访问过; vis[1]true代表编号为1的节点被访问过 bool vis[N]; // h数组为邻接表; h数组上的每个坑位都串了一个单链…

MapCrafter - 定制精美的地图海报! | 限时免费

MapCrafter: 打造个性化城市地图海报的终极工具&#xff01; 在您的 iPhone、iPad、Mac 或 VisionPro 上轻松制作美丽的城市地图海报&#xff0c;展示您的城市情感与创意。 https://apps.apple.com/cn/app/mapcrafter/id6557037905 为什么选择 MapCrafter&#xff1f; • 城市…

[css3] 如何设置边框颜色渐变

div {border: 4px solid;border-image: linear-gradient(to right, #8f41e9, #578aef) 1; }参考&#xff1a; 5种CSS实现渐变色边框&#xff08;Gradient borders方法的汇总

C# Unity 面向对象补全计划 之 类class

本文仅作学习笔记与交流&#xff0c;不作任何商业用途&#xff0c;作者能力有限&#xff0c;如有不足还请斧正 本系列旨在通过补全学习之后&#xff0c;给出任意类图都能实现并做到逻辑上严丝合缝 1.类和对象 在 C# 中&#xff0c;类&#xff08;Class&#xff09;是一种数据结…

【Java基础题型】矩阵的对角线之和

二维数组真是存矩阵的好东西啊&#xff0c;现在问题来了&#xff0c;输入一个5*5一共25个数字&#xff0c;要求你求出它们两个对角线上的数字之和&#xff01; 输入格式 25个数字&#xff0c;5行5列 输出格式 它们两个对角线上的和 左上->右下第一条 右上->左下第二…

使用 MinIO、Langchain 和 Ray Data 构建分布式嵌入式子系统

嵌入子系统是实现检索增强生成所需的四个子系统之一。它将您的自定义语料库转换为可以搜索语义含义的向量数据库。其他子系统是用于创建自定义语料库的数据管道&#xff0c;用于查询向量数据库以向用户查询添加更多上下文的检索器&#xff0c;最后是托管大型语言模型 &#xff…