Skip to content

Commit

Permalink
fix(mp-weixin): 修复游客模式下调用 getAppBaseInfo 报错的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
GRCmade committed Jan 20, 2025
1 parent 9d2574e commit 0cb5b81
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 2 additions & 4 deletions packages/uni-mp-core/src/api/locale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LOCALE_EN, normalizeLocale } from '@dcloudio/uni-i18n'
import { getLocaleLanguage } from '../runtime/util'
import { isFunction } from '@vue/shared'

export const getLocale: typeof uni.getLocale = () => {
Expand All @@ -7,9 +7,7 @@ export const getLocale: typeof uni.getLocale = () => {
if (app && app.$vm) {
return app.$vm.$locale
}
return __PLATFORM__ === 'mp-weixin'
? normalizeLocale(__GLOBAL__.getAppBaseInfo().language) || LOCALE_EN
: normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
return getLocaleLanguage()
}

export const setLocale: typeof uni.setLocale = (locale) => {
Expand Down
8 changes: 2 additions & 6 deletions packages/uni-mp-core/src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {

import { initBaseInstance } from './componentInstance'
import { initHooks, initUnknownHooks } from './componentHooks'
import { LOCALE_EN, normalizeLocale } from '@dcloudio/uni-i18n'
import { getLocaleLanguage } from '../runtime/util'

import App = WechatMiniprogram.App
import {
Expand Down Expand Up @@ -169,11 +169,7 @@ export function initAppLifecycle(
}

function initLocale(appVm: ComponentPublicInstance) {
const locale = ref<string>(
__PLATFORM__ === 'mp-weixin'
? normalizeLocale(__GLOBAL__.getAppBaseInfo().language) || LOCALE_EN
: normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
)
const locale = ref<string>(getLocaleLanguage())
Object.defineProperty(appVm, '$locale', {
get() {
return locale.value
Expand Down
16 changes: 15 additions & 1 deletion packages/uni-mp-core/src/runtime/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type ComponentPublicInstance,
nextTick,
} from 'vue'

import { LOCALE_EN, normalizeLocale } from '@dcloudio/uni-i18n'
import type { MPComponentInstance, MPComponentOptions } from './component'

export function initVueIds(
Expand Down Expand Up @@ -228,3 +228,17 @@ export function getTriggerEventDetail(eventId: number) {
delete triggerEventDetails[eventId]
return detail
}

export function getLocaleLanguage() {
let localeLanguage = ''
if (__PLATFORM__ === 'mp-weixin') {
const appBaseInfo = __GLOBAL__.getAppBaseInfo()
const language =
appBaseInfo && appBaseInfo.language ? appBaseInfo.language : LOCALE_EN
localeLanguage = normalizeLocale(language) || LOCALE_EN
} else {
localeLanguage =
normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
}
return localeLanguage
}

0 comments on commit 0cb5b81

Please sign in to comment.