zoukankan      html  css  js  c++  java
  • 微信小程序--中英文切换(2)

    需求:第一次打开时的小程序的语言为本机的系统语言,在小程序中进行语言设置后再次打开其语言保持设置不变。

    流程图

     

    1.获取本机系统设置

    wx.getSystemInfoSync()

    获取系统异步接口:

    try{
        wx.getSystemInfo({
          success: function(res) {
            console.log(res.model)
            console.log(res.pixelRatio)
            console.log(res.windowWidth)
            console.log(res.windowHeight)
            console.log(res.language)
            console.log(res.version)
            console.log(res.platform)
     
        } })
    }

    获取系统同步接口:

    try {
      var res = wx.getSystemInfoSync()
      console.log(res.model)
      console.log(res.pixelRatio)
      console.log(res.windowWidth)
      console.log(res.windowHeight)
      console.log(res.language)
      console.log(res.version)
      console.log(res.platform)
    } catch (e) {
      // Do something when catch error
    }

    在本文中只需获取language信息即可。console.log(key),用于在运行后console中查看key的内容信息。

    2.获取缓存信息

    wx.getStorageSync()

    从本地缓存中同步获取key对应的内容:

    try {
      var value = wx.getStorageSync('key')
      if (value) {
          // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }

    key是我们所需的缓存内容的名字。

    从本地缓存中异步获取key对应的内容:

    wx.getStorageInfo({
      success: function(res) {
        console.log(res.keys)
        console.log(res.currentSize)
        console.log(res.limitSize)
      }
    })

    因为语言设置为全局设置,所以设置放在app.js中,在页面的js中调用app.js。

    //third.js
    var
    app = getApp();

    源代码

    //app.js
    App({
    
      onLaunch: function (options) {
        //读取系统缓存
        var value = wx.getStorageSync('language')
    
        //系统缓存不存在
        if (value == "") {
                   var res = wx.getSystemInfoSync()  //读取手机信息
          this.language = res.language     // 这个地方用this
             }else if(value == "zh_CN"){
          console.log("系统缓存为'zh_CN'")  //系统缓存为"zh_CN"
              this.language = "zh_CN"
        }else{
               this.language = "zh_EN" //系统缓存为"zh_EN"
        }
      },
      language:""//全局变量
    }

    其他的语言设置部分同上节。

  • 相关阅读:
    进程详解
    java 实体对象与Map之间的转换工具类(自己还没看)
    fastjson中toString与toJSONString的差别
    JSONObject.toJSONString()包含或排除指定的属性
    FastJson中JSONObject用法及常用方法总结
    SpringBoot之ApplicationRunner接口和@Order注解
    shiro使用
    RedisTemplate 中 opsForHash()使用 (没有测试过,copy的)
    解决:javax.servlet.ServletException: Circular view path []: would dispatch back to the current....
    【Springboot】spring-boot-starter-redis包报错 :unknown
  • 原文地址:https://www.cnblogs.com/cff2121/p/9325874.html
Copyright © 2011-2022 走看看