From 470a06e179b6476d5bf7fdf2650cf88b311f4203 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Thu, 2 Sep 2021 19:39:07 +0800 Subject: [PATCH] wip(i18n): Compatible with vue2 --- packages/uni-api/src/service/ui/locale.ts | 4 ++-- packages/uni-h5/dist/uni-h5.cjs.js | 8 ++------ packages/uni-h5/dist/uni-h5.es.js | 8 ++------ .../src/framework/components/page/pageHead.tsx | 4 ++-- packages/uni-i18n/dist/uni-i18n.cjs.js | 15 ++++++++++++--- packages/uni-i18n/dist/uni-i18n.es.js | 15 ++++++++++++--- packages/uni-i18n/src/vue-i18n.ts | 18 +++++++++++++----- 7 files changed, 45 insertions(+), 27 deletions(-) diff --git a/packages/uni-api/src/service/ui/locale.ts b/packages/uni-api/src/service/ui/locale.ts index d8a769a81..11221d368 100644 --- a/packages/uni-api/src/service/ui/locale.ts +++ b/packages/uni-api/src/service/ui/locale.ts @@ -25,8 +25,6 @@ export const setLocale = defineSyncApi( const oldLocale = getApp().$vm.$locale if (oldLocale !== locale) { getApp().$vm.$locale = locale - // 执行 uni.onLocaleChange - UniServiceJSBridge.invokeOnCallback(API_ON_LOCALE_CHANGE, { locale }) if (__PLATFORM__ === 'app') { const pages = getCurrentPages() pages.forEach((page) => { @@ -38,6 +36,8 @@ export const setLocale = defineSyncApi( }) weex.requireModule('plus').setLanguage(locale) } + // 执行 uni.onLocaleChange + UniServiceJSBridge.invokeOnCallback(API_ON_LOCALE_CHANGE, { locale }) return true } return false diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index 366ffcfae..301e8b6c1 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -10470,9 +10470,7 @@ function createPageHeadSearchInputTsx(navigationBar, { "style": { color }, - "placeholder-style": { - color: placeholderColor - }, + "placeholder-style": "color: " + placeholderColor, "class": "uni-page-head-search-input", "confirm-type": "search", "onClick": onClick @@ -10481,9 +10479,7 @@ function createPageHeadSearchInputTsx(navigationBar, { "style": { color }, - "placeholder-style": { - color: placeholderColor - }, + "placeholder-style": "color: " + placeholderColor, "class": "uni-page-head-search-input", "confirm-type": "search", "onFocus": onFocus, diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 5392cefc9..43a9ce4b1 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -20828,9 +20828,7 @@ function createPageHeadSearchInputTsx(navigationBar, { "style": { color }, - "placeholder-style": { - color: placeholderColor - }, + "placeholder-style": "color: " + placeholderColor, "class": "uni-page-head-search-input", "confirm-type": "search", "onClick": onClick @@ -20839,9 +20837,7 @@ function createPageHeadSearchInputTsx(navigationBar, { "style": { color }, - "placeholder-style": { - color: placeholderColor - }, + "placeholder-style": "color: " + placeholderColor, "class": "uni-page-head-search-input", "confirm-type": "search", "onFocus": onFocus, diff --git a/packages/uni-h5/src/framework/components/page/pageHead.tsx b/packages/uni-h5/src/framework/components/page/pageHead.tsx index 60214df3a..b285bf67c 100644 --- a/packages/uni-h5/src/framework/components/page/pageHead.tsx +++ b/packages/uni-h5/src/framework/components/page/pageHead.tsx @@ -220,7 +220,7 @@ function createPageHeadSearchInputTsx( appVm.$locale, (newLocale) => { - i18n.setLocale(newLocale); - }); + // 需要保证 watch 的触发在组件渲染之前 + if (appVm.$watchLocale) { + // vue2 + appVm.$watchLocale((newLocale) => { + i18n.setLocale(newLocale); + }); + } + else { + appVm.$watch(() => appVm.$locale, (newLocale) => { + i18n.setLocale(newLocale); + }); + } } function initVueI18n(locale, messages = {}, fallbackLocale, watcher) { // 兼容旧版本入参 diff --git a/packages/uni-i18n/dist/uni-i18n.es.js b/packages/uni-i18n/dist/uni-i18n.es.js index d4d1d6331..1c7f5e9e1 100644 --- a/packages/uni-i18n/dist/uni-i18n.es.js +++ b/packages/uni-i18n/dist/uni-i18n.es.js @@ -219,9 +219,18 @@ class I18n { const ignoreVueI18n = true; function watchAppLocale(appVm, i18n) { - appVm.$watch(() => appVm.$locale, (newLocale) => { - i18n.setLocale(newLocale); - }); + // 需要保证 watch 的触发在组件渲染之前 + if (appVm.$watchLocale) { + // vue2 + appVm.$watchLocale((newLocale) => { + i18n.setLocale(newLocale); + }); + } + else { + appVm.$watch(() => appVm.$locale, (newLocale) => { + i18n.setLocale(newLocale); + }); + } } function initVueI18n(locale, messages = {}, fallbackLocale, watcher) { // 兼容旧版本入参 diff --git a/packages/uni-i18n/src/vue-i18n.ts b/packages/uni-i18n/src/vue-i18n.ts index c89ba49c9..fc88b6e2b 100644 --- a/packages/uni-i18n/src/vue-i18n.ts +++ b/packages/uni-i18n/src/vue-i18n.ts @@ -14,12 +14,20 @@ type Interpolate = ( ) => string function watchAppLocale(appVm: any, i18n: I18n) { - appVm.$watch( - () => appVm.$locale, - (newLocale: string) => { + // 需要保证 watch 的触发在组件渲染之前 + if (appVm.$watchLocale) { + // vue2 + appVm.$watchLocale((newLocale: string) => { i18n.setLocale(newLocale) - } - ) + }) + } else { + appVm.$watch( + () => appVm.$locale, + (newLocale: string) => { + i18n.setLocale(newLocale) + } + ) + } } // function getDefaultLocale() { -- GitLab