vue-i18n.ts 2.8 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { I18n, BuiltInLocale, LocaleMessages, LOCALE_EN } from './I18n'
fxy060608's avatar
fxy060608 已提交
2

Q
qiang 已提交
3 4
const ignoreVueI18n = true

fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10
type Interpolate = (
  key: string,
  values?: Record<string, unknown> | Array<unknown>
) => string

function initLocaleWatcher(appVm: any, i18n: I18n) {
Q
qiang 已提交
11 12 13 14
  if (appVm.$i18n) {
    const vm = appVm.$i18n.vm ? appVm.$i18n.vm : appVm
    vm.$watch(
      appVm.$i18n.vm ? 'locale' : () => appVm.$i18n.locale,
fxy060608's avatar
fxy060608 已提交
15 16 17 18 19 20 21
      (newLocale: BuiltInLocale) => {
        i18n.setLocale(newLocale)
      },
      {
        immediate: true,
      }
    )
Q
qiang 已提交
22
  }
fxy060608's avatar
fxy060608 已提交
23 24
}

fxy060608's avatar
fxy060608 已提交
25 26 27 28 29 30 31 32 33 34
// function getDefaultLocale() {
//   if (typeof navigator !== 'undefined') {
//     return (navigator as any).userLanguage || navigator.language
//   }
//   if (typeof plus !== 'undefined') {
//     // TODO 待调整为最新的获取语言代码
//     return plus.os.language
//   }
//   return uni.getSystemInfoSync().language
// }
fxy060608's avatar
fxy060608 已提交
35 36

export function initVueI18n(
fxy060608's avatar
fxy060608 已提交
37
  locale: BuiltInLocale = LOCALE_EN,
fxy060608's avatar
fxy060608 已提交
38
  messages: LocaleMessages = {},
Q
qiang 已提交
39 40
  fallbackLocale: BuiltInLocale = LOCALE_EN,
  watcher?: (locale: BuiltInLocale) => void
fxy060608's avatar
fxy060608 已提交
41
) {
42 43 44 45
  // 兼容旧版本入参
  if (typeof locale !== 'string') {
    ;[locale, messages] = [messages as BuiltInLocale, locale as LocaleMessages]
  }
fxy060608's avatar
fxy060608 已提交
46 47 48
  if (typeof locale !== 'string') {
    locale = fallbackLocale
  }
fxy060608's avatar
fxy060608 已提交
49 50 51 52
  const i18n = new I18n({
    locale: locale || fallbackLocale,
    fallbackLocale,
    messages,
Q
qiang 已提交
53
    watcher,
fxy060608's avatar
fxy060608 已提交
54 55 56
  })
  let t: Interpolate = (key, values) => {
    if (typeof getApp !== 'function') {
fxy060608's avatar
fxy060608 已提交
57
      // app view
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63
      /* eslint-disable no-func-assign */
      t = function (key, values) {
        return i18n.t(key, values)
      }
    } else {
      const appVm = getApp().$vm
Q
qiang 已提交
64
      if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
fxy060608's avatar
fxy060608 已提交
65 66 67
        // if (!locale) {
        //   i18n.setLocale(getDefaultLocale())
        // }
fxy060608's avatar
fxy060608 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        /* eslint-disable no-func-assign */
        t = function (key, values) {
          return i18n.t(key, values)
        }
      } else {
        initLocaleWatcher(appVm, i18n)
        /* eslint-disable no-func-assign */
        t = function (key, values) {
          const $i18n = appVm.$i18n
          const silentTranslationWarn = $i18n.silentTranslationWarn
          $i18n.silentTranslationWarn = true
          const msg = appVm.$t(key, values)
          $i18n.silentTranslationWarn = silentTranslationWarn
          if (msg !== key) {
            return msg
          }
          return i18n.t(key, $i18n.locale, values)
        }
      }
    }
    return t(key, values)
  }
  return {
fxy060608's avatar
fxy060608 已提交
91
    i18n,
fxy060608's avatar
fxy060608 已提交
92 93 94
    t(key: string, values?: Record<string, unknown> | Array<unknown>) {
      return t(key, values)
    },
fxy060608's avatar
fxy060608 已提交
95 96 97
    add(locale: BuiltInLocale, message: Record<string, string>) {
      return i18n.add(locale, message)
    },
fxy060608's avatar
fxy060608 已提交
98 99 100 101 102 103 104 105
    getLocale() {
      return i18n.getLocale()
    },
    setLocale(newLocale: BuiltInLocale) {
      return i18n.setLocale(newLocale)
    },
  }
}