base.js

function getLanguage() {
  //返回缓存中的language属性 (en / zh_CN)
  return wx.getStorageSync('Language') || 'zh_CN'
};

function translate(){
  //返回翻译的对照信息
  return require('language/'+ getLanguage() + '.js').languageMap;
}

function translateTxt(desc){
  //翻译
  return translate()[desc] || '竟然没有翻译';
}

module.exports = {
  getLanguage: getLanguage,
  _t: translate,
  _: translateTxt,
}

language/en.js

const languageMap = {
  '蓝牙搜索':'bluetooth search',
}
module.exports = {
  languageMap : languageMap
}

app.js

 onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    if (this.globalData.base.getLanguage() == 'zh_CN') {
      this.globalData.language = 'English'
    } else {
      this.globalData.language = '中文'
    };
    this.globalData._t = this.globalData.base._t()
  },
  globalData: {
    language:'中文',
    base:require('./utils/base'),
}
03-05 20:36