当前位置: 首页 > article >正文

uniapp实现中英文切换

在这里插入图片描述
在这里插入图片描述
home.js

const data = {
    ZH: {
        content1: "苹果",
    },
    EN: {
        content1: “Apple”,
    }
}
export default data

index.js

import home from "./home.js"
export default {
    home
}

en.js

import part1 from './data/part1/index.js'
const en = {
	language: {
		name: "english"
	},
	part1: {
		home: {
			content1: part1.home.EN.content1,
		}
     }
}
export default en

zh.js

import part1 from './data/part1/index'
const zh = {
	language: {
		name: "中文"
	},
	part1: {
		home: {
			content1: part1.home.ZH.content1,
		},
	},
}
export default zh

使用

{{$t("part1.home.content1")}}

页面标题

onShow: function() {
			uni.hideHomeButton();
			if (this.$i18n.locale == 'en') {
				uni.setNavigationBarTitle({
					title: 'Home Page'
				});
			} else {
				uni.setNavigationBarTitle({
					title: '首页'
				});
			}
		},

main.js

import ZH from '@/language/zh.js'  //中文最终汇总暴露的信息
import EN from '@/language/en.js'  //英文
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n({
  // localStorage.getItem('languageSet') || 'zh'
  locale: 'zh',   //从localStorage里获取用户中英文选择,没有则默认中文
  messages: {
    'zh': ZH,
    'en': EN
  }
})

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

使用

<u-button size="medium" type='primary' @click="chagelanguage">{{ language }}</u-button>

chagelanguage() {
				this.$i18n.locale == 'zh' ? this.$i18n.locale = 'en' : this.$i18n.locale = 'zh' //设置中英文模式
				if (this.$i18n.locale === 'zh') {
					this.language = "en"
					uni.setNavigationBarTitle({
						title: '首页'
					});
					uni.setTabBarItem({
						index: 0,
						text: '首页',
					})
					uni.setTabBarItem({
						index: 1,
						text: '我的',
					})
				} else {
					this.language = "zh"
					uni.setNavigationBarTitle({
						title: 'Home Page'
					});
					uni.setTabBarItem({
						index: 0,
						text: 'Home Page',
					})
					uni.setTabBarItem({
						index: 1,
						text: 'User',
					})

				}
			},

http://www.kler.cn/a/401217.html

相关文章:

  • 15分钟学 Go 第 59 天 :更高级的Go话题——接触微服务
  • 力扣题目总结
  • 解析 Apache SeaTunnel 的任务运行过程
  • 第7章硬件测试-7.4 专业实验
  • 内容分发网络CDN、动态内容缓存简介
  • Vite 基础理解及应用
  • ThreadLocal 和 Caffeine 缓存是两种不同的缓存机制,它们在用途和实现上有明显的区别
  • hash表和B树
  • 《人工智能深度学习的基本路线图》
  • DevOps-Jenkins-新手入门级
  • IndentationError: unindent does not match any outer indentation level
  • 汽车资讯新高度:Spring Boot技术飞跃
  • 百度智能云 VectorDB 优势数量 TOP 1
  • 湘潭大学软件工程算法设计与分析考试复习笔记(三)
  • 【Hadoop】【大数据技术基础】实践三 NoSQL数据库 大数据基础编程、实验和案例教程(第2版)
  • opencascade源码学习之BRepOffsetAPI包 -BRepOffsetAPI_DraftAngle
  • 大话C++:第28篇 详解独占智能指针
  • Vue3中使用:deep修改element-plus的样式无效怎么办?
  • 【算法】P5018 对称二叉树
  • 基于YOLOv8深度学习的智慧课堂教师上课行为检测系统研究与实现(PyQt5界面+数据集+训练代码)