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

【微信小程序】实现中英文切换

1、组织语言资源

创建两个文件夹,分别用于存放中文和英文的语言资源。例如,可以在 utils 文件夹下创建 lang 文件夹,然后在其中创建 zh.js 和 en.js 文件,分别存放中文和英文的文本内容。

zh.js:

const zh = {home: {title: '这里是首页',},
};module.exports = zh;

en.js:

const en = {home: {title: 'Here is the home page',},
};module.exports = en;
2、创建语言切换逻辑

在 app.js 文件中,设置默认语言并提供切换语言的功能。

// app.js
App({globalData: {language: 'zh', // 默认语言translations: require('./utils/lang/zh') // 默认加载中文语言包},onLaunch: function () {// 从缓存中获取用户选择的语言const language = wx.getStorageSync('language') || 'zh';this.setLanguage(language);},setLanguage: function (lang) {this.globalData.language = lang;if (lang === 'en') {this.globalData.translations = require('./utils/lang/en');} else {this.globalData.translations = require('./utils/lang/zh');}wx.setStorageSync('language', lang);}
});
3、使用语言资源
① 在页面中使用
Page({data: {translations: {}},onShow: function () {const app = getApp();this.setData({translations: app.globalData.translations});},switchLanguage: function () {const app = getApp();const newLang = app.globalData.language === 'zh' ? 'en' : 'zh';app.setLanguage(newLang);this.setData({translations: app.globalData.translations});}
});
<view><text>{{translations.home.title}}</text><button bindtap="switchLanguage">切换语言</button>
</view>
② 导航栏修改

把页面导航栏信息放到语言文件中:

// zh.js
nav: {home: '首页',myInfo: '我的',func: '功能'
},
// en.js
nav: {home: 'home',myInfo: 'myInfo',func: 'function'
},
 onShow: function () {const app = getApp();wx.setNavigationBarTitle({title: translations.nav.home});}
③ 底部栏修改

使用自定义 tabbar
可参考 微信小程序自定义tabbar

custom-tab-bar文件中:

index.js:

Component({/*** 组件的属性列表*/properties: {},/*** 组件的初始数据*/data: {selected: 0,list: [{pagePath: "/pages/index/index",text: "首页",iconPath: "/public/image/icon_home2.png",selectedIconPath: "/public/image/icon_home1.png"},{pagePath: "/pages/apply/apply",text: "功能",iconPath: "/public/image/icon_app2.png",selectedIconPath: "/public/image/icon_app1.png"},{pagePath: "/pages/myInfo/myInfo",text: "我的",iconPath: "/public/image/icon_set2.png",selectedIconPath: "/public/image/icon_set1.png"}]},/*** 组件的方法列表*/methods: {switchTab(e) {const data = e.currentTarget.datasetconst url = data.pathwx.switchTab({ url })},}
})

index.json:

{"component": true,"usingComponents": {}
}

index.wxml:

<view class="tabBar"><view class="cont"><view wx:for="{{list}}" wx:key="index" data-path="{{item.pagePath}}" data-index="{{item.pagePath}}" bindtap="switchTab" class="item {{selected === index ? 'on' : 'off'}}"><image class="cont_img" src="{{selected === index  ? item.selectedIconPath : item.iconPath}}"></image><view>{{item.text}}</view></view></view>
</view>

index.wxss:

.tabBar {z-index: 100;width: 100%;position: fixed;bottom: 0;font-size: 28rpx;border-radius: 50rpx 50rpx 0 0;color: #636363;box-sizing: border-box;overflow: hidden;
}
.bgcW {background-color: #fff;
}
.bgcNone {background-color: #fff0;
}.cont {height: 150rpx;margin: 0 auto;border-radius: 40rpx 0;overflow: hidden;display: flex;background-color: #fff;padding-bottom: 40rpx;
}.cont .item {font-size: 24rpx;position: relative;flex: 1;text-align: center;padding: 0;display: block;height: 100%;
}
.cont .cont_img {width: 40rpx;height: 40rpx;margin: 40rpx 0 0;
}.off {color: #a0a0a0;
}
.on {color: #363636;
}

做中英文切换时,需要把 tabbar 的信息放到语言文件中:

// zh.js
list: [{pagePath: "/pages/index/index",text: "首页",iconPath: "/public/image/icon_home2.png",selectedIconPath: "/public/image/icon_home1.png"},{pagePath: "/pages/apply/apply",text: "功能",iconPath: "/public/image/icon_app2.png",selectedIconPath: "/public/image/icon_app1.png"},{pagePath: "/pages/myInfo/myInfo",text: "我的",iconPath: "/public/image/icon_set2.png",selectedIconPath: "/public/image/icon_set1.png"}]
// en.js
list: [{pagePath: "/pages/index/index",text: "Index",iconPath: "/public/image/icon_home2.png",selectedIconPath: "/public/image/icon_home1.png"},{pagePath: "/pages/apply/apply",text: "Function",iconPath: "/public/image/icon_app2.png",selectedIconPath: "/public/image/icon_app1.png"},{pagePath: "/pages/myInfo/myInfo",text: "MyInfo",iconPath: "/public/image/icon_set2.png",selectedIconPath: "/public/image/icon_set1.png"}]

在 tabbar 有关的页面中:

onShow() {const app = getApp();const translations = app.globalData.translations;if (typeof this.getTabBar === 'function' && this.getTabBar()) {this.getTabBar().setData({list: translations.list});}
}

在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 100 Exercises To Learn Rust 挑战!准备篇
  • 掌握网络数据的钥匙:Python Requests-HTML库深度解析
  • AI秘境-墨小黑奇遇记 - 初体验(一)
  • GPT-4o mini发布,轻量级大模型如何颠覆AI的未来?
  • PCDN管道收益项目详谈二运营商资管图
  • 【idea】 已经commit还没push怎么修改commit信息?
  • C++引用产生临时量的情况
  • AI学习指南机器学习篇-多层感知器(MLP)Python中的实现和使用
  • Windows File Recovery卡在99%怎么解决?实用指南!
  • 鸿蒙面试题
  • Spring WebFlux 实现 SSE 流式回复:类GPT逐字显示回复效果完整指南
  • 施工窝工停工计算及索赔要点汇总
  • <数据集>集装箱缺陷识别数据集<目标检测>
  • 【MySQL】用户管理——用户、用户信息、创建用户、删除用户、修改用户密码、数据库的权限、给用户权限、回收权限
  • 0001 与会计的渊源
  • Bytom交易说明(账户管理模式)
  • conda常用的命令
  • ECMAScript6(0):ES6简明参考手册
  • ESLint简单操作
  • JavaScript HTML DOM
  • JAVA之继承和多态
  • vue从创建到完整的饿了么(18)购物车详细信息的展示与删除
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 湖南卫视:中国白领因网络偷菜成当代最寂寞的人?
  • 简单数学运算程序(不定期更新)
  • 免费小说阅读小程序
  • 前端性能优化--懒加载和预加载
  • 我感觉这是史上最牛的防sql注入方法类
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • ​LeetCode解法汇总2808. 使循环数组所有元素相等的最少秒数
  • ​Spring Boot 分片上传文件
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • ​业务双活的数据切换思路设计(下)
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (2)从源码角度聊聊Jetpack Navigator的工作流程
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (二刷)代码随想录第16天|104.二叉树的最大深度 559.n叉树的最大深度● 111.二叉树的最小深度● 222.完全二叉树的节点个数
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (简单) HDU 2612 Find a way,BFS。
  • (南京观海微电子)——I3C协议介绍
  • (七)理解angular中的module和injector,即依赖注入
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • (最完美)小米手机6X的Usb调试模式在哪里打开的流程
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET 中让 Task 支持带超时的异步等待
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .NET版Word处理控件Aspose.words功能演示:在ASP.NET MVC中创建MS Word编辑器
  • .net程序集学习心得