Vue3多语言实现

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,//全局配置$t
  legacy: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.t

const 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;
	}
};

Vue 3实现语言功能,最常用的是使用 `vue-i18n` 插件。以下是详细步骤: ### 安装 `vue-i18n` 首先要安装 `vue-i18n` 插件,这是实现多语言支持的基础 [^2]。 ### 配置语言包 在根目录创建一个 `i18n` 文件夹,并在其中创建 `index.js` 文件,示例代码如下: ```javascript import { createI18n } from &#39;vue-i18n&#39; // 以下为语言包单独设置的场景,单独设置时语言包需单独引入 const messages = { zh_CN: require("./locale/zh_CN"), // 中文语言包 en_US: require("./locale/en_US"), // 英文语言包 }; const i18n = createI18n({ locale: &#39;zh_CN&#39;, // 设置默认语言 messages: messages // 设置语言消息 }) export default i18n ``` 上述代码创建了一个 `i18n` 实例,指定了默认语言为中文,并引入了中文和英文语言包 [^5]。 ### 在项目中集成 `vue-i18n` 在项目的主入口文件(通常是 `main.js`)中引入并使用 `i18n` 实例: ```javascript import { createApp } from &#39;vue&#39; import App from &#39;./App.vue&#39; import i18n from &#39;./i18n&#39; const app = createApp(App) app.use(i18n) app.mount(&#39;#app&#39;) ``` 通过上述代码,将 `i18n` 实例集成到 Vue 3 项目中 [^2]。 ### 在组件中使用多语言 可以使用组合式 API 方案来使用多语言功能,示例代码如下: ```javascript import { useI18n } from &#39;vue-i18n&#39; export default { setup() { const { t, locale } = useI18n() return { t, locale } } } ``` 在组件中通过 `t` 函数来获取对应语言的文本,通过 `locale` 来切换语言 [^3]。 ### 后续优化 可以把切换的语言放到 `store` 中,以便在其他地方使用,从而为不同语言用户提供更好的用户体验 [^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值