uniapp国际化配置

news/2025/2/5 22:11:29/

1、创建资源文件

创建一个locale文件夹,新增index.js,en.json,zh-hans.json
在这里插入图片描述

2.配置locale文件夹中的index.js文件

import Vue from 'vue'
import VueI18n from 'vue-i18n'// v8.x
import en from './en.json'
import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.json'
import ja from './ja.json'Vue.use(VueI18n)
const messages = {en,'zh-Hans': zhHans,'zh-Hant': zhHant,ja
}let i18nConfig = {locale: uni.getLocale(),// 获取已设置的语言messages
}
const i18n = new VueI18n(i18nConfig)
export default i18n

3、main.js 引入

// VUE2
import i18n from './locale'Vue.use(i18n)const app = new Vue({i18n,...App
})

4、国际化json文件内容

{"index.title": "Hello i18n"
}

5、页面使用i18n

vue和js里的内容国际化是与web通行的方案。

<template><view class="container"><view class="title">{{$t('index.title')}}</view></view>
</template><script>export default {data() {return {}}}
</script>
//普通文本使用方式: 
{{ $t('index.titlee') }}
//标签内使用方式:   
:placeholder="$t('index.title')"
//js内使用方式       
this.$t('index.title')

6、在js文件中使用国际化

import i18n from '../locale'export default {'401': i18n.t('errorCode.401'),'403': i18n.t('errorCode.403'),'404': i18n.t('errorCode.404'),'default': i18n.t('errorCode.default')
}
// 即在引入后使用
i18n.t('')

7、与后台同步切换语言文件

利用封装的request.js对发给后台的接口Header进行统一处理

import store from '@/store'
import config from '@/config'
import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { toast, showConfirm, tansParams } from '@/utils/common'
import i18n from '../locale'
let timeout = 60000
const baseUrl = config.baseUrlconst request = config => {// 是否需要设置 tokenconst isToken = (config.headers || {}).isToken === falseconfig.header = config.header || {}if (getToken() && !isToken) {config.header['Authorization'] = 'Bearer ' + getToken()}config.header["Accept-Language"] = (uni.getLocale()==='zh-Hans'?'zh':'en') || "en"// get请求映射params参数if (config.params) {let url = config.url + '?' + tansParams(config.params)url = url.slice(0, -1)config.url = url}return new Promise((resolve, reject) => {uni.request({method: config.method || 'get',timeout: config.timeout ||  timeout,url: config.baseUrl || baseUrl + config.url,data: config.data,header: config.header,dataType: 'json'}).then(response => {let [error, res] = responseif (error) {toast(i18n.t('request.unusual'))reject(i18n.t('request.unusual'))return}const code = res.data.code || 200const msg = errorCode[code] || res.data.msg || errorCode['default']if (code === 401) {showConfirm(i18n.t('request.exceedTheTimeLimit')).then(res => {if (res.confirm) {store.dispatch('LogOut').then(res => {uni.reLaunch({ url: '/pages/login' })})}})reject(i18n.t('request.ofNoAvail'))} else if (code === 500) {toast(msg)reject('500')} else if (code !== 200) {toast(msg)reject(code)}resolve(res.data)}).catch(error => {let { message } = errorif (message === 'Network Error') {message = i18n.t('request.unusual')} else if (message.includes('timeout')) {message = i18n.t('request.overtime')} else if (message.includes('Request failed with status code')) {message = i18n.t('request.system') + message.substr(message.length - 3) + i18n.t('request.unusual2')}toast(message)reject(error)})})
}export default request

即将选择语言写到接口的Header中,实现与后端同步切换语言

config.header["Accept-Language"] = (uni.getLocale()==='zh-Hans'?'zh':'en') || "en"

8、在页面切换语言

注意:页面中设置语言后需要调用 this.$i18n.locale = ‘zh-Hans’ 后生效

