uniapp国际化(多语言)开发
  XbErmt1KecDa 2023年11月02日 53 0

uni-app 实现 中文、英文、越南语多语言切换。pages.json(导航栏、tabber)也国际化

uniapp国际化(多语言)开发_Vue

1.main.js 引入并初始化 VueI18n

安装

npm i VueI18n

引入

import Vue from 'vue'
import App from './App'
// 国际化
import messages from './locale/index'
let i18nConfig = {
  locale: uni.getLocale(),
  messages
}
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)
 
Vue.config.productionTip = false
 

App.mpType = 'app'

const app = new Vue({
	i18n, 
	...App
})
app.$mount()

2.自定义语言包配置文件

uniapp国际化(多语言)开发_Vue_02

index.js 引用不同的语言配置文件

import en from './en.json'
import zhHans from './zh-Hans.json' 
import vie from './vie.json'
export default {
	'zh-Hans': zhHans,
	en,
	vie
}

zh-Hans.json 中文

{
	"pageTitle.index": "首页",
	"pageTitle.setLanguage": "设置语言",
	"language":{
		"zh":"中文",
		"vie":"越南语",
		"en":"英语",
		"nowLanguage":"中文",
		"confirmcontent":"确认切换该语言吗",
		"setTitle":"当前系统语言"
	},
	"public":{
		"confirm":"确认",
		"cancellation":"取消"
	} 
	 
}

en.json 英文

{
	"pageTitle.index": "Home page",
	"pageTitle.setLanguage": "Set Language",
	"language":{
		"zh":"Chinese",
		"vie":"Vietnamese",
		"en":"English",
		"nowLanguage":"English",
		"confirmcontent":"Are you sure to switch to this language",
		"setTitle":"Current system language"
	},
	"public":{
		"confirm":"confirm",
		"cancellation":"cancellation"
	}
}

vie.json 越南语

{
	"pageTitle.index": "Home page",
	"pageTitle.setLanguage": "Set Language",
	"language":{
		"zh":"Chinese",
		"vie":"Vietnamese",
		"en":"English",
		"nowLanguage":"English",
		"confirmcontent":"Are you sure to switch to this language",
		"setTitle":"Current system language"
	},
	"public":{
		"confirm":"confirm",
		"cancellation":"cancellation"
	}
}

3.多语言语法

语法:$t('')

uniapp国际化(多语言)开发_uniapp_03

uniapp国际化(多语言)开发_App_04

在pages.json 配置文件也使用国际化,使用%

uniapp国际化(多语言)开发_App_05

最终国际化切换页面

<template>
  <view class="container">
     
    <view class="locale-setting">{{$t('language.setTitle')}}:{{$t('language.nowLanguage')}}</view>
    <view class="locale-list">
      <view class="locale-item" v-for="(item, index) in locales" :key="index" @click="onLocaleChange(item)">
        <text class="text">{{item.text}}</text>
		<image src="../../../static/checked.png" mode="widthFix" v-show="applicationLocale==item.code"></image>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        systemLocale: '',
        applicationLocale: ''
      }
    },
    computed:{
      locales() {
        return [  {
            text: this.$t('language.zh'),
            code: 'zh-Hans'
          }, {
            text: this.$t('language.en'),
            code: 'en'
          }, {
            text: this.$t('language.vie'),
            code: 'vie'
          }
        ]
      }
    },
    onLoad() {
      let systemInfo = uni.getSystemInfoSync();
      this.systemLocale = systemInfo.language;
      this.applicationLocale = uni.getLocale();
     
      uni.onLocaleChange((e) => {
        this.applicationLocale = e.locale;
      })
    },
    methods: {
      onLocaleChange(e) {
      
		  uni.showModal({
			content: this.$t('language.confirmcontent'),
			cancelText:this.$t('public.confirm'),
			confirmText:this.$t('public.cancellation'),
			success: (res) => {
			  if (res.confirm) {
				uni.setLocale(e.code);
			  }
			}
		  })
        
      }
    }
  }
</script>

<style lang="scss" scoped>
.container{
	padding: 20px;
	.locale-list{
		margin-top: 20rpx;
	}
	.locale-item{
		border-bottom: 1px solid #e6e6e6;
		line-height: 50rpx;
		font-size: 22rpx;
		color: #333;
		padding: 5rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		image{
			width: 25rpx;
			height: 25rpx;
		}
	}
	
	
}
</style>


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  gYl4rku9YpWY   2024年05月17日   57   0   0 Vue
  JZjRRktyDDvK   2024年05月02日   76   0   0 Vue
  VlNAKfyhjjp9   2024年04月30日   66   0   0 Vue
  JNTrZmaOQEcq   2024年04月30日   51   0   0 Vue
  onf2Mh1AWJAW   2024年05月17日   57   0   0 Vue
  3A8RnFxQCDqM   2024年05月17日   49   0   0 Vue
XbErmt1KecDa