Explorar o código

1、完成国际化语言

zhoux %!s(int64=2) %!d(string=hai) anos
pai
achega
c5a18e9d28
Modificáronse 8 ficheiros con 170 adicións e 8 borrados
  1. 11 1
      app.js
  2. 24 0
      i18n/en-US.js
  3. 23 0
      i18n/zh-CN.js
  4. 25 5
      mixins/index.js
  5. 9 0
      utils/Constants.js
  6. 76 0
      utils/LangUtils.js
  7. 1 1
      utils/common.js
  8. 1 1
      utils/util.js

+ 11 - 1
app.js

@@ -11,6 +11,8 @@ import './api/pages/core/user.js';
 const util = require('./utils/util.js')
 const baseMethod = require('./api/pages/baseMethod.js')
 const baseRoute = require('./api/pages/baseRoute.js')
+//初始化国际化语言设置
+const LangUtils = require( './utils/LangUtils')
 
 // 引入文件系统管理器模块
 const fs = wx.getFileSystemManager();
@@ -42,9 +44,13 @@ function readJsFiles(dirPath) {
   }
   return returnFiles;
 }
+function setRouteService(){
+  
+}
 App({
   onLaunch() { 
     console.log("onLaunch");
+    // LangUtils.setTabBarLang();
   },
   globalData: {
     userInfo: null,
@@ -67,7 +73,6 @@ try {
     }else{
       util.objectMergeByMainJs(m['routeUrl'][m.name],baseRoute.routeUrl)
     }
-
     if(m.name != 'common'){
       routeUrl[m.name] = m['routeUrl'][m.name];
     }
@@ -75,6 +80,11 @@ try {
   app.globalData['routeUrl'] = routeUrl;
   // 输出结果
   console.log('文件列表:', files,baseMethod,routeUrl);
+  // 国际化的初始化
+  LangUtils.initLang();
+  let langSrc = LangUtils.getLangSrc()
+  console.log('langSrc',langSrc);
+  app.globalData['lang'] = {...langSrc.items};
 } catch (error) {
   console.error('读取文件夹失败:', error);
 }

+ 24 - 0
i18n/en-US.js

@@ -0,0 +1,24 @@
+/**
+  * 导出常量
+  */
+module.exports = {  
+  //一页条数
+  PAGE_SIZE : 5,
+  // 成功的code
+  SUCESS_CODE : 200,
+  //页面加载数据API
+  BASIC_COMMON_API : "mdm-server/wxapi/basic/common/",
+  // 最大的数量 
+  Mess_Max_Quantity : '超过最大值或最小值,最大值为9999999999.999999',
+  // 手机号正则表达式 目前只有位数校验(11位)
+  Mobile_Phone_Number_Regular_Expression : /^[0-9][0-9][0-9]{9}$/,
+  // 提示信息
+  Messages:{
+    save_sucess:'保存成功',
+  },
+ 
+  //销售订单相关API
+  SALE_ORDER_API : "order-server/wxapi/sale/order/",
+  WARE_HOUSE : "mdm-server/mst/warehouse/",
+  PAGE_ITEM_MUST_API : "basic-server/basic/pageItemMust",
+};

+ 23 - 0
i18n/zh-CN.js

@@ -0,0 +1,23 @@
+// 按钮
+const button = {
+ add:'新建'
+}
+
+// 列
+const columns = {
+  test:'测试'
+}
+
+// 提示信息
+const messages = {
+
+}
+
+// 其他信息
+const others = {
+
+}
+
+module.exports  = {
+  items:Object.assign({},columns,button,messages,others)
+}

+ 25 - 5
mixins/index.js

@@ -32,6 +32,19 @@ module.exports = {
         params:null,
         // 查询结果集
         tableData:[],
+        $t:app.globalData.lang,
+    },
+    /**
+     * @desc : 获取语言的方法
+     * @author : 周兴
+     * @date : 2024/1/19
+     */
+    $t(name){
+        if(this.data.$t[name]){
+            return this.data.$t[name];
+        }else{
+            return name;
+        }
     },
     /**
      * @desc : 查询共通方法
@@ -123,19 +136,19 @@ module.exports = {
      */
     open(e){
         // 如果没有路由就不往下进行
-        if(!this.routeObjName)return;
+        if(!this.data.routeObjName)return;
         let name;
         let item;
         if(typeof e === 'object'){
             name = e.currentTarget.dataset.name;
             item = e.currentTarget.dataset.item;
-            if(this.primaryKey){
-                item.id = item[this.primaryKey]
+            if(this.data.primaryKey){
+                item.id = item[this.data.primaryKey]
             }
         }else{
             name = e;
         }
-        let routeUrl = app.globalData['routeUrl'][this.routeObjName][name];
+        let routeUrl = app.globalData['routeUrl'][this.data.routeObjName][name];
         if(!routeUrl)return;
         // 如果有链接,就跳转
         // console.log('url',routeUrl);
@@ -164,7 +177,9 @@ module.exports = {
             if (!res) {
               return;
             }
-            this.loading = true;
+            this.setData({
+                loading:true
+            })
             // 设置参数
             this._setParams();
             // 如果需要有询问
@@ -298,6 +313,11 @@ module.exports = {
             }
         }
     },
+     /**
+     * @desc : 生命周期函数(onLoad)
+     * @author : 周兴
+     * @date : 2024/1/22
+     */
     onLoad() {
         // this.searchData();
     },

+ 9 - 0
utils/Constants.js

@@ -16,6 +16,15 @@ module.exports = {
   Messages:{
     save_sucess:'保存成功',
   },
+
+  lang:{
+    //中文编码
+    langCN: 'zh-CN',
+    //英文编码
+    langEN: 'en-US',
+  },
+
+ 
  
   //销售订单相关API
   SALE_ORDER_API : "order-server/wxapi/sale/order/",

+ 76 - 0
utils/LangUtils.js

@@ -0,0 +1,76 @@
+import zh from '../i18n/zh-CN.js'
+import en from '../i18n/en-US.js'
+import Constants from '../utils/Constants';
+
+module.exports = {
+
+  //初始化语言设置。在 app.js 里调用这个方法。
+  initLang() {
+    //先获取是不是已存在语言的设置
+    let lang = wx.getStorageSync('lang')
+    if (!lang) {
+      //如果不存在,设置默认语言为中文
+      this.setLang(Constants.lang.langCN)
+    }
+  },
+
+  //设置语言
+  setLang(lang) {
+    try {
+      wx.setStorageSync('lang', lang)
+    } catch (e) {
+      console.log('设置语言失败', e)
+    }
+  },
+
+  //获取语言设置
+  getLang() {
+    try {
+      let lang = wx.getStorageSync('lang')
+      return lang;
+    } catch (e) {
+      console.log('获取语言设置失败', e)
+    }
+  },
+
+  //获取当前语言下的资源文件
+  getLangSrc() {
+    let lang = this.getLang();
+    if (lang === Constants.lang.langCN) {
+      return zh;
+    } else if (lang === Constants.lang.langEN) {
+      return en;
+    } else {
+      return zh;
+    }
+  },
+
+  //设置 NavigationBarTitle
+  setNavigationBarTitle(title) {
+    wx.setNavigationBarTitle({
+      title: title
+    })
+  },
+
+  /**
+  * 设置 tabBar。因为 tabBar 是在 app.json 里写死的,需要使用 wx.setTabBarItem
+  * 循环设置 tabBar
+  */
+  setTabBarLang() {
+    let tabBarTitles = this.getLangSrc().tabBarTitles;
+    console.log('tabBarTitles', tabBarTitles)
+    tabBarTitles.forEach((item, index) => {
+      console.log(item, index)
+      wx.setTabBarItem({
+        index: index,
+        text: item,
+        success: (res) => {
+          console.log('setTabBarItem success', res)
+        },
+        fail: (err) => {
+          console.log('setTabBarItem fail', err)
+        }
+      });
+    });
+  },
+}

+ 1 - 1
utils/common.js

@@ -9,7 +9,7 @@
  *******************************************************************************/
 var QQMapWX = require('./qqmap-wx-jssdk.min');
 const config = require('../config/config.js');
-const Constants = require('./Constants.js');
+const Constants = require('../utils/Constants.js');
 const util = require('./util');
 
 const app = getApp();

+ 1 - 1
utils/util.js

@@ -1,4 +1,4 @@
-const Constants = require('./Constants.js')
+const Constants = require('../utils/Constants.js')
 
 // 转换时间
 const getDate = (year, month, day, hour, minute) => {