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

wip(i18n): mp

上级 5da9c336
...@@ -67,7 +67,7 @@ function initUniCloudEnv() { ...@@ -67,7 +67,7 @@ function initUniCloudEnv() {
process.env.UNI_CLOUD_PROVIDER = JSON.stringify(uniCloudSpaces.map((space) => { process.env.UNI_CLOUD_PROVIDER = JSON.stringify(uniCloudSpaces.map((space) => {
if (space.clientSecret) { if (space.clientSecret) {
return { return {
provider: 'aliyun', provider: space.provider || 'aliyun',
spaceName: space.name, spaceName: space.name,
spaceId: space.id, spaceId: space.id,
clientSecret: space.clientSecret, clientSecret: space.clientSecret,
...@@ -76,7 +76,7 @@ function initUniCloudEnv() { ...@@ -76,7 +76,7 @@ function initUniCloudEnv() {
} }
else { else {
return { return {
provider: 'tencent', provider: space.provider || 'tencent',
spaceName: space.name, spaceName: space.name,
spaceId: space.id, spaceId: space.id,
}; };
......
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
"__GLOBAL__": "my", "__GLOBAL__": "my",
"__PLATFORM__": "\"mp-alipay\"", "__PLATFORM__": "\"mp-alipay\"",
"__PLATFORM_TITLE__": "支付宝小程序" "__PLATFORM_TITLE__": "支付宝小程序"
} },
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
} }
...@@ -49,8 +49,8 @@ function onAppLaunch(hook) { ...@@ -49,8 +49,8 @@ function onAppLaunch(hook) {
my.appLaunchHooks.push(hook); my.appLaunchHooks.push(hook);
} }
function getBaseSystemInfo() { function getBaseSystemInfo() {
return my.getSystemInfoSync() return my.getSystemInfoSync()
} }
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { ...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args); emitter.emit(name, ...args);
}, EmitProtocol); }, 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$/; const CONTEXT_API_RE = /^create|Manager$/;
// Context例外情况 // Context例外情况
const CONTEXT_API_RE_EXC = ['createBLEConnection']; const CONTEXT_API_RE_EXC = ['createBLEConnection'];
...@@ -727,6 +727,30 @@ function initWrapper(protocols) { ...@@ -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 = { const baseApis = {
$on, $on,
$off, $off,
...@@ -736,6 +760,9 @@ const baseApis = { ...@@ -736,6 +760,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
}; };
function initUni(api, protocols) { function initUni(api, protocols) {
const wrapper = initWrapper(protocols); const wrapper = initWrapper(protocols);
......
import { isPlainObject, isArray, extend, hyphenate, isObject, hasOwn, toNumber, capitalize, isFunction, NOOP, EMPTY_OBJ, camelize } from '@vue/shared'; 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; const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) { function stringifyQuery(obj, encodeStr = encode) {
...@@ -578,6 +578,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -578,6 +578,7 @@ function parseApp(instance, parseAppOptions) {
instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
}, },
}; };
initLocale(instance);
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS); initHooks(appOptions, HOOKS);
initUnknownHooks(appOptions, vueOptions); initUnknownHooks(appOptions, vueOptions);
...@@ -594,6 +595,17 @@ function initCreateApp(parseAppOptions) { ...@@ -594,6 +595,17 @@ function initCreateApp(parseAppOptions) {
return function createApp(vm) { return function createApp(vm) {
return App(parseApp(vm, parseAppOptions)); 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]; const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
......
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
"__GLOBAL__": "swan", "__GLOBAL__": "swan",
"__PLATFORM__": "\"mp-baidu\"", "__PLATFORM__": "\"mp-baidu\"",
"__PLATFORM_TITLE__": "百度小程序" "__PLATFORM_TITLE__": "百度小程序"
} },
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
} }
...@@ -49,8 +49,8 @@ function onAppLaunch(hook) { ...@@ -49,8 +49,8 @@ function onAppLaunch(hook) {
swan.appLaunchHooks.push(hook); swan.appLaunchHooks.push(hook);
} }
function getBaseSystemInfo() { function getBaseSystemInfo() {
return swan.getSystemInfoSync() return swan.getSystemInfoSync()
} }
function validateProtocolFail(name, msg) { function validateProtocolFail(name, msg) {
...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { ...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args); emitter.emit(name, ...args);
}, EmitProtocol); }, 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$/; const CONTEXT_API_RE = /^create|Manager$/;
// Context例外情况 // Context例外情况
const CONTEXT_API_RE_EXC = ['createBLEConnection']; const CONTEXT_API_RE_EXC = ['createBLEConnection'];
...@@ -727,6 +727,30 @@ function initWrapper(protocols) { ...@@ -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 = { const baseApis = {
$on, $on,
$off, $off,
...@@ -736,6 +760,9 @@ const baseApis = { ...@@ -736,6 +760,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
}; };
function initUni(api, protocols) { function initUni(api, protocols) {
const wrapper = initWrapper(protocols); const wrapper = initWrapper(protocols);
......
import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; 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; const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) { function stringifyQuery(obj, encodeStr = encode) {
...@@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) {
instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
}, },
}; };
initLocale(instance);
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS); initHooks(appOptions, HOOKS);
initUnknownHooks(appOptions, vueOptions); initUnknownHooks(appOptions, vueOptions);
...@@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) { ...@@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) {
return function createApp(vm) { return function createApp(vm) {
return App(parseApp(vm, parseAppOptions)); 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]; const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
......
...@@ -16,6 +16,7 @@ import { initWrapper } from './wrapper' ...@@ -16,6 +16,7 @@ import { initWrapper } from './wrapper'
import { MPProtocols } from './protocols' import { MPProtocols } from './protocols'
import { onAppLaunch } from './hook' import { onAppLaunch } from './hook'
import { getLocale, setLocale, onLocaleChange } from './locale'
const baseApis = { const baseApis = {
$on, $on,
...@@ -26,6 +27,9 @@ const baseApis = { ...@@ -26,6 +27,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
} }
export function initUni(api: Record<string, any>, protocols: MPProtocols) { 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 { ...@@ -7,7 +7,7 @@ import {
} from '@dcloudio/uni-api/src/helpers/interceptor' } from '@dcloudio/uni-api/src/helpers/interceptor'
const SYNC_API_RE = 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$/ const CONTEXT_API_RE = /^create|Manager$/
......
import { extend } from '@vue/shared' import { extend } from '@vue/shared'
import { ComponentPublicInstance, ComponentOptions } from 'vue' import { ComponentPublicInstance, ComponentOptions, ref } from 'vue'
import { initBaseInstance } from './componentInstance' import { initBaseInstance } from './componentInstance'
import { initHooks, initUnknownHooks } from './componentHooks' import { initHooks, initUnknownHooks } from './componentHooks'
...@@ -65,6 +65,8 @@ function parseApp( ...@@ -65,6 +65,8 @@ function parseApp(
}, },
} }
initLocale(instance)
const vueOptions = instance.$.type as ComponentOptions const vueOptions = instance.$.type as ComponentOptions
initHooks(appOptions, HOOKS) initHooks(appOptions, HOOKS)
...@@ -87,3 +89,15 @@ export function initCreateApp(parseAppOptions?: ParseAppOptions) { ...@@ -87,3 +89,15 @@ export function initCreateApp(parseAppOptions?: ParseAppOptions) {
return App(parseApp(vm, 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 @@ ...@@ -19,5 +19,6 @@
"__GLOBAL__": "ks", "__GLOBAL__": "ks",
"__PLATFORM__": "\"mp-kuaishou\"", "__PLATFORM__": "\"mp-kuaishou\"",
"__PLATFORM_TITLE__": "快手小程序" "__PLATFORM_TITLE__": "快手小程序"
} },
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
} }
...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { ...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args); emitter.emit(name, ...args);
}, EmitProtocol); }, 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$/; const CONTEXT_API_RE = /^create|Manager$/;
// Context例外情况 // Context例外情况
const CONTEXT_API_RE_EXC = ['createBLEConnection']; const CONTEXT_API_RE_EXC = ['createBLEConnection'];
...@@ -727,6 +727,30 @@ function initWrapper(protocols) { ...@@ -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 = { const baseApis = {
$on, $on,
$off, $off,
...@@ -736,6 +760,9 @@ const baseApis = { ...@@ -736,6 +760,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
}; };
function initUni(api, protocols) { function initUni(api, protocols) {
const wrapper = initWrapper(protocols); const wrapper = initWrapper(protocols);
......
import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; 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; const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) { function stringifyQuery(obj, encodeStr = encode) {
...@@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) {
instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
}, },
}; };
initLocale(instance);
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS); initHooks(appOptions, HOOKS);
initUnknownHooks(appOptions, vueOptions); initUnknownHooks(appOptions, vueOptions);
...@@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) { ...@@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) {
return function createApp(vm) { return function createApp(vm) {
return App(parseApp(vm, parseAppOptions)); 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]; const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
......
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
"__GLOBAL__": "qq", "__GLOBAL__": "qq",
"__PLATFORM__": "\"mp-qq\"", "__PLATFORM__": "\"mp-qq\"",
"__PLATFORM_TITLE__": "QQ小程序" "__PLATFORM_TITLE__": "QQ小程序"
} },
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
} }
...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { ...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args); emitter.emit(name, ...args);
}, EmitProtocol); }, 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$/; const CONTEXT_API_RE = /^create|Manager$/;
// Context例外情况 // Context例外情况
const CONTEXT_API_RE_EXC = ['createBLEConnection']; const CONTEXT_API_RE_EXC = ['createBLEConnection'];
...@@ -727,6 +727,30 @@ function initWrapper(protocols) { ...@@ -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 = { const baseApis = {
$on, $on,
$off, $off,
...@@ -736,6 +760,9 @@ const baseApis = { ...@@ -736,6 +760,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
}; };
function initUni(api, protocols) { function initUni(api, protocols) {
const wrapper = initWrapper(protocols); const wrapper = initWrapper(protocols);
......
import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; 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; const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) { function stringifyQuery(obj, encodeStr = encode) {
...@@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -603,6 +603,7 @@ function parseApp(instance, parseAppOptions) {
instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
}, },
}; };
initLocale(instance);
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS); initHooks(appOptions, HOOKS);
initUnknownHooks(appOptions, vueOptions); initUnknownHooks(appOptions, vueOptions);
...@@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) { ...@@ -619,6 +620,17 @@ function initCreateApp(parseAppOptions) {
return function createApp(vm) { return function createApp(vm) {
return App(parseApp(vm, parseAppOptions)); 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]; const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
......
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
"__GLOBAL__": "tt", "__GLOBAL__": "tt",
"__PLATFORM__": "\"mp-toutiao\"", "__PLATFORM__": "\"mp-toutiao\"",
"__PLATFORM_TITLE__": "字节跳动小程序" "__PLATFORM_TITLE__": "字节跳动小程序"
} },
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
} }
...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { ...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args); emitter.emit(name, ...args);
}, EmitProtocol); }, 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$/; const CONTEXT_API_RE = /^create|Manager$/;
// Context例外情况 // Context例外情况
const CONTEXT_API_RE_EXC = ['createBLEConnection']; const CONTEXT_API_RE_EXC = ['createBLEConnection'];
...@@ -727,6 +727,30 @@ function initWrapper(protocols) { ...@@ -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 = { const baseApis = {
$on, $on,
$off, $off,
...@@ -736,6 +760,9 @@ const baseApis = { ...@@ -736,6 +760,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
}; };
function initUni(api, protocols) { function initUni(api, protocols) {
const wrapper = initWrapper(protocols); const wrapper = initWrapper(protocols);
......
import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; 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; const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) { function stringifyQuery(obj, encodeStr = encode) {
...@@ -606,6 +606,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -606,6 +606,7 @@ function parseApp(instance, parseAppOptions) {
instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
}, },
}; };
initLocale(instance);
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS); initHooks(appOptions, HOOKS);
initUnknownHooks(appOptions, vueOptions); initUnknownHooks(appOptions, vueOptions);
...@@ -622,6 +623,17 @@ function initCreateApp(parseAppOptions) { ...@@ -622,6 +623,17 @@ function initCreateApp(parseAppOptions) {
return function createApp(vm) { return function createApp(vm) {
return App(parseApp(vm, parseAppOptions)); 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]; const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
......
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
"__GLOBAL__": "wx", "__GLOBAL__": "wx",
"__PLATFORM__": "\"mp-weixin\"", "__PLATFORM__": "\"mp-weixin\"",
"__PLATFORM_TITLE__": "微信小程序" "__PLATFORM_TITLE__": "微信小程序"
} },
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
} }
...@@ -547,7 +547,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { ...@@ -547,7 +547,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args); emitter.emit(name, ...args);
}, EmitProtocol); }, 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$/; const CONTEXT_API_RE = /^create|Manager$/;
// Context例外情况 // Context例外情况
const CONTEXT_API_RE_EXC = ['createBLEConnection']; const CONTEXT_API_RE_EXC = ['createBLEConnection'];
...@@ -691,6 +691,30 @@ function initWrapper(protocols) { ...@@ -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 = { const baseApis = {
$on, $on,
$off, $off,
...@@ -700,6 +724,9 @@ const baseApis = { ...@@ -700,6 +724,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
}; };
function initUni(api, protocols) { function initUni(api, protocols) {
const wrapper = initWrapper(protocols); const wrapper = initWrapper(protocols);
......
import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; 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; const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) { function stringifyQuery(obj, encodeStr = encode) {
...@@ -549,6 +549,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -549,6 +549,7 @@ function parseApp(instance, parseAppOptions) {
instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
}, },
}; };
initLocale(instance);
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS); initHooks(appOptions, HOOKS);
initUnknownHooks(appOptions, vueOptions); initUnknownHooks(appOptions, vueOptions);
...@@ -565,6 +566,17 @@ function initCreateApp(parseAppOptions) { ...@@ -565,6 +566,17 @@ function initCreateApp(parseAppOptions) {
return function createApp(vm) { return function createApp(vm) {
return App(parseApp(vm, parseAppOptions)); 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]; const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
......
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
"__GLOBAL__": "qa", "__GLOBAL__": "qa",
"__PLATFORM__": "\"quickapp-webview\"", "__PLATFORM__": "\"quickapp-webview\"",
"__PLATFORM_TITLE__": "快应用(Webview)版" "__PLATFORM_TITLE__": "快应用(Webview)版"
} },
"external": ["@dcloudio/uni-i18n", "@vue/shared", "vue"]
} }
...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => { ...@@ -583,7 +583,7 @@ const $emit = defineSyncApi(API_EMIT, (name, ...args) => {
emitter.emit(name, ...args); emitter.emit(name, ...args);
}, EmitProtocol); }, 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$/; const CONTEXT_API_RE = /^create|Manager$/;
// Context例外情况 // Context例外情况
const CONTEXT_API_RE_EXC = ['createBLEConnection']; const CONTEXT_API_RE_EXC = ['createBLEConnection'];
...@@ -727,6 +727,30 @@ function initWrapper(protocols) { ...@@ -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 = { const baseApis = {
$on, $on,
$off, $off,
...@@ -736,6 +760,9 @@ const baseApis = { ...@@ -736,6 +760,9 @@ const baseApis = {
addInterceptor, addInterceptor,
removeInterceptor, removeInterceptor,
onAppLaunch, onAppLaunch,
getLocale,
setLocale,
onLocaleChange,
}; };
function initUni(api, protocols) { function initUni(api, protocols) {
const wrapper = initWrapper(protocols); const wrapper = initWrapper(protocols);
......
import { isPlainObject, hasOwn, isArray, extend, hyphenate, isObject, toNumber, isFunction, NOOP, camelize } from '@vue/shared'; 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; const encode = encodeURIComponent;
function stringifyQuery(obj, encodeStr = encode) { function stringifyQuery(obj, encodeStr = encode) {
...@@ -534,6 +534,7 @@ function parseApp(instance, parseAppOptions) { ...@@ -534,6 +534,7 @@ function parseApp(instance, parseAppOptions) {
instance.$callHook(ON_LAUNCH, extend({ app: this }, options)); instance.$callHook(ON_LAUNCH, extend({ app: this }, options));
}, },
}; };
initLocale(instance);
const vueOptions = instance.$.type; const vueOptions = instance.$.type;
initHooks(appOptions, HOOKS); initHooks(appOptions, HOOKS);
initUnknownHooks(appOptions, vueOptions); initUnknownHooks(appOptions, vueOptions);
...@@ -550,6 +551,17 @@ function initCreateApp(parseAppOptions) { ...@@ -550,6 +551,17 @@ function initCreateApp(parseAppOptions) {
return function createApp(vm) { return function createApp(vm) {
return App(parseApp(vm, parseAppOptions)); 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]; 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.
先完成此消息的编辑!
想要评论请 注册