提交 52382584 编写于 作者: fxy060608's avatar fxy060608

wip(i18n): mp

上级 5da9c336
......@@ -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,
};
......
......@@ -19,5 +19,6 @@
"__GLOBAL__": "my",
"__PLATFORM__": "\"mp-alipay\"",
"__PLATFORM_TITLE__": "支付宝小程序"
}
},
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
}
......@@ -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);
......
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];
......
......@@ -19,5 +19,6 @@
"__GLOBAL__": "swan",
"__PLATFORM__": "\"mp-baidu\"",
"__PLATFORM_TITLE__": "百度小程序"
}
},
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
}
......@@ -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);
......
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];
......
......@@ -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<string, any>, protocols: MPProtocols) {
......
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<typeof uni.onLocaleChange>[0]
const onLocaleChangeCallbacks: OnLocaleCHangeCallback[] = []
export const onLocaleChange: typeof uni.onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn)
}
}
......@@ -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$/
......
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<string>(uni.getSystemInfoSync().language || 'zh-Hans')
Object.defineProperty(appVm, '$locale', {
get() {
return locale.value
},
set(v) {
locale.value = v
},
})
}
......@@ -19,5 +19,6 @@
"__GLOBAL__": "ks",
"__PLATFORM__": "\"mp-kuaishou\"",
"__PLATFORM_TITLE__": "快手小程序"
}
},
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
}
......@@ -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);
......
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];
......
......@@ -19,5 +19,6 @@
"__GLOBAL__": "qq",
"__PLATFORM__": "\"mp-qq\"",
"__PLATFORM_TITLE__": "QQ小程序"
}
},
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
}
......@@ -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);
......
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];
......
......@@ -19,5 +19,6 @@
"__GLOBAL__": "tt",
"__PLATFORM__": "\"mp-toutiao\"",
"__PLATFORM_TITLE__": "字节跳动小程序"
}
},
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
}
......@@ -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);
......
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];
......
......@@ -19,5 +19,6 @@
"__GLOBAL__": "wx",
"__PLATFORM__": "\"mp-weixin\"",
"__PLATFORM_TITLE__": "微信小程序"
}
},
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
}
......@@ -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);
......
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];
......
......@@ -19,5 +19,6 @@
"__GLOBAL__": "qa",
"__PLATFORM__": "\"quickapp-webview\"",
"__PLATFORM_TITLE__": "快应用(Webview)版"
}
},
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
}
......@@ -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);
......
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];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册