index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import enUS from './en-US.json'
  2. import zhCN from './zh-Hans.json'
  3. import zhHK from './zh-HK.json'
  4. import arSA from './ar-SA.json'
  5. import deDE from './de-DE.json'
  6. import esES from './es-ES.json'
  7. import frFR from './fr-FR.json'
  8. import jaJP from './ja-JP.json'
  9. import koKR from './ko-KR.json'
  10. import ruRU from './ru-RU.json'
  11. // #ifdef VUE2
  12. import VueI18n from 'vue-i18n'
  13. // #endif
  14. // #ifdef VUE3
  15. import { createI18n as createI18nVue3 } from 'vue-i18n'
  16. const VueI18n = {}
  17. // #endif
  18. function $t(key){
  19. return zhCN[key]
  20. }
  21. function createI18n(config) {
  22. const defaultConfig = {
  23. locale: 'zh-CN', // 默认显示语言
  24. fallbackLocale: 'en-US',
  25. messages: {
  26. 'zh-Hans': zhCN, //简体中文
  27. 'zh-HK': zhHK, //繁体中文(香港)
  28. 'en-US': enUS, //英文
  29. 'ja-JP': jaJP, //日文
  30. 'ko-KR': koKR, //韩文
  31. 'fr-FR': frFR, //法文
  32. 'de-DE': deDE, //德文
  33. 'es-ES': esES, //西班牙文
  34. 'ru-RU': ruRU, //俄文
  35. 'ar-SA': arSA, //阿拉伯文
  36. }
  37. }
  38. config = uni.$u.deepMerge(defaultConfig, config)
  39. let i18n = null
  40. // #ifdef VUE2
  41. i18n = new VueI18n(config)
  42. uni.$u.$t = i18n.t.bind(i18n)
  43. // #endif
  44. // #ifdef VUE3
  45. i18n = createI18nVue3(config)
  46. uni.$u.$t = i18n.global.t.bind(i18n.global)
  47. // #endif
  48. uni.$u.setLocale = (locale) => {
  49. // #ifdef VUE2
  50. i18n.locale = locale
  51. // #endif
  52. // #ifdef VUE3
  53. i18n.global.locale = locale
  54. // #endif
  55. uni.setStorageSync('locale', locale)
  56. }
  57. return i18n
  58. }
  59. export {
  60. $t,
  61. VueI18n,
  62. createI18n
  63. }