uniapp項(xiàng)目國(guó)際化標(biāo)準(zhǔn)的配置與實(shí)現(xiàn)
UniApp是一種基于Vue.js的跨平臺(tái)開發(fā)框架,可以快速地開發(fā)同時(shí)運(yùn)行在多個(gè)平臺(tái)的應(yīng)用程序。這篇文章主要介紹了uniapp項(xiàng)目國(guó)際化標(biāo)準(zhǔn)的配置與實(shí)現(xiàn),需要的朋友可以參考下。
創(chuàng)建資源文件
創(chuàng)建一個(gè)locale文件夾,新增index.js,en.json,zh-hans.json
配置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(),// 獲取已設(shè)置的語(yǔ)言
messages
}
const i18n = new VueI18n(i18nConfig)
export default i18nmain.js 引入
// VUE2
import i18n from './locale'
Vue.use(i18n)
const app = new Vue({
i18n,
...App
})國(guó)際化json文件內(nèi)容
{
"index.title": "Hello i18n"
}頁(yè)面使用i18n
vue和js里的內(nèi)容國(guó)際化是與web通行的方案。
<template>
<view class="container">
<view class="title">{{$t('index.title')}}</view>
</view>
</template>
<script>
export default {
data() {
return {
}
}
}
</script>//普通文本使用方式:
{{ $t('index.titlee') }}
//標(biāo)簽內(nèi)使用方式:
:placeholder="$t('index.title')"
//js內(nèi)使用方式
this.$t('index.title')
在js文件中使用國(guó)際化
import i18n from '../locale'
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('')
與后臺(tái)同步切換語(yǔ)言文件
利用封裝的request.js對(duì)發(fā)給后臺(tái)的接口Header進(jìn)行統(tǒng)一處理
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.baseUrl
const request = config => {
// 是否需要設(shè)置 token
const isToken = (config.headers || {}).isToken === false
config.header = config.header || {}
if (getToken() && !isToken) {
config.header['Authorization'] = 'Bearer ' + getToken()
}
config.header["Accept-Language"] = (uni.getLocale()==='zh-Hans'?'zh':'en') || "en"
// get請(qǐng)求映射params參數(shù)
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] = response
if (error) {
toast(i18n.t('request.unusual'))
reject(i18n.t('request.unusual'))
return
}
const code = res.data.code || 200
const 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 } = error
if (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即將選擇語(yǔ)言寫到接口的Header中,實(shí)現(xiàn)與后端同步切換語(yǔ)言
config.header["Accept-Language"] = (uni.getLocale()==='zh-Hans'?'zh':'en') || "en"
在頁(yè)面切換語(yǔ)言
注意:頁(yè)面中設(shè)置語(yǔ)言后需要調(diào)用 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 {
// 語(yǔ)言標(biāo)識(shí)
lang:''
}
},
methods: {
// 動(dòng)態(tài)更改語(yǔ)言
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>
到此這篇關(guān)于uniapp項(xiàng)目國(guó)際化標(biāo)準(zhǔn)的配置與實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)uniapp國(guó)際化配置與實(shí)現(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ElementUI實(shí)現(xiàn)在下拉列表里面進(jìn)行搜索功能詳解
有時(shí)候需要用到下拉列表全選和搜索,下面這篇文章主要給大家介紹了關(guān)于ElementUI實(shí)現(xiàn)在下拉列表里面進(jìn)行搜索功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Vue?項(xiàng)目運(yùn)行完成后自動(dòng)打開瀏覽器的方法匯總
這篇文章主要介紹了Vue?項(xiàng)目運(yùn)行完成后自動(dòng)打開瀏覽器的多種實(shí)現(xiàn)方法,方法一比較適用于vue3,每種方法通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-02-02
Element?UI?Upload?組件上傳圖片可刪除、預(yù)覽功能
這篇文章主要介紹了Element?UI?Upload?組件?上傳圖片可刪除、預(yù)覽,設(shè)置只允許上傳單張?/?多張圖片的操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
vscode中使用vue的一些插件總結(jié)(方便開發(fā))
對(duì)于很多使用vscode編寫vue項(xiàng)目的新手同學(xué)來說,可能不知道使用什么插件,下面這篇文章主要給大家介紹了關(guān)于vscode中使用vue的一些插件,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
關(guān)于vue項(xiàng)目部署后刷新網(wǎng)頁(yè)報(bào)404錯(cuò)誤解決
這篇文章主要介紹了關(guān)于vue項(xiàng)目部署后刷新網(wǎng)頁(yè)報(bào)404錯(cuò)誤解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
淺談vue同一頁(yè)面中擁有兩個(gè)表單時(shí),的驗(yàn)證問題
今天小編就為大家分享一篇淺談vue同一頁(yè)面中擁有兩個(gè)表單時(shí),的驗(yàn)證問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09