<template><view><view class="login-footer"><text @click="changeLang('en')" style="margin-right: 5px;" :class="lang==='en'?'text-black':'text-blue'">English</text><text @click="changeLang('zh')" style="margin-left: 5px;" :class="lang==='en'?'text-blue':'text-black'">中文</text></view></view>
</template>
<script>
export default {data() {return {// 语言标识lang:''}},methods: {// 动态更改语言changeLang(e) {this.lang = e || 'en'console.log(e);if (e === 'en') {uni.setLocale('en');this.$i18n.locale = 'en'this.changTitle()} else {uni.setLocale('zh-Hans');this.$i18n.locale = 'zh-Hans'this.changTitle()this.lang = 'zh'}}}}
</script><style lang="scss">	.login-footer {height: 40px;line-height: 40px;position: fixed;// bottom: 40px;margin-top: 20px;width: 100%;text-align: center;font-family: Arial;font-size: 18px;letter-spacing: 1px;}
</style>

http://www.ppmy.cn/news/37992.html

相关文章

AIGC之Stable Diffusion 提示词学徒库

前言 描述&#xff1a;本文主要用来记录 提示词TAG 一、提示词 1、提升画面品质的提示词 masterpiece 杰作best quality 最佳品质ultra highers 超高分辨率8k resolution 8k分辨率realistic 逼真ultra detailed 超细致sharp focus 清晰聚焦RAW photo RAW 照片 大概的权重比…

剪格子

[蓝桥杯 2013 省 A] 剪格子 题目描述 如图 111 所示&#xff0c;333\times 333 的格子中填写了一些整数。 我们沿着图中的红色线剪开&#xff0c;得到两个部分&#xff0c;每个部分的数字和都是 606060。 本题的要求就是请你编程判定&#xff1a;对给定的 mnm\times nmn 的格…

Python学习总结

Python是一门十分流行的编程语言&#xff0c;由于其简单易学、语法清晰、应用广泛等特点&#xff0c;吸引了越来越多的人学习和使用。这是对Python学习中进行总结。 1.基础语法 在Python学习的中&#xff0c;主要学习了Python的基础语法&#xff0c;包括变量、运算符、条件语…

精选简历模板

1.应届生通用简历模板&#xff08;.docx) 适用于应届生找工作的学生群体 https://download.csdn.net/download/weixin_43042683/87652099https://download.csdn.net/download/weixin_43042683/87652099 部分缩略图如下&#xff1a; 2.研究生通用简历模板&#xff08;.docx)…

GIS 粒计算定义的几种方式(骆老师,吴老师专用)

摘要: 要想写出无懈可击的定义, 就需要使用集合、元组、序列等术语. 1. 区域 在二维平面中, 区域可定义如下: 定义 1 (连通区域, Area): 二维平面中的连通区域为 A⊆R2,\mathbf{A} \subseteq \mathbb{R}^2,A⊆R2, 其中 ∀(x1,y1),(x2,y2)∈A\forall (x_1, y_1), (x_2, y_2) \…

LeetCode.每日一题 831. 隐藏个人信息

Halo&#xff0c;这里是Ppeua。平时主要更新C语言&#xff0c;C&#xff0c;数据结构算法......感兴趣就关注我吧&#xff01;你定不会失望。 &#x1f308;个人主页&#xff1a;主页链接 &#x1f308;算法专栏&#xff1a;专栏链接 我会一直往里填充内容哒&#xff01; &…

【Yapi】Yapi最新版详细安装步骤图文教程,避免踩坑

1. 安装 node.js 安装node.js&#xff0c;选择版本node-v12.16.1-x64.msi 一直点击下一步就好&#xff0c;路径默认C盘就好&#xff0c;不需要做修改。 2. 安装MongDB 下载mongodb-win32-x86_64-2012plus-4.2.3-signed.msi&#xff0c;安装 选择complete完整的安装模式&…

Windows安装Anaconda使用教程

主要介绍在 windows 11 系统中安装 Anaconda3 的详细过程 下载 Anaconda 官网下载地址&#xff1a;https://www.anaconda.com/products/distribution 目前最新版本是 python 3.10&#xff0c;默认下载也是 Python 3.10 安装 安装较为简单&#xff0c;基本都是下一步&#…