From aab998ab063ee0c9c48835536ed4fbd2ca7dce35 Mon Sep 17 00:00:00 2001 From: chenzhensheng79 <14766248+chenzhensheng79@user.noreply.gitee.com> Date: Tue, 13 Aug 2024 15:32:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?LocalizationChannel.getStringResource?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenzhensheng79 <15118029047@139.com> --- .../localization/LocalizationPlugin.ets | 53 ++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets index 93ce6e6d25..87f131f185 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets @@ -13,20 +13,23 @@ * limitations under the License. */ -import LocalizationChannel, { LocalizationMessageHandler } from '../../embedding/engine/systemchannels/LocalizationChannel' +import LocalizationChannel, { + LocalizationMessageHandler +} from '../../embedding/engine/systemchannels/LocalizationChannel' import common from '@ohos.app.ability.common'; import intl from '@ohos.intl'; import Log from '../../util/Log'; import i18n from '@ohos.i18n'; -const TAG = "LocalizationPlugin"; +const TAG = "LocalizationPlugin"; + export default class LocalizationPlugin { - private localizationChannel:LocalizationChannel; + private localizationChannel: LocalizationChannel; private context: common.Context; localeFromString(localeString: string): intl.Locale { - localeString = localeString.replace('_','-'); - let parts: string[] = localeString.split('-',-1); + localeString = localeString.replace('_', '-'); + let parts: string[] = localeString.split('-', -1); let languageCode = parts[0]; let scriptCode = ""; let countryCode = ""; @@ -41,13 +44,36 @@ export default class LocalizationPlugin { countryCode = parts[index]; index++; } - return new intl.Locale(languageCode+'-'+ countryCode +'-' + scriptCode); + return new intl.Locale(languageCode + '-' + countryCode + '-' + scriptCode); } - private localizationMessageHandler: LocalizationMessageHandler =new enterGetStringResource(()=>{ - Log.i(TAG, "getResource enter"); - return "" - }) + private localizationMessageHandler: LocalizationMessageHandler = + new enterGetStringResource((key: string, localeString: string | null) => { + + Log.i(TAG, "getStringResource,key: " + key + ",localeString: " + localeString); + let localContext: common.Context = this.context; + let stringToReturn: string | null = null; + // 获取资源管理器 + let resMgt = localContext.resourceManager; + + try { + // 如果localeString不为空,则更新为指定地区的资源管理器 + if (localeString) { + let overrideConfig = resMgr.getOverrideConfiguration(); + overrideConfig.locale = localeString; + let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig); + stringToReturn = overrideResMgr.getStringByNameSync(key); + } else { + stringToReturn = overrideResMgr.getStringByNameSync(key); + } + } catch (e) { + Log.e(TAG, e); + return null; + } + + return stringToReturn; + }) + constructor(context: common.Context, localizationChannel: LocalizationChannel) { this.context = context; this.localizationChannel = localizationChannel; @@ -61,10 +87,11 @@ export default class LocalizationPlugin { this.localizationChannel.sendLocales(data); } } -class enterGetStringResource{ - getStringResource : (key: string, localeString: string)=>string - constructor(getStringResource: (key: string, localeString: string)=>string) { +class enterGetStringResource { + getStringResource: (key: string, localeString: string | null) => string | null + + constructor(getStringResource: (key: string, localeString: string | null) => string | null) { this.getStringResource = getStringResource } } \ No newline at end of file -- Gitee From 1e8613fd901ce21775192d23d635ac16d431148e Mon Sep 17 00:00:00 2001 From: chenzhensheng79 <14766248+chenzhensheng79@user.noreply.gitee.com> Date: Tue, 13 Aug 2024 16:15:24 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenzhensheng79 <15118029047@139.com> --- .../src/main/ets/plugin/localization/LocalizationPlugin.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets index 87f131f185..f5e1307755 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets @@ -64,7 +64,7 @@ export default class LocalizationPlugin { let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig); stringToReturn = overrideResMgr.getStringByNameSync(key); } else { - stringToReturn = overrideResMgr.getStringByNameSync(key); + stringToReturn = resMgt.getStringByNameSync(key); } } catch (e) { Log.e(TAG, e); -- Gitee From ee1d4f35d60e5af9c4092d793dcfe4284d3dbb4e Mon Sep 17 00:00:00 2001 From: chenzhensheng79 <14766248+chenzhensheng79@user.noreply.gitee.com> Date: Tue, 13 Aug 2024 17:16:37 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=97=A8=E7=A6=81=E9=97=AE=E9=A2=98=20Sign?= =?UTF-8?q?ed-off-by:=20chenzhensheng79=20<14766248+chenzhensheng79@user.n?= =?UTF-8?q?oreply.gitee.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/plugin/localization/LocalizationPlugin.ets | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets index f5e1307755..05421fc41e 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/localization/LocalizationPlugin.ets @@ -54,7 +54,7 @@ export default class LocalizationPlugin { let localContext: common.Context = this.context; let stringToReturn: string | null = null; // 获取资源管理器 - let resMgt = localContext.resourceManager; + let resMgr = localContext.resourceManager; try { // 如果localeString不为空,则更新为指定地区的资源管理器 @@ -64,7 +64,7 @@ export default class LocalizationPlugin { let overrideResMgr = resMgr.getOverrideResourceManager(overrideConfig); stringToReturn = overrideResMgr.getStringByNameSync(key); } else { - stringToReturn = resMgt.getStringByNameSync(key); + stringToReturn = resMgr.getStringByNameSync(key); } } catch (e) { Log.e(TAG, e); -- Gitee