From 9762439a95a3f4abadf6f593b2dcfebdb9aaca55 Mon Sep 17 00:00:00 2001 From: w00611838 Date: Thu, 30 Nov 2023 20:16:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9C=B0=E5=8C=BA=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=97=B6=E5=8F=91=E9=80=81=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: maozuzu <1016899463@qq.com> --- frameworks/intl/include/locale_config.h | 1 + frameworks/intl/src/locale_config.cpp | 46 ++++++++++++++++--------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/frameworks/intl/include/locale_config.h b/frameworks/intl/include/locale_config.h index e136c59c..d06015f2 100644 --- a/frameworks/intl/include/locale_config.h +++ b/frameworks/intl/include/locale_config.h @@ -141,6 +141,7 @@ private: static std::set validHcTag; static bool listsInitialized; static bool InitializeLists(); + static bool updateLocal(const std::string &locale); }; } // namespace I18n } // namespace Global diff --git a/frameworks/intl/src/locale_config.cpp b/frameworks/intl/src/locale_config.cpp index 12bf8f55..35aa0dac 100644 --- a/frameworks/intl/src/locale_config.cpp +++ b/frameworks/intl/src/locale_config.cpp @@ -357,21 +357,7 @@ bool LocaleConfig::SetSystemLanguage(const string &language) std::string oldLanguage = GetSystemLanguage(); if (SetParameter(LANGUAGE_KEY, language.data()) == 0) { bool isUpdateSuccess = UpdateSystemLocale(language); - if (isUpdateSuccess) { -#ifdef SUPPORT_GRAPHICS - auto appMgrClient = std::make_unique(); - AppExecFwk::Configuration configuration; - configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language); - appMgrClient->UpdateConfiguration(configuration); - - OHOS::AAFwk::Want localeChangeWant; - localeChangeWant.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED); - OHOS::EventFwk::CommonEventData event(localeChangeWant); - if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(event)) { - HiLog::Info(LABEL, "Failed to Publish event %{public}s", localeChangeWant.GetAction().c_str()); - return false; - } -#endif + if (isUpdateSuccess && updateLocal(language)) { return true; } else { SetParameter(LANGUAGE_KEY, oldLanguage.data()); @@ -410,7 +396,10 @@ bool LocaleConfig::SetSystemRegion(const string ®ion) return false; } } - return SetParameter(LOCALE_KEY, newLocale.data()) == 0; + if (SetParameter(LOCALE_KEY, newLocale.data()) == 0 && updateLocal(newLocale)) { + return true; + } + return false; } bool LocaleConfig::SetSystemLocale(const string &locale) @@ -421,7 +410,30 @@ bool LocaleConfig::SetSystemLocale(const string &locale) if (!IsValidTag(locale)) { return false; } - return SetParameter(LOCALE_KEY, locale.data()) == 0; + if (SetParameter(LOCALE_KEY, locale.data()) == 0 && updateLocal(locale)) { + return true; + } + return false; +} + +bool LocaleConfig::updateLocal(const string &locale) { +#ifdef SUPPORT_GRAPHICS + auto appMgrClient = std::make_unique(); + AppExecFwk::Configuration configuration; + configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, locale); + appMgrClient->UpdateConfiguration(configuration); + + OHOS::AAFwk::Want localeChangeWant; + localeChangeWant.SetAction(OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED); + OHOS::EventFwk::CommonEventData event(localeChangeWant); + if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(event)) { + HiLog::Error(LABEL, "Failed to Publish event %{public}s", localeChangeWant.GetAction().c_str()); + return false; + } else { + return true; + } +#endif + return false; } bool LocaleConfig::IsValidLanguage(const string &language)