9***@qq.com
9***@qq.com
  • 发布:2023-07-12 09:19
  • 更新:2023-08-04 10:16
  • 阅读:1024

pages.json 国际化不生效

分类:uni-app

按照官网的demo配置的国际化,vue页面中正常,但是pages.json中没有生效,直接就是字符串显示
如图


local文件夹路径


pages.json中 tabbar的配置

main.ts文件

import { createSSRApp } from "vue";  
import { createI18n } from "vue-i18n";  
import App from "./App.vue";  
import { en, zh } from '/locale';  
import uviewPlus from 'uview-plus';  

export type LocaleMessages_type = keyof typeof messages;  

const messages = {  
  en,  
  'zh-Hans': zh  
};  

const i18n = createI18n({  
  locale: uni.getLocale(), // 获取已经设置的语言  
  messages, // 翻译字典  
  legacy: false, // VUE3 中使用组合式API,必须设置为 false,  
  globalInjection: true // 全局注入 $t 函数  
});  

export function createApp() {  
  const app = createSSRApp(App);  
  app.use(i18n);  
  app.use(uviewPlus as any);  

  // 配置uview-plus内部的各项内置配置  
  uni.$u.setConfig({  
    // 修改$u.config对象的属性  
    config: {  
      // 修改默认单位为rpx,相当于执行 uni.$u.config.unit = 'rpx'  
      unit: 'rpx'  
    },  
    // 修改$u.props对象的属性  
    props: {  
      // 修改radio组件的size参数的默认值,相当于执行 uni.$u.props.radio.size = 30  
      radio: {  
        size: 15  
      }  
      // 其他组件属性配置  
      // ......  
    }  
  });  

  return { app };  
}  

zh-Hans.json文件

{  
  "A new way to make your air conditioner smarter": "让您的暖通空调更智能",  
  "GET STARTED": "开始",  
  "Tenant": "租户",  
  "Password": "密码",  
  "I have read and agree": "我已阅读并同意",  
  "User agreement": "用户协议",  
  "Privacy Policy": "隐私政策",  
  "Sign In": "登录",  
  "Sign Up": "注册",  
  "Please agree and check the User Agreement and Privacy Policy": "请同意并勾选用户协议、隐私政策",  
  "Please enter the correct tenant name": "请填写正确的租户名",  
  "Gateway SN": "网关序列号",  
  "Data value": "数值",  
  "Device name": "设备名称",  
  "Generation time": "发生时间",  
  "Message": "消息",  
  "Workbench": "工作台",  
  "Me": "我的",  
  "User Information": "用户信息",  
  "Change Password": "修改密码",  
  "Lite Mode": "精简模式",  
  "Setting": "设置",  
  "Log Out": "退出",  
  "End users can only enable it": "终端用户才可开启",  
  "User name": "用户名",  
  "Real name": "真实姓名",  
  "Gender": "性别",  
  "Phone number": "电话",  
  "Email": "邮箱",  
  "Last login time": "上次登录时间",  
  "Male": "男",  
  "Femal": "女",  
  "Old password": "旧密码",  
  "New password": "新密码",  
  "Repeat password": "再次确认密码",  
  "Confirm": "确认",  
  "Can not be empty": "不能为空",  
  "Old password is incorrect": "旧密码不正确",  
  "Password length is 8 to 16": "密码长度应在8到16位",  
  "Passwords do not match": "两次密码不一致",  
  "Change successfully": "修改成功",  
  "Change fail": "修改失败",  
  "Language": "语言",  
  "About us": "关于我们",  
  "Currently selected": "当前选中",  
  "Search keywords": "搜索关键字",  
  "All": "全部",  
  "Online": "在线",  
  "Offline": "离线",  
  "Gateway status": "网关状态",  
  "Alarm status": "告警状态",  
  "Device details": "设备详情",  
  "Control panel": "遥控器",  
  "Device alias": "设备别名",  
  "Device status": "设备状态",  
  "Residual flow": "剩余流量",  
  "Last register time": "最后注册时间",  
  "Last connect time": "最后联系时间",  
  "More": "更多",  
  "Installation information": "安装信息",  
  "Scheduled task": "定时任务",  
  "Control record": "控制记录",  
  "Alarm message": "告警消息",  
  "Temp": "温度",  
  "Wind scale": "风向",  
  "Humidity": "湿度",  
  "Data name": "数据项名称",  
  "Data alias": "数据项别名",  
  "Set value": "设置值",  
  "Update time": "更新时间",  
  "Run time": "运行时间",  
  "Edit": "编辑",  
  "Delete": "删除",  
  "Cancel": "取消",  
  "Confirm delete": "确认删除?",  
  "Data item": "数据项",  
  "Task name": "任务名称",  
  "Periodic": "周期执行",  
  "Enable": "是否运行",  
  "Periodic type": "周期类型",  
  "Everyday": "每天",  
  "Workday": "工作日",  
  "Custom": "自定义",  
  "Sunday": "周日",  
  "Monday": "周一",  
  "Tuesday": "周二",  
  "Wednesday": "周三",  
  "Thursday": "周四",  
  "Friday": "周五",  
  "Saturday": "周六",  
  "Select data item": "选择数据项",  
  "Filter data item": "过滤数据项",  
  "There are still contents not filled in": "还有内容未填写",  
  "Operator customer": "操作人",  
  "Starting time": "开始时间",  
  "Control result": "控制结果",  
  "No more data": "没有更多了",  
  "Success": "成功",  
  "Fail": "失败"  
}  
2023-07-12 09:19 负责人:DCloud_UNI_GSQ 分享
已邀请:
j***@163.com

j***@163.com - hello

请问解决了吗

2***@qq.com

2***@qq.com

官网:https://uniapp.dcloud.net.cn/tutorial/i18n.html#%E8%AF%AD%E8%A8%80%E4%BB%A3%E7%A0%81

项目根目录支持 locale 目录,locale/uni-app.语言地区代码.json,如:uni-app.en.json,uni-app.zh-Hans.json,uni-app.zh-Hant.json

我这边是将local放在了src下面,main.js文件中也没有引用,问题就解决了。

注意:

  1. 设置语言用uni.setLocale("zh-Hans"),这行代码我这边在main.js文件中使用会报错,可以放在App.vue中的onLaunch里面。
  2. json文件不能随意命名,zh-Hans.json不能写成zh.json等等(https://ask.dcloud.net.cn/question/140724)

App.vue代码

<script>  
    export default {  
        onLaunch: function() {  
                        // 我这边用$i18n.locale判断,请忽略,可以使用uni.getLocale()获取当前语言  
            if(this.$i18n.locale == 'zh'){  
                uni.setLocale('zh-Hans') // 使用这个切换语言  
            }else{  
                uni.setLocale('ja-JP')  
            }  

            console.log('App Launch')  
        },  
        onShow: function() {  
            console.log('App Show')  
        },  
        onHide: function() {  
            console.log('App Hide')  
        }  
    }  
</script>
  • j***@163.com

    您好,这样设置之后,页面的展示正常,tabbar需要单独写方法处理,是设置的有问题吗

    2023-08-04 11:44

要回复问题请先登录注册