Vue实现国际化处理

本文介绍了如何在Vue项目中实现国际化处理,包括安装相关插件、配置国际化文件(zh.ts、en.ts)、设置国际化配置文件(index.ts),并在main.ts中注册插件。在组件中,可以使用`$t()`获取国际化内容,通过`useI18n`方法的`locale.value`来切换语言。整个过程基于查表思想,将需切换的语言内容存储于语言包,然后动态获取。

1 安装 插件

npm install vue-i18n@next  

2 在src 目录下配置国际化文件locales, 并且创建 语言表,和配置文件

zh.ts

 export default {
    layout: {
        Home: '首页',
        
    }
}

en.ts

 export default {
    layout: {
        Home: 'home',
        
    }
}

国际化配置文件

index.ts

import { createI18n } from 'vue-i18n' //引入vue-i18n组件
import zh from './zh'  // 中文语言包
import en from './en'  // 英文语言包
 
// 实例化I18n
const i18n = createI18n({
    legacy: false, //处理报错Uncaught (in promise) 
    locale: "zh", // 初始化配置语言
    messages: {
        zh,
        en
    }
})
 
export default i18n

在到mian.ts中注册插件

import i18n from './locales/index'  // 引入
import App from './App.vue'

createApp(App)
    .use(i18n)    //加载

这样我们就可以在组件中使用国际化了

1 获取 =>{{$t(layout.Home)}}

2 改变语言

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <button  @click="change('zh')">中文</button> --
 
  <router-view />
</template>
 
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
 
const { locale } = useI18n()
    function change(type: string) {
      locale.value = type;
    }
</script>

总结: 国际化的本质 查表的思想 

1:将我们项目中的所有需要切换语言 内容 存放到语言包中

2: 在将我们所有需要却换内容的地方  国际化获取到动态的数据 {{ $t(表中对应的内容)}}}

3: 在通过 useI18n 这个 方法返回对象中的数据 locale.value 改变 语言

### 实现 Spring Boot 和 Vue 项目的国际化 #### 后端配置 (Spring Boot) 为了支持多语言,在 `application.properties` 或者 `application.yml` 文件中设置默认的语言和地区: ```properties spring.messages.basename=i18n/messages spring.mvc.locale=zh_CN spring.mvc.locales=en_US,zh_CN ``` 创建资源文件夹 `/src/main/resources/i18n/`, 并放置不同语言的消息文件,比如 `messages_zh_CN.properties` 和 `messages_en_US.properties`. 这些文件包含了键值对形式的信息字符串[^1]. 定义控制器来处理请求并返回相应的视图或数据: ```java import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class LocaleController { @GetMapping("/locale") public String getLocale() { return LocaleContextHolder.getLocale().toString(); } } ``` #### 前端配置 (Vue.js) 安装必要的依赖包以启用 i18n 功能: ```bash npm install vue-i18n --save ``` 初始化插件并在 main.js 中注册它: ```javascript // main.js import Vue from 'vue' import VueI18n from 'vue-i18n' Vue.use(VueI18n); const messages = { en: { message: require('./locales/en') }, zh: { message: require('./locales/zh') } } export const i18n = new VueI18n({ locale: localStorage.getItem('lang') || navigator.language.split('-')[0], // 设置初始语言 fallbackLocale: 'en', messages, }) ``` 在组件内部使用 `$t()` 方法获取翻译后的文本: ```html <template> <div>{{ $t('message.hello') }}</div> </template> <script> export default {} </script> ``` 对于消息文件, 创建对应的 JSON 文件放在 locales 目录下,例如 `./locales/en.json` 及其对应中文版本。 ```json { "hello": "Hello world!" } ``` 当用户切换语言时更新应用中的当前语言环境,并保存用户的偏好以便下次访问自动加载该语言: ```javascript methods: { changeLang(lang) { this.$i18n.locale = lang; localStorage.setItem('lang', lang); } } ``` 通过上述方式可以有效地实现在基于 Spring Boot 和 Vue 的应用程序上的国际化特性。这不仅提高了用户体验,还使得产品能够更好地适应全球市场的需求[^2].
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值