提交 12f61422 编写于 作者: Q qiang

feat: getLanguage, setLanguage

上级 52dade77
...@@ -70,6 +70,7 @@ declare namespace UniApp { ...@@ -70,6 +70,7 @@ declare namespace UniApp {
} }
onReady: (fn: Function) => void onReady: (fn: Function) => void
serviceReady: boolean serviceReady: boolean
language: string
} }
interface UniRoute { interface UniRoute {
......
...@@ -14,6 +14,7 @@ export * from './service/ui/createSelectorQuery' ...@@ -14,6 +14,7 @@ export * from './service/ui/createSelectorQuery'
export * from './service/ui/createAnimation' export * from './service/ui/createAnimation'
export * from './service/ui/tabBar' export * from './service/ui/tabBar'
export * from './service/ui/window' export * from './service/ui/window'
export * from './service/ui/language'
export * from './service/keyboard/getSelectedTextRange' export * from './service/keyboard/getSelectedTextRange'
......
import { defineSyncApi } from '../../helpers/api'
import { useI18n } from '@dcloudio/uni-core'
import { BuiltInLocale } from '@dcloudio/uni-i18n'
export const getLanguage = defineSyncApi<typeof uni.getLanguage>(
'getLanguage',
() => {
const i18n = useI18n()
return i18n.getLocale()
}
)
export const setLanguage = defineSyncApi<typeof uni.setLanguage>(
'setLanguage',
(locale) => {
const i18n = useI18n()
return i18n.setLocale(locale as BuiltInLocale)
}
)
...@@ -1432,13 +1432,16 @@ var serviceContext = (function (vue) { ...@@ -1432,13 +1432,16 @@ var serviceContext = (function (vue) {
} }
} }
const ignoreVueI18n = true;
function initLocaleWatcher(appVm, i18n) { function initLocaleWatcher(appVm, i18n) {
appVm.$i18n && if (appVm.$i18n) {
appVm.$i18n.vm.$watch('locale', (newLocale) => { const vm = appVm.$i18n.vm ? appVm.$i18n.vm : appVm;
vm.$watch(appVm.$i18n.vm ? 'locale' : () => appVm.$i18n.locale, (newLocale) => {
i18n.setLocale(newLocale); i18n.setLocale(newLocale);
}, { }, {
immediate: true, immediate: true,
}); });
}
} }
// function getDefaultLocale() { // function getDefaultLocale() {
// if (typeof navigator !== 'undefined') { // if (typeof navigator !== 'undefined') {
...@@ -1450,7 +1453,7 @@ var serviceContext = (function (vue) { ...@@ -1450,7 +1453,7 @@ var serviceContext = (function (vue) {
// } // }
// return uni.getSystemInfoSync().language // return uni.getSystemInfoSync().language
// } // }
function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN) { function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN, watcher) {
// 兼容旧版本入参 // 兼容旧版本入参
if (typeof locale !== 'string') { if (typeof locale !== 'string') {
[locale, messages] = [messages, locale]; [locale, messages] = [messages, locale];
...@@ -1462,6 +1465,7 @@ var serviceContext = (function (vue) { ...@@ -1462,6 +1465,7 @@ var serviceContext = (function (vue) {
locale: locale || fallbackLocale, locale: locale || fallbackLocale,
fallbackLocale, fallbackLocale,
messages, messages,
watcher,
}); });
let t = (key, values) => { let t = (key, values) => {
if (typeof getApp !== 'function') { if (typeof getApp !== 'function') {
...@@ -1473,7 +1477,7 @@ var serviceContext = (function (vue) { ...@@ -1473,7 +1477,7 @@ var serviceContext = (function (vue) {
} }
else { else {
const appVm = getApp().$vm; const appVm = getApp().$vm;
if (!appVm.$t || !appVm.$i18n) { if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) { // if (!locale) {
// i18n.setLocale(getDefaultLocale()) // i18n.setLocale(getDefaultLocale())
// } // }
...@@ -1522,10 +1526,28 @@ var serviceContext = (function (vue) { ...@@ -1522,10 +1526,28 @@ var serviceContext = (function (vue) {
if (!i18n) { if (!i18n) {
let language; let language;
{ {
// TODO 需替换为新API if (typeof getApp === 'function') {
language = plus.os.language; language = weex.requireModule('plus').getLanguage();
}
else {
language = plus.webview.currentWebview().getStyle().language;
}
}
const SET_LOCALE_API = 'i18n.setLocale';
{
i18n = initVueI18n(language, undefined, undefined, typeof getApp === 'function'
? (locale) => {
const pages = getCurrentPages();
pages.forEach((page) => {
UniServiceJSBridge.publishHandler(SET_LOCALE_API, locale, page.$page.id);
});
weex.requireModule('plus').setLanguage(locale);
}
: undefined);
}
if (typeof getApp !== 'function') {
UniViewJSBridge.subscribe(SET_LOCALE_API, i18n.setLocale);
} }
i18n = initVueI18n(language);
} }
return i18n; return i18n;
} }
...@@ -4076,6 +4098,15 @@ var serviceContext = (function (vue) { ...@@ -4076,6 +4098,15 @@ var serviceContext = (function (vue) {
// window.removeEventListener('resize', onResize) // window.removeEventListener('resize', onResize)
}); });
const getLanguage = defineSyncApi('getLanguage', () => {
const i18n = useI18n();
return i18n.getLocale();
});
const setLanguage = defineSyncApi('setLanguage', (locale) => {
const i18n = useI18n();
return i18n.setLocale(locale);
});
const API_GET_SELECTED_TEXT_RANGE = 'getSelectedTextRange'; const API_GET_SELECTED_TEXT_RANGE = 'getSelectedTextRange';
const getSelectedTextRange = defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, { resolve, reject }) => { const getSelectedTextRange = defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, { resolve, reject }) => {
...@@ -10243,6 +10274,7 @@ var serviceContext = (function (vue) { ...@@ -10243,6 +10274,7 @@ var serviceContext = (function (vue) {
// android 需要使用 // android 需要使用
webviewStyle.isTab = isTabBar; webviewStyle.isTab = isTabBar;
} }
webviewStyle.language = weex.requireModule('plus').getLanguage();
if ((process.env.NODE_ENV !== 'production')) { if ((process.env.NODE_ENV !== 'production')) {
console.log(formatLog('updateWebview', webviewStyle)); console.log(formatLog('updateWebview', webviewStyle));
} }
...@@ -12034,6 +12066,8 @@ var serviceContext = (function (vue) { ...@@ -12034,6 +12066,8 @@ var serviceContext = (function (vue) {
canvasPutImageData: canvasPutImageData, canvasPutImageData: canvasPutImageData,
canvasToTempFilePath: canvasToTempFilePath, canvasToTempFilePath: canvasToTempFilePath,
getSelectedTextRange: getSelectedTextRange, getSelectedTextRange: getSelectedTextRange,
getLanguage: getLanguage,
setLanguage: setLanguage,
$on: $on, $on: $on,
$off: $off, $off: $off,
$once: $once, $once: $once,
......
因为 它太大了无法显示 source diff 。你可以改为 查看blob
...@@ -96,6 +96,8 @@ export { ...@@ -96,6 +96,8 @@ export {
canvasPutImageData, canvasPutImageData,
canvasToTempFilePath, canvasToTempFilePath,
getSelectedTextRange, getSelectedTextRange,
getLanguage,
setLanguage,
$on, $on,
$off, $off,
$once, $once,
......
...@@ -17,6 +17,7 @@ export function initWebviewStyle( ...@@ -17,6 +17,7 @@ export function initWebviewStyle(
// android 需要使用 // android 需要使用
webviewStyle.isTab = isTabBar webviewStyle.isTab = isTabBar
} }
webviewStyle.language = weex.requireModule('plus').getLanguage()
if (__DEV__) { if (__DEV__) {
console.log(formatLog('updateWebview', webviewStyle)) console.log(formatLog('updateWebview', webviewStyle))
} }
......
...@@ -12,6 +12,7 @@ export function parseWebviewStyle( ...@@ -12,6 +12,7 @@ export function parseWebviewStyle(
uniPageUrl?: InitUniPageUrl uniPageUrl?: InitUniPageUrl
debugRefresh?: DebugRefresh debugRefresh?: DebugRefresh
isTab?: boolean isTab?: boolean
language?: string
} { } {
const webviewStyle: PlusWebviewWebviewStyles = { const webviewStyle: PlusWebviewWebviewStyles = {
bounce: 'vertical', bounce: 'vertical',
......
...@@ -15,6 +15,7 @@ export function initRecursiveMerge( ...@@ -15,6 +15,7 @@ export function initRecursiveMerge(
name: userManifestJson.versionName, name: userManifestJson.versionName,
code: userManifestJson.versionCode, code: userManifestJson.versionCode,
}, },
language: userManifestJson.language,
}, },
{ plus: userManifestJson['app-plus'] } { plus: userManifestJson['app-plus'] }
) )
......
...@@ -3,6 +3,10 @@ import { BuiltInLocale, initVueI18n } from '@dcloudio/uni-i18n' ...@@ -3,6 +3,10 @@ import { BuiltInLocale, initVueI18n } from '@dcloudio/uni-i18n'
let i18n: ReturnType<typeof initVueI18n> let i18n: ReturnType<typeof initVueI18n>
interface webviewStyleWithLanguage extends PlusWebviewWebviewStyles {
language: string
}
export function useI18n() { export function useI18n() {
if (!i18n) { if (!i18n) {
let language: BuiltInLocale let language: BuiltInLocale
...@@ -10,15 +14,45 @@ export function useI18n() { ...@@ -10,15 +14,45 @@ export function useI18n() {
if (__NODE_JS__) { if (__NODE_JS__) {
language = getEnvLocale() as BuiltInLocale language = getEnvLocale() as BuiltInLocale
} else { } else {
language = navigator.language as BuiltInLocale language = (__uniConfig.language || navigator.language) as BuiltInLocale
} }
} else if (__PLATFORM__ === 'app') { } else if (__PLATFORM__ === 'app') {
// TODO 需替换为新API if (typeof getApp === 'function') {
language = plus.os.language as BuiltInLocale language = weex.requireModule('plus').getLanguage() as BuiltInLocale
} else {
language = (
plus.webview.currentWebview().getStyle() as webviewStyleWithLanguage
).language as BuiltInLocale
}
} else { } else {
language = uni.getSystemInfoSync().language as BuiltInLocale language = uni.getSystemInfoSync().language as BuiltInLocale
} }
i18n = initVueI18n(language) const SET_LOCALE_API = 'i18n.setLocale'
if (__PLATFORM__ === 'app') {
i18n = initVueI18n(
language,
undefined,
undefined,
typeof getApp === 'function'
? (locale) => {
const pages = getCurrentPages()
pages.forEach((page) => {
UniServiceJSBridge.publishHandler(
SET_LOCALE_API,
locale,
page.$page.id
)
})
weex.requireModule('plus').setLanguage(locale)
}
: undefined
)
} else {
i18n = initVueI18n(language)
}
if (__PLATFORM__ === 'app' && typeof getApp !== 'function') {
UniViewJSBridge.subscribe(SET_LOCALE_API, i18n.setLocale)
}
} }
return i18n return i18n
} }
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
"downloadFile", "downloadFile",
"getFileInfo", "getFileInfo",
"getImageInfo", "getImageInfo",
"getLanguage",
"getLeftWindowStyle", "getLeftWindowStyle",
"getLocation", "getLocation",
"getNetworkType", "getNetworkType",
...@@ -101,6 +102,7 @@ ...@@ -101,6 +102,7 @@
"scanCode", "scanCode",
"sendSocketMessage", "sendSocketMessage",
"setKeepScreenOn", "setKeepScreenOn",
"setLanguage",
"setLeftWindowStyle", "setLeftWindowStyle",
"setNavigationBarColor", "setNavigationBarColor",
"setNavigationBarTitle", "setNavigationBarTitle",
......
...@@ -49,6 +49,9 @@ export function uniManifestJsonPlugin(): Plugin { ...@@ -49,6 +49,9 @@ export function uniManifestJsonPlugin(): Plugin {
sdkConfigs.maps.qqmap.key) || sdkConfigs.maps.qqmap.key) ||
defaultQQMapKey defaultQQMapKey
let language: string | null | undefined = manifest.language
language = language && language.toUpperCase() !== 'AUTO' ? language : ''
const flexDirection = const flexDirection =
(manifest['app'] && (manifest['app'] &&
manifest['app'].nvue && manifest['app'].nvue &&
...@@ -67,6 +70,7 @@ export function uniManifestJsonPlugin(): Plugin { ...@@ -67,6 +70,7 @@ export function uniManifestJsonPlugin(): Plugin {
export const async = ${JSON.stringify(async)} export const async = ${JSON.stringify(async)}
export const qqMapKey = '${qqMapKey}' export const qqMapKey = '${qqMapKey}'
export const sdkConfigs = ${JSON.stringify(sdkConfigs)} export const sdkConfigs = ${JSON.stringify(sdkConfigs)}
export const language = '${language}'
`, `,
map: { mappings: '' }, map: { mappings: '' },
} }
......
...@@ -55,7 +55,7 @@ function generatePagesJsonCode( ...@@ -55,7 +55,7 @@ function generatePagesJsonCode(
return ` return `
import { defineAsyncComponent, resolveComponent, createVNode, withCtx, openBlock, createBlock } from 'vue' import { defineAsyncComponent, resolveComponent, createVNode, withCtx, openBlock, createBlock } from 'vue'
import { PageComponent, AsyncLoadingComponent, AsyncErrorComponent, setupWindow } from '@dcloudio/uni-h5' import { PageComponent, AsyncLoadingComponent, AsyncErrorComponent, setupWindow } from '@dcloudio/uni-h5'
import { appid, debug, networkTimeout, router, async, sdkConfigs, qqMapKey, nvue } from '${manifestJsonPath}' import { appid, debug, networkTimeout, router, async, sdkConfigs, qqMapKey, nvue, language } from '${manifestJsonPath}'
${importLayoutComponentsCode} ${importLayoutComponentsCode}
const extend = Object.assign const extend = Object.assign
${cssCode} ${cssCode}
...@@ -255,6 +255,7 @@ delete ${globalName}['____'+appid+'____'] ...@@ -255,6 +255,7 @@ delete ${globalName}['____'+appid+'____']
sdkConfigs, sdkConfigs,
qqMapKey, qqMapKey,
nvue, nvue,
language,
router router
}) })
` `
......
...@@ -15,7 +15,9 @@ function useI18n() { ...@@ -15,7 +15,9 @@ function useI18n() {
language = uniShared.getEnvLocale(); language = uniShared.getEnvLocale();
} }
} }
i18n = uniI18n.initVueI18n(language); {
i18n = uniI18n.initVueI18n(language);
}
} }
return i18n; return i18n;
} }
......
...@@ -9,10 +9,12 @@ function useI18n() { ...@@ -9,10 +9,12 @@ function useI18n() {
let language; let language;
{ {
{ {
language = navigator.language; language = __uniConfig.language || navigator.language;
} }
} }
i18n = initVueI18n(language); {
i18n = initVueI18n(language);
}
} }
return i18n; return i18n;
} }
...@@ -4240,6 +4242,14 @@ const onWindowResize = /* @__PURE__ */ defineOnApi(API_ON_WINDOW_RESIZE, () => { ...@@ -4240,6 +4242,14 @@ const onWindowResize = /* @__PURE__ */ defineOnApi(API_ON_WINDOW_RESIZE, () => {
}); });
const offWindowResize = /* @__PURE__ */ defineOffApi(API_OFF_WINDOW_RESIZE, () => { const offWindowResize = /* @__PURE__ */ defineOffApi(API_OFF_WINDOW_RESIZE, () => {
}); });
const getLanguage = /* @__PURE__ */ defineSyncApi("getLanguage", () => {
const i18n2 = useI18n();
return i18n2.getLocale();
});
const setLanguage = /* @__PURE__ */ defineSyncApi("setLanguage", (locale) => {
const i18n2 = useI18n();
return i18n2.setLocale(locale);
});
const API_GET_SELECTED_TEXT_RANGE = "getSelectedTextRange"; const API_GET_SELECTED_TEXT_RANGE = "getSelectedTextRange";
const getSelectedTextRange$1 = /* @__PURE__ */ defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, { resolve, reject }) => { const getSelectedTextRange$1 = /* @__PURE__ */ defineAsyncApi(API_GET_SELECTED_TEXT_RANGE, (_, { resolve, reject }) => {
UniServiceJSBridge.invokeViewMethod("getSelectedTextRange", {}, getCurrentPageId(), (res) => { UniServiceJSBridge.invokeViewMethod("getSelectedTextRange", {}, getCurrentPageId(), (res) => {
...@@ -19031,6 +19041,8 @@ var api = { ...@@ -19031,6 +19041,8 @@ var api = {
canvasPutImageData, canvasPutImageData,
canvasToTempFilePath, canvasToTempFilePath,
getSelectedTextRange: getSelectedTextRange$1, getSelectedTextRange: getSelectedTextRange$1,
getLanguage,
setLanguage,
$on, $on,
$off, $off,
$once, $once,
...@@ -21184,4 +21196,4 @@ var index = /* @__PURE__ */ defineSystemComponent({ ...@@ -21184,4 +21196,4 @@ var index = /* @__PURE__ */ defineSystemComponent({
return openBlock(), createBlock("div", clazz, [loadingVNode]); return openBlock(), createBlock("div", clazz, [loadingVNode]);
} }
}); });
export { $emit, $off, $on, $once, index$1 as AsyncErrorComponent, index as AsyncLoadingComponent, index$s as Button, index$q as Canvas, index$o as Checkbox, index$p as CheckboxGroup, index$4 as CoverImage, index$5 as CoverView, index$n as Editor, index$u as Form, index$m as Icon, index$l as Image, Input, index$t as Label, LayoutComponent, Map$1 as Map, MovableArea, MovableView, index$k as Navigator, index$2 as PageComponent, index$3 as Picker, PickerView, PickerViewColumn, index$j as Progress, index$h as Radio, index$i as RadioGroup, ResizeSensor, index$g as RichText, ScrollView, index$f as Slider, Swiper, SwiperItem, index$e as Switch, index$d as Text, index$c as Textarea, UniServiceJSBridge$1 as UniServiceJSBridge, UniViewJSBridge$1 as UniViewJSBridge, index$8 as Video, index$b as View, index$7 as WebView, addInterceptor, addPhoneContact, arrayBufferToBase64, base64ToArrayBuffer, canIUse, canvasGetImageData, canvasPutImageData, canvasToTempFilePath, chooseFile, chooseImage, chooseLocation, chooseVideo, clearStorage, clearStorageSync, closeSocket, connectSocket, createAnimation$1 as createAnimation, createCameraContext, createCanvasContext, createInnerAudioContext, createIntersectionObserver, createLivePlayerContext, createMapContext, createMediaQueryObserver, createSelectorQuery, createVideoContext, cssBackdropFilter, cssConstant, cssEnv, cssVar, downloadFile, getApp$1 as getApp, getCurrentPages$1 as getCurrentPages, getFileInfo, getImageInfo, getLeftWindowStyle, getLocation, getNetworkType, getProvider, getRealPath, getRecorderManager, getRightWindowStyle, getSavedFileInfo, getSavedFileList, getScreenBrightness, getSelectedTextRange$1 as getSelectedTextRange, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, getSystemInfo, getSystemInfoSync, getTopWindowStyle, getVideoInfo, hideKeyboard, hideLeftWindow, hideLoading, hideNavigationBarLoading, hideRightWindow, hideTabBar, hideTabBarRedDot, hideToast, hideTopWindow, loadFontFace, login, makePhoneCall, navigateBack, navigateTo, offAccelerometerChange, offCompassChange, offNetworkStatusChange, offWindowResize, onAccelerometerChange, onCompassChange, onGyroscopeChange, onMemoryWarning, onNetworkStatusChange, onSocketClose, onSocketError, onSocketMessage, onSocketOpen, onTabBarMidButtonTap, onUserCaptureScreen, onWindowResize, openDocument, openLocation, pageScrollTo, index$9 as plugin, preloadPage, previewImage, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, removeSavedFileInfo, removeStorage, removeStorageSync, removeTabBarBadge, request, saveFile, saveImageToPhotosAlbum, saveVideoToPhotosAlbum, scanCode, sendSocketMessage, setKeepScreenOn, setLeftWindowStyle, setNavigationBarColor, setNavigationBarTitle, setRightWindowStyle, setScreenBrightness, setStorage, setStorageSync, setTabBarBadge, setTabBarItem, setTabBarStyle, setTopWindowStyle, setupApp, setupPage, setupWindow, showActionSheet, showLeftWindow, showLoading, showModal, showNavigationBarLoading, showRightWindow, showTabBar, showTabBarRedDot, showToast, showTopWindow, startAccelerometer, startCompass, startGyroscope, startPullDownRefresh, stopAccelerometer, stopCompass, stopGyroscope, stopPullDownRefresh, switchTab, uni$1 as uni, uploadFile, upx2px, useTabBar, vibrateLong, vibrateShort }; export { $emit, $off, $on, $once, index$1 as AsyncErrorComponent, index as AsyncLoadingComponent, index$s as Button, index$q as Canvas, index$o as Checkbox, index$p as CheckboxGroup, index$4 as CoverImage, index$5 as CoverView, index$n as Editor, index$u as Form, index$m as Icon, index$l as Image, Input, index$t as Label, LayoutComponent, Map$1 as Map, MovableArea, MovableView, index$k as Navigator, index$2 as PageComponent, index$3 as Picker, PickerView, PickerViewColumn, index$j as Progress, index$h as Radio, index$i as RadioGroup, ResizeSensor, index$g as RichText, ScrollView, index$f as Slider, Swiper, SwiperItem, index$e as Switch, index$d as Text, index$c as Textarea, UniServiceJSBridge$1 as UniServiceJSBridge, UniViewJSBridge$1 as UniViewJSBridge, index$8 as Video, index$b as View, index$7 as WebView, addInterceptor, addPhoneContact, arrayBufferToBase64, base64ToArrayBuffer, canIUse, canvasGetImageData, canvasPutImageData, canvasToTempFilePath, chooseFile, chooseImage, chooseLocation, chooseVideo, clearStorage, clearStorageSync, closeSocket, connectSocket, createAnimation$1 as createAnimation, createCameraContext, createCanvasContext, createInnerAudioContext, createIntersectionObserver, createLivePlayerContext, createMapContext, createMediaQueryObserver, createSelectorQuery, createVideoContext, cssBackdropFilter, cssConstant, cssEnv, cssVar, downloadFile, getApp$1 as getApp, getCurrentPages$1 as getCurrentPages, getFileInfo, getImageInfo, getLanguage, getLeftWindowStyle, getLocation, getNetworkType, getProvider, getRealPath, getRecorderManager, getRightWindowStyle, getSavedFileInfo, getSavedFileList, getScreenBrightness, getSelectedTextRange$1 as getSelectedTextRange, getStorage, getStorageInfo, getStorageInfoSync, getStorageSync, getSystemInfo, getSystemInfoSync, getTopWindowStyle, getVideoInfo, hideKeyboard, hideLeftWindow, hideLoading, hideNavigationBarLoading, hideRightWindow, hideTabBar, hideTabBarRedDot, hideToast, hideTopWindow, loadFontFace, login, makePhoneCall, navigateBack, navigateTo, offAccelerometerChange, offCompassChange, offNetworkStatusChange, offWindowResize, onAccelerometerChange, onCompassChange, onGyroscopeChange, onMemoryWarning, onNetworkStatusChange, onSocketClose, onSocketError, onSocketMessage, onSocketOpen, onTabBarMidButtonTap, onUserCaptureScreen, onWindowResize, openDocument, openLocation, pageScrollTo, index$9 as plugin, preloadPage, previewImage, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, removeSavedFileInfo, removeStorage, removeStorageSync, removeTabBarBadge, request, saveFile, saveImageToPhotosAlbum, saveVideoToPhotosAlbum, scanCode, sendSocketMessage, setKeepScreenOn, setLanguage, setLeftWindowStyle, setNavigationBarColor, setNavigationBarTitle, setRightWindowStyle, setScreenBrightness, setStorage, setStorageSync, setTabBarBadge, setTabBarItem, setTabBarStyle, setTopWindowStyle, setupApp, setupPage, setupWindow, showActionSheet, showLeftWindow, showLoading, showModal, showNavigationBarLoading, showRightWindow, showTabBar, showTabBarRedDot, showToast, showTopWindow, startAccelerometer, startCompass, startGyroscope, startPullDownRefresh, stopAccelerometer, stopCompass, stopGyroscope, stopPullDownRefresh, switchTab, uni$1 as uni, uploadFile, upx2px, useTabBar, vibrateLong, vibrateShort };
...@@ -85,6 +85,8 @@ export { ...@@ -85,6 +85,8 @@ export {
canvasPutImageData, canvasPutImageData,
canvasToTempFilePath, canvasToTempFilePath,
getSelectedTextRange, getSelectedTextRange,
getLanguage,
setLanguage,
$on, $on,
$off, $off,
$once, $once,
......
...@@ -203,13 +203,16 @@ class I18n { ...@@ -203,13 +203,16 @@ class I18n {
} }
} }
const ignoreVueI18n = true;
function initLocaleWatcher(appVm, i18n) { function initLocaleWatcher(appVm, i18n) {
appVm.$i18n && if (appVm.$i18n) {
appVm.$i18n.vm.$watch('locale', (newLocale) => { const vm = appVm.$i18n.vm ? appVm.$i18n.vm : appVm;
vm.$watch(appVm.$i18n.vm ? 'locale' : () => appVm.$i18n.locale, (newLocale) => {
i18n.setLocale(newLocale); i18n.setLocale(newLocale);
}, { }, {
immediate: true, immediate: true,
}); });
}
} }
// function getDefaultLocale() { // function getDefaultLocale() {
// if (typeof navigator !== 'undefined') { // if (typeof navigator !== 'undefined') {
...@@ -221,7 +224,7 @@ function initLocaleWatcher(appVm, i18n) { ...@@ -221,7 +224,7 @@ function initLocaleWatcher(appVm, i18n) {
// } // }
// return uni.getSystemInfoSync().language // return uni.getSystemInfoSync().language
// } // }
function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN) { function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN, watcher) {
// 兼容旧版本入参 // 兼容旧版本入参
if (typeof locale !== 'string') { if (typeof locale !== 'string') {
[locale, messages] = [messages, locale]; [locale, messages] = [messages, locale];
...@@ -233,6 +236,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_ ...@@ -233,6 +236,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
locale: locale || fallbackLocale, locale: locale || fallbackLocale,
fallbackLocale, fallbackLocale,
messages, messages,
watcher,
}); });
let t = (key, values) => { let t = (key, values) => {
if (typeof getApp !== 'function') { if (typeof getApp !== 'function') {
...@@ -244,7 +248,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_ ...@@ -244,7 +248,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
} }
else { else {
const appVm = getApp().$vm; const appVm = getApp().$vm;
if (!appVm.$t || !appVm.$i18n) { if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) { // if (!locale) {
// i18n.setLocale(getDefaultLocale()) // i18n.setLocale(getDefaultLocale())
// } // }
......
...@@ -199,13 +199,16 @@ class I18n { ...@@ -199,13 +199,16 @@ class I18n {
} }
} }
const ignoreVueI18n = true;
function initLocaleWatcher(appVm, i18n) { function initLocaleWatcher(appVm, i18n) {
appVm.$i18n && if (appVm.$i18n) {
appVm.$i18n.vm.$watch('locale', (newLocale) => { const vm = appVm.$i18n.vm ? appVm.$i18n.vm : appVm;
vm.$watch(appVm.$i18n.vm ? 'locale' : () => appVm.$i18n.locale, (newLocale) => {
i18n.setLocale(newLocale); i18n.setLocale(newLocale);
}, { }, {
immediate: true, immediate: true,
}); });
}
} }
// function getDefaultLocale() { // function getDefaultLocale() {
// if (typeof navigator !== 'undefined') { // if (typeof navigator !== 'undefined') {
...@@ -217,7 +220,7 @@ function initLocaleWatcher(appVm, i18n) { ...@@ -217,7 +220,7 @@ function initLocaleWatcher(appVm, i18n) {
// } // }
// return uni.getSystemInfoSync().language // return uni.getSystemInfoSync().language
// } // }
function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN) { function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_EN, watcher) {
// 兼容旧版本入参 // 兼容旧版本入参
if (typeof locale !== 'string') { if (typeof locale !== 'string') {
[locale, messages] = [messages, locale]; [locale, messages] = [messages, locale];
...@@ -229,6 +232,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_ ...@@ -229,6 +232,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
locale: locale || fallbackLocale, locale: locale || fallbackLocale,
fallbackLocale, fallbackLocale,
messages, messages,
watcher,
}); });
let t = (key, values) => { let t = (key, values) => {
if (typeof getApp !== 'function') { if (typeof getApp !== 'function') {
...@@ -240,7 +244,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_ ...@@ -240,7 +244,7 @@ function initVueI18n(locale = LOCALE_EN, messages = {}, fallbackLocale = LOCALE_
} }
else { else {
const appVm = getApp().$vm; const appVm = getApp().$vm;
if (!appVm.$t || !appVm.$i18n) { if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) { // if (!locale) {
// i18n.setLocale(getDefaultLocale()) // i18n.setLocale(getDefaultLocale())
// } // }
......
import { I18n, BuiltInLocale, LocaleMessages, LOCALE_EN } from './I18n' import { I18n, BuiltInLocale, LocaleMessages, LOCALE_EN } from './I18n'
const ignoreVueI18n = true
type Interpolate = ( type Interpolate = (
key: string, key: string,
values?: Record<string, unknown> | Array<unknown> values?: Record<string, unknown> | Array<unknown>
) => string ) => string
function initLocaleWatcher(appVm: any, i18n: I18n) { function initLocaleWatcher(appVm: any, i18n: I18n) {
appVm.$i18n && if (appVm.$i18n) {
appVm.$i18n.vm.$watch( const vm = appVm.$i18n.vm ? appVm.$i18n.vm : appVm
'locale', vm.$watch(
appVm.$i18n.vm ? 'locale' : () => appVm.$i18n.locale,
(newLocale: BuiltInLocale) => { (newLocale: BuiltInLocale) => {
i18n.setLocale(newLocale) i18n.setLocale(newLocale)
}, },
...@@ -16,6 +19,7 @@ function initLocaleWatcher(appVm: any, i18n: I18n) { ...@@ -16,6 +19,7 @@ function initLocaleWatcher(appVm: any, i18n: I18n) {
immediate: true, immediate: true,
} }
) )
}
} }
// function getDefaultLocale() { // function getDefaultLocale() {
...@@ -32,7 +36,8 @@ function initLocaleWatcher(appVm: any, i18n: I18n) { ...@@ -32,7 +36,8 @@ function initLocaleWatcher(appVm: any, i18n: I18n) {
export function initVueI18n( export function initVueI18n(
locale: BuiltInLocale = LOCALE_EN, locale: BuiltInLocale = LOCALE_EN,
messages: LocaleMessages = {}, messages: LocaleMessages = {},
fallbackLocale: BuiltInLocale = LOCALE_EN fallbackLocale: BuiltInLocale = LOCALE_EN,
watcher?: (locale: BuiltInLocale) => void
) { ) {
// 兼容旧版本入参 // 兼容旧版本入参
if (typeof locale !== 'string') { if (typeof locale !== 'string') {
...@@ -45,6 +50,7 @@ export function initVueI18n( ...@@ -45,6 +50,7 @@ export function initVueI18n(
locale: locale || fallbackLocale, locale: locale || fallbackLocale,
fallbackLocale, fallbackLocale,
messages, messages,
watcher,
}) })
let t: Interpolate = (key, values) => { let t: Interpolate = (key, values) => {
if (typeof getApp !== 'function') { if (typeof getApp !== 'function') {
...@@ -55,7 +61,7 @@ export function initVueI18n( ...@@ -55,7 +61,7 @@ export function initVueI18n(
} }
} else { } else {
const appVm = getApp().$vm const appVm = getApp().$vm
if (!appVm.$t || !appVm.$i18n) { if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) { // if (!locale) {
// i18n.setLocale(getDefaultLocale()) // i18n.setLocale(getDefaultLocale())
// } // }
......
...@@ -922,10 +922,10 @@ ...@@ -922,10 +922,10 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
"@dcloudio/types@^2.5.0": "@dcloudio/types@^2.5.1":
version "2.5.0" version "2.5.1"
resolved "https://registry.nlark.com/@dcloudio/types/download/@dcloudio/types-2.5.0.tgz#fe25f19166a9ba091213f72a8a07e1704d205134" resolved "https://registry.yarnpkg.com/@dcloudio/types/-/types-2.5.1.tgz#1aacead4158e2d7448e2585db1a68af28fccac1a"
integrity sha1-/iXxkWapugkSE/cqigfhcE0gUTQ= integrity sha512-tw+ELDxWJFG2/GoVlQuUuA8YATtXMmCP7AzRvb1syrdf1rVgrzCGJr3GVlGE8qLEl/lhhRKuufgcOp3h9VSEzA==
"@eslint/eslintrc@^0.4.3": "@eslint/eslintrc@^0.4.3":
version "0.4.3" version "0.4.3"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册