LangUtils.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import zh from '../i18n/zh-CN.js'
  2. import en from '../i18n/en-US.js'
  3. import Constants from '../utils/Constants';
  4. module.exports = {
  5. //初始化语言设置。在 app.js 里调用这个方法。
  6. initLang() {
  7. //先获取是不是已存在语言的设置
  8. let lang = wx.getStorageSync('lang')
  9. if (!lang) {
  10. //如果不存在,设置默认语言为中文
  11. this.setLang(Constants.lang.langCN)
  12. }
  13. },
  14. //设置语言
  15. setLang(lang) {
  16. try {
  17. wx.setStorageSync('lang', lang)
  18. } catch (e) {
  19. }
  20. },
  21. //获取语言设置
  22. getLang() {
  23. try {
  24. let lang = wx.getStorageSync('lang')
  25. return lang;
  26. } catch (e) {
  27. }
  28. },
  29. //获取当前语言下的资源文件
  30. getLangSrc() {
  31. let lang = this.getLang();
  32. if (lang === Constants.lang.langCN) {
  33. return zh;
  34. } else if (lang === Constants.lang.langEN) {
  35. return en;
  36. } else {
  37. return zh;
  38. }
  39. },
  40. //设置 NavigationBarTitle
  41. setNavigationBarTitle(title) {
  42. wx.setNavigationBarTitle({
  43. title: title
  44. })
  45. },
  46. /**
  47. * 设置 tabBar。因为 tabBar 是在 app.json 里写死的,需要使用 wx.setTabBarItem
  48. * 循环设置 tabBar
  49. */
  50. setTabBarLang() {
  51. let tabBarTitles = this.getLangSrc().tabBarTitles;
  52. tabBarTitles.forEach((item, index) => {
  53. wx.setTabBarItem({
  54. index: index,
  55. text: item,
  56. success: (res) => {
  57. },
  58. fail: (err) => {
  59. }
  60. });
  61. });
  62. },
  63. }