diff --git a/packages/uni-cloud/lib/uni.plugin.js b/packages/uni-cloud/lib/uni.plugin.js index 164b228d178e0a22b9a9b37618c97d5145dfd3a1..95fb7f8371aef7d9cd3489adc2732747f06518a5 100644 --- a/packages/uni-cloud/lib/uni.plugin.js +++ b/packages/uni-cloud/lib/uni.plugin.js @@ -67,7 +67,7 @@ function initUniCloudEnv() { process.env.UNI_CLOUD_PROVIDER = JSON.stringify(uniCloudSpaces.map((space) => { if (space.clientSecret) { return { - provider: 'aliyun', + provider: space.provider || 'aliyun', spaceName: space.name, spaceId: space.id, clientSecret: space.clientSecret, @@ -76,7 +76,7 @@ function initUniCloudEnv() { } else { return { - provider: 'tencent', + provider: space.provider || 'tencent', spaceName: space.name, spaceId: space.id, }; diff --git a/packages/uni-mp-alipay/build.json b/packages/uni-mp-alipay/build.json index 5b510d97c0483ea23552c0c5647dc97267e6dfe9..b15ef80001cf2d93cb75884ec4a8497a9a24cee6 100644 --- a/packages/uni-mp-alipay/build.json +++ b/packages/uni-mp-alipay/build.json @@ -19,5 +19,6 @@ "__GLOBAL__": "my", "__PLATFORM__": "\"mp-alipay\"", "__PLATFORM_TITLE__": "支付宝小程序" - } + }, + "external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"] } diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index b62bbbb2e691ca24b8a84dc02027827e340a8169..b319981ff37174b1434b8eb6a3d0c24af60e0397 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -49,8 +49,8 @@ function onAppLaunch(hook) { my.appLaunchHooks.push(hook); } -function getBaseSystemInfo() { - return my.getSystemInfoSync() +function getBaseSystemInfo() { + return my.getSystemInfoSync() } function validateProtocolFail(name, msg) { @@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { emitter.emit(name, ...args); }, EmitProtocol); -const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; +const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; // Context例外情况 const CONTEXT_API_RE_EXC = ['createBLEConnection']; @@ -727,6 +727,30 @@ function initWrapper(protocols) { }; } +const getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }); + if (app && app.$vm) { + return app.$vm.$locale; + } + return uni.getSystemInfoSync().language || 'zh-Hans'; +}; +const setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale; + if (oldLocale !== locale) { + getApp().$vm.$locale = locale; + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })); + return true; + } + return false; +}; +const onLocaleChangeCallbacks = []; +const onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn); + } +}; + const baseApis = { $on, $off, @@ -736,6 +760,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, }; function initUni(api, protocols) { const wrapper = initWrapper(protocols); diff --git a/packages/uni-mp-alipay/dist/uni.mp.esm.js b/packages/uni-mp-alipay/dist/uni.mp.esm.js index a04f9acf31ce9e3a510bc1d0db42757cc7d7fb36..93b8781bc5c7c0daa6060a2ae918a4729603d111 100644 --- a/packages/uni-mp-alipay/dist/uni.mp.esm.js +++ b/packages/uni-mp-alipay/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, isArray, extend, hyphenate, isObject, hasOwn, toNumber, capitalize, isFunction, NOOP, EMPTY_OBJ, camelize } from '@vue/shared'; -import { onUnmounted, injectHook } from 'vue'; +import { onUnmounted, injectHook, ref } from 'vue'; const encode = encodeURIComponent; function stringifyQuery(obj, encodeStr = encode) { @@ -578,6 +578,7 @@ function parseApp(instance, parseAppOptions) { instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); }, }; + initLocale(instance); const vueOptions = instance.$.type; initHooks(appOptions, HOOKS); initUnknownHooks(appOptions, vueOptions); @@ -594,6 +595,17 @@ function initCreateApp(parseAppOptions) { return function createApp(vm) { return App(parseApp(vm, parseAppOptions)); }; +} +function initLocale(appVm) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans'); + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value; + }, + set(v) { + locale.value = v; + }, + }); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; diff --git a/packages/uni-mp-baidu/build.json b/packages/uni-mp-baidu/build.json index 98fdcedda7e8304d30993709129f76eb473d89bb..0dc8d8e5587a7350061863fcd302a52c04f1e8a0 100644 --- a/packages/uni-mp-baidu/build.json +++ b/packages/uni-mp-baidu/build.json @@ -19,5 +19,6 @@ "__GLOBAL__": "swan", "__PLATFORM__": "\"mp-baidu\"", "__PLATFORM_TITLE__": "百度小程序" - } + }, + "external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"] } diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index bebc81822fd5824d3b887812c61ff6bcf3959b4c..65a5f130802dc46a77815bb876600cab06d8fa3e 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -49,8 +49,8 @@ function onAppLaunch(hook) { swan.appLaunchHooks.push(hook); } -function getBaseSystemInfo() { - return swan.getSystemInfoSync() +function getBaseSystemInfo() { + return swan.getSystemInfoSync() } function validateProtocolFail(name, msg) { @@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { emitter.emit(name, ...args); }, EmitProtocol); -const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; +const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; // Context例外情况 const CONTEXT_API_RE_EXC = ['createBLEConnection']; @@ -727,6 +727,30 @@ function initWrapper(protocols) { }; } +const getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }); + if (app && app.$vm) { + return app.$vm.$locale; + } + return uni.getSystemInfoSync().language || 'zh-Hans'; +}; +const setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale; + if (oldLocale !== locale) { + getApp().$vm.$locale = locale; + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })); + return true; + } + return false; +}; +const onLocaleChangeCallbacks = []; +const onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn); + } +}; + const baseApis = { $on, $off, @@ -736,6 +760,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, }; function initUni(api, protocols) { const wrapper = initWrapper(protocols); diff --git a/packages/uni-mp-baidu/dist/uni.mp.esm.js b/packages/uni-mp-baidu/dist/uni.mp.esm.js index 52e50249108a3fc723ac86112124e834549b19b6..bcaa5f73461c7f7f62e4e01497ee05d857fa609d 100644 --- a/packages/uni-mp-baidu/dist/uni.mp.esm.js +++ b/packages/uni-mp-baidu/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; -import { onUnmounted, injectHook } from 'vue'; +import { onUnmounted, injectHook, ref } from 'vue'; const encode = encodeURIComponent; function stringifyQuery(obj, encodeStr = encode) { @@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) { instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); }, }; + initLocale(instance); const vueOptions = instance.$.type; initHooks(appOptions, HOOKS); initUnknownHooks(appOptions, vueOptions); @@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) { return function createApp(vm) { return App(parseApp(vm, parseAppOptions)); }; +} +function initLocale(appVm) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans'); + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value; + }, + set(v) { + locale.value = v; + }, + }); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; diff --git a/packages/uni-mp-core/src/api/index.ts b/packages/uni-mp-core/src/api/index.ts index 2649cfba1dd3af890403f163a8fb254d4bb8183e..6b92944ba6d92a1c316e299af948c2f26b17f9e8 100644 --- a/packages/uni-mp-core/src/api/index.ts +++ b/packages/uni-mp-core/src/api/index.ts @@ -16,6 +16,7 @@ import { initWrapper } from './wrapper' import { MPProtocols } from './protocols' import { onAppLaunch } from './hook' +import { getLocale, setLocale, onLocaleChange } from './locale' const baseApis = { $on, @@ -26,6 +27,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, } export function initUni(api: Record, protocols: MPProtocols) { diff --git a/packages/uni-mp-core/src/api/locale.ts b/packages/uni-mp-core/src/api/locale.ts new file mode 100644 index 0000000000000000000000000000000000000000..14165d6394899c209fd40bda64144dd175cd4faf --- /dev/null +++ b/packages/uni-mp-core/src/api/locale.ts @@ -0,0 +1,26 @@ +export const getLocale: typeof uni.getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }) + if (app && app.$vm) { + return app.$vm.$locale + } + return uni.getSystemInfoSync().language || 'zh-Hans' +} + +export const setLocale: typeof uni.setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale + if (oldLocale !== locale) { + getApp().$vm.$locale = locale + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })) + return true + } + return false +} + +type OnLocaleCHangeCallback = Parameters[0] +const onLocaleChangeCallbacks: OnLocaleCHangeCallback[] = [] +export const onLocaleChange: typeof uni.onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn) + } +} diff --git a/packages/uni-mp-core/src/api/promise.ts b/packages/uni-mp-core/src/api/promise.ts index 03463e0e7ba0a95ffdb9301404548b05a6964e09..2caa3a6c0c50a6f7c1e8f8975191f368f8f32281 100644 --- a/packages/uni-mp-core/src/api/promise.ts +++ b/packages/uni-mp-core/src/api/promise.ts @@ -7,7 +7,7 @@ import { } from '@dcloudio/uni-api/src/helpers/interceptor' const SYNC_API_RE = - /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/ + /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/ const CONTEXT_API_RE = /^create|Manager$/ diff --git a/packages/uni-mp-core/src/runtime/app.ts b/packages/uni-mp-core/src/runtime/app.ts index 37543b1e3288332524547c429df9f3e09d35d520..42d20898d41f51f300c13a2ad4c140091ae5272c 100644 --- a/packages/uni-mp-core/src/runtime/app.ts +++ b/packages/uni-mp-core/src/runtime/app.ts @@ -1,5 +1,5 @@ import { extend } from '@vue/shared' -import { ComponentPublicInstance, ComponentOptions } from 'vue' +import { ComponentPublicInstance, ComponentOptions, ref } from 'vue' import { initBaseInstance } from './componentInstance' import { initHooks, initUnknownHooks } from './componentHooks' @@ -65,6 +65,8 @@ function parseApp( }, } + initLocale(instance) + const vueOptions = instance.$.type as ComponentOptions initHooks(appOptions, HOOKS) @@ -87,3 +89,15 @@ export function initCreateApp(parseAppOptions?: ParseAppOptions) { return App(parseApp(vm, parseAppOptions)) } } + +function initLocale(appVm: ComponentPublicInstance) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans') + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value + }, + set(v) { + locale.value = v + }, + }) +} diff --git a/packages/uni-mp-kuaishou/build.json b/packages/uni-mp-kuaishou/build.json index 2110c8cf8e44c2edfcea9773800a9e49087fa159..7627da9a95802306557694a11621caf4af53e519 100644 --- a/packages/uni-mp-kuaishou/build.json +++ b/packages/uni-mp-kuaishou/build.json @@ -19,5 +19,6 @@ "__GLOBAL__": "ks", "__PLATFORM__": "\"mp-kuaishou\"", "__PLATFORM_TITLE__": "快手小程序" - } + }, + "external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"] } diff --git a/packages/uni-mp-kuaishou/dist/uni.api.esm.js b/packages/uni-mp-kuaishou/dist/uni.api.esm.js index a71abe19788af49d6b83156ca0e48259ae6d5bfe..7ad77fc218800258db99e27aa250920f139b2c8a 100644 --- a/packages/uni-mp-kuaishou/dist/uni.api.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.api.esm.js @@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { emitter.emit(name, ...args); }, EmitProtocol); -const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; +const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; // Context例外情况 const CONTEXT_API_RE_EXC = ['createBLEConnection']; @@ -727,6 +727,30 @@ function initWrapper(protocols) { }; } +const getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }); + if (app && app.$vm) { + return app.$vm.$locale; + } + return uni.getSystemInfoSync().language || 'zh-Hans'; +}; +const setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale; + if (oldLocale !== locale) { + getApp().$vm.$locale = locale; + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })); + return true; + } + return false; +}; +const onLocaleChangeCallbacks = []; +const onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn); + } +}; + const baseApis = { $on, $off, @@ -736,6 +760,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, }; function initUni(api, protocols) { const wrapper = initWrapper(protocols); diff --git a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js index 860f9bf9a464c76be791b18c482d08f5d0116950..87ce3029afc74970e1a4da125b96c42db213e403 100644 --- a/packages/uni-mp-kuaishou/dist/uni.mp.esm.js +++ b/packages/uni-mp-kuaishou/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; -import { onUnmounted, injectHook } from 'vue'; +import { onUnmounted, injectHook, ref } from 'vue'; const encode = encodeURIComponent; function stringifyQuery(obj, encodeStr = encode) { @@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) { instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); }, }; + initLocale(instance); const vueOptions = instance.$.type; initHooks(appOptions, HOOKS); initUnknownHooks(appOptions, vueOptions); @@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) { return function createApp(vm) { return App(parseApp(vm, parseAppOptions)); }; +} +function initLocale(appVm) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans'); + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value; + }, + set(v) { + locale.value = v; + }, + }); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; diff --git a/packages/uni-mp-qq/build.json b/packages/uni-mp-qq/build.json index 58b9656a12c41708622f957de9a7860a29cc1598..c965be3a02cd52e27545951252fe1db1d87f04b9 100644 --- a/packages/uni-mp-qq/build.json +++ b/packages/uni-mp-qq/build.json @@ -19,5 +19,6 @@ "__GLOBAL__": "qq", "__PLATFORM__": "\"mp-qq\"", "__PLATFORM_TITLE__": "QQ小程序" - } + }, + "external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"] } diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index 6ee33ac8fabbf3bfbc3b7018e0193af230522a1e..9941b76b8dc205554573e3463cee83a4034a4f96 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { emitter.emit(name, ...args); }, EmitProtocol); -const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; +const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; // Context例外情况 const CONTEXT_API_RE_EXC = ['createBLEConnection']; @@ -727,6 +727,30 @@ function initWrapper(protocols) { }; } +const getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }); + if (app && app.$vm) { + return app.$vm.$locale; + } + return uni.getSystemInfoSync().language || 'zh-Hans'; +}; +const setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale; + if (oldLocale !== locale) { + getApp().$vm.$locale = locale; + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })); + return true; + } + return false; +}; +const onLocaleChangeCallbacks = []; +const onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn); + } +}; + const baseApis = { $on, $off, @@ -736,6 +760,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, }; function initUni(api, protocols) { const wrapper = initWrapper(protocols); diff --git a/packages/uni-mp-qq/dist/uni.mp.esm.js b/packages/uni-mp-qq/dist/uni.mp.esm.js index ea73fc1e344f37b13cbe0e2c3772d257b0b5d7a5..d21075e70b357f3097c614fa030aed791f0e4d76 100644 --- a/packages/uni-mp-qq/dist/uni.mp.esm.js +++ b/packages/uni-mp-qq/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; -import { onUnmounted, injectHook } from 'vue'; +import { onUnmounted, injectHook, ref } from 'vue'; const encode = encodeURIComponent; function stringifyQuery(obj, encodeStr = encode) { @@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) { instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); }, }; + initLocale(instance); const vueOptions = instance.$.type; initHooks(appOptions, HOOKS); initUnknownHooks(appOptions, vueOptions); @@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) { return function createApp(vm) { return App(parseApp(vm, parseAppOptions)); }; +} +function initLocale(appVm) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans'); + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value; + }, + set(v) { + locale.value = v; + }, + }); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; diff --git a/packages/uni-mp-toutiao/build.json b/packages/uni-mp-toutiao/build.json index c85f026cfd26d9215f65c44658bb94417dda04e8..0ba5fa0ad22df1944d2c20be22be1ffe3a0a6167 100644 --- a/packages/uni-mp-toutiao/build.json +++ b/packages/uni-mp-toutiao/build.json @@ -19,5 +19,6 @@ "__GLOBAL__": "tt", "__PLATFORM__": "\"mp-toutiao\"", "__PLATFORM_TITLE__": "字节跳动小程序" - } + }, + "external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"] } diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index 758ac002d89bcab69f3aa9d501b5a8626c7f1ef9..bae0f52686fa972c905adc09750812cab13dbb4e 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { emitter.emit(name, ...args); }, EmitProtocol); -const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; +const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; // Context例外情况 const CONTEXT_API_RE_EXC = ['createBLEConnection']; @@ -727,6 +727,30 @@ function initWrapper(protocols) { }; } +const getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }); + if (app && app.$vm) { + return app.$vm.$locale; + } + return uni.getSystemInfoSync().language || 'zh-Hans'; +}; +const setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale; + if (oldLocale !== locale) { + getApp().$vm.$locale = locale; + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })); + return true; + } + return false; +}; +const onLocaleChangeCallbacks = []; +const onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn); + } +}; + const baseApis = { $on, $off, @@ -736,6 +760,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, }; function initUni(api, protocols) { const wrapper = initWrapper(protocols); diff --git a/packages/uni-mp-toutiao/dist/uni.mp.esm.js b/packages/uni-mp-toutiao/dist/uni.mp.esm.js index c8cc1366d384df7113c9316b77649037496e8d66..de97b679926b8b5a4763255ca9966a4f1e51aa84 100644 --- a/packages/uni-mp-toutiao/dist/uni.mp.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; -import { onUnmounted, injectHook } from 'vue'; +import { onUnmounted, injectHook, ref } from 'vue'; const encode = encodeURIComponent; function stringifyQuery(obj, encodeStr = encode) { @@ -606,6 +606,7 @@ function parseApp(instance, parseAppOptions) { instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); }, }; + initLocale(instance); const vueOptions = instance.$.type; initHooks(appOptions, HOOKS); initUnknownHooks(appOptions, vueOptions); @@ -622,6 +623,17 @@ function initCreateApp(parseAppOptions) { return function createApp(vm) { return App(parseApp(vm, parseAppOptions)); }; +} +function initLocale(appVm) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans'); + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value; + }, + set(v) { + locale.value = v; + }, + }); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; diff --git a/packages/uni-mp-weixin/build.json b/packages/uni-mp-weixin/build.json index a926b1988010695077249a5787a8ea22d22018f7..845f0c65495fbb2ef4a3112d08101f58d92d187c 100644 --- a/packages/uni-mp-weixin/build.json +++ b/packages/uni-mp-weixin/build.json @@ -19,5 +19,6 @@ "__GLOBAL__": "wx", "__PLATFORM__": "\"mp-weixin\"", "__PLATFORM_TITLE__": "微信小程序" - } + }, + "external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"] } diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 429473fb040a1e6b930bd3cb0bbfc7143eed0ccc..d05147288a89a468a2a3e4d830dd94068c9d6b26 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -547,7 +547,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { emitter.emit(name, ...args); }, EmitProtocol); -const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; +const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; // Context例外情况 const CONTEXT_API_RE_EXC = ['createBLEConnection']; @@ -691,6 +691,30 @@ function initWrapper(protocols) { }; } +const getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }); + if (app && app.$vm) { + return app.$vm.$locale; + } + return uni.getSystemInfoSync().language || 'zh-Hans'; +}; +const setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale; + if (oldLocale !== locale) { + getApp().$vm.$locale = locale; + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })); + return true; + } + return false; +}; +const onLocaleChangeCallbacks = []; +const onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn); + } +}; + const baseApis = { $on, $off, @@ -700,6 +724,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, }; function initUni(api, protocols) { const wrapper = initWrapper(protocols); diff --git a/packages/uni-mp-weixin/dist/uni.mp.esm.js b/packages/uni-mp-weixin/dist/uni.mp.esm.js index e0ecaf6650cc4334d0ee4709b14c68d2b546e6ed..4fcc49a7f72540babd8ebd074c291c3745bba1c8 100644 --- a/packages/uni-mp-weixin/dist/uni.mp.esm.js +++ b/packages/uni-mp-weixin/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; -import { onUnmounted, injectHook } from 'vue'; +import { onUnmounted, injectHook, ref } from 'vue'; const encode = encodeURIComponent; function stringifyQuery(obj, encodeStr = encode) { @@ -549,6 +549,7 @@ function parseApp(instance, parseAppOptions) { instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); }, }; + initLocale(instance); const vueOptions = instance.$.type; initHooks(appOptions, HOOKS); initUnknownHooks(appOptions, vueOptions); @@ -565,6 +566,17 @@ function initCreateApp(parseAppOptions) { return function createApp(vm) { return App(parseApp(vm, parseAppOptions)); }; +} +function initLocale(appVm) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans'); + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value; + }, + set(v) { + locale.value = v; + }, + }); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null]; diff --git a/packages/uni-quickapp-webview/build.json b/packages/uni-quickapp-webview/build.json index 0029b4f2cf1262122922a6ed45ba81c455937e11..f7dcdfe06a8e858bf315ff615efd3353898a31b6 100644 --- a/packages/uni-quickapp-webview/build.json +++ b/packages/uni-quickapp-webview/build.json @@ -19,5 +19,6 @@ "__GLOBAL__": "qa", "__PLATFORM__": "\"quickapp-webview\"", "__PLATFORM_TITLE__": "快应用(Webview)版" - } + }, + "external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"] } diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index d6efb2763126e7689038078c20f1c0885614a9db..44740deda87fbd26971a86a7f04ee81b616dce0d 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { emitter.emit(name, ...args); }, EmitProtocol); -const SYNC_API_RE = /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; +const SYNC_API_RE = /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/; const CONTEXT_API_RE = /^create|Manager$/; // Context例外情况 const CONTEXT_API_RE_EXC = ['createBLEConnection']; @@ -727,6 +727,30 @@ function initWrapper(protocols) { }; } +const getLocale = () => { + // 优先使用 $locale + const app = getApp({ allowDefault: true }); + if (app && app.$vm) { + return app.$vm.$locale; + } + return uni.getSystemInfoSync().language || 'zh-Hans'; +}; +const setLocale = (locale) => { + const oldLocale = getApp().$vm.$locale; + if (oldLocale !== locale) { + getApp().$vm.$locale = locale; + onLocaleChangeCallbacks.forEach((fn) => fn({ locale })); + return true; + } + return false; +}; +const onLocaleChangeCallbacks = []; +const onLocaleChange = (fn) => { + if (onLocaleChangeCallbacks.indexOf(fn) === -1) { + onLocaleChangeCallbacks.push(fn); + } +}; + const baseApis = { $on, $off, @@ -736,6 +760,9 @@ const baseApis = { addInterceptor, removeInterceptor, onAppLaunch, + getLocale, + setLocale, + onLocaleChange, }; function initUni(api, protocols) { const wrapper = initWrapper(protocols); diff --git a/packages/uni-quickapp-webview/dist/uni.mp.esm.js b/packages/uni-quickapp-webview/dist/uni.mp.esm.js index 84d914f82c0ea1b099e56afce29d649e0adaeabc..5542f38f092dca004d5d3834cdbc1a5f01bd7ddb 100644 --- a/packages/uni-quickapp-webview/dist/uni.mp.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.mp.esm.js @@ -1,5 +1,5 @@ import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; -import { injectHook } from 'vue'; +import { injectHook, ref } from 'vue'; const encode = encodeURIComponent; function stringifyQuery(obj, encodeStr = encode) { @@ -534,6 +534,7 @@ function parseApp(instance, parseAppOptions) { instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); }, }; + initLocale(instance); const vueOptions = instance.$.type; initHooks(appOptions, HOOKS); initUnknownHooks(appOptions, vueOptions); @@ -550,6 +551,17 @@ function initCreateApp(parseAppOptions) { return function createApp(vm) { return App(parseApp(vm, parseAppOptions)); }; +} +function initLocale(appVm) { + const locale = ref(uni.getSystemInfoSync().language || 'zh-Hans'); + Object.defineProperty(appVm, '$locale', { + get() { + return locale.value; + }, + set(v) { + locale.value = v; + }, + }); } const PROP_TYPES = [String, Number, Boolean, Object, Array, null];