WXL
2 天以前 9bce51f651aad297ef9eb6df832bfdaf1de05d84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { createI18n } from 'vue-i18n'
import zhHans from './locale/zh-Hans'
import zhHant from './locale/zh-Hant'
import pt from './locale/pt'
 
const i18n = createI18n({
  legacy: false,
  globalInjection: true,
  locale: uni.getStorageSync('language') || 'zh-Hans',
  fallbackLocale: 'zh-Hans',
  messages: {
    'zh-Hans': zhHans,
    'zh-Hant': zhHant,
    'pt': pt
  },
  missing: (locale, key) => {
    console.warn(`[i18n] Missing translation key: "${key}" for locale: "${locale}"`)
    return key
  },
  fallbackWarn: false,
  missingWarn: process.env.NODE_ENV === 'development'
})
 
export function setLanguage(locale) {
  i18n.global.locale.value = locale
  uni.setStorageSync('language', locale)
  
  const tabBarTexts = [
    i18n.global.t('tabbar.home'),
    i18n.global.t('tabbar.appointment'),
    i18n.global.t('tabbar.my')
  ]
  
  tabBarTexts.forEach((text, index) => {
    uni.setTabBarItem({
      index,
      text
    })
  })
  
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  if (currentPage?.route) {
    const titleKey = currentPage.$page?.navigationBarTitleText
    if (titleKey) {
      uni.setNavigationBarTitle({
        title: i18n.global.t(titleKey)
      })
    }
  }
}
 
export default i18n