From aa42a680f76a70931c63af9e20aa13751461e6b8 Mon Sep 17 00:00:00 2001 From: LY Date: Mon, 21 Apr 2025 18:10:48 +0800 Subject: [PATCH] =?UTF-8?q?Configuration=E6=96=B0=E5=A2=9Elocale=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LY --- frameworks/intl/include/locale_config.h | 21 +++- frameworks/intl/include/multi_users.h | 3 +- frameworks/intl/src/collator.cpp | 2 +- frameworks/intl/src/date_time_format.cpp | 2 +- frameworks/intl/src/i18n_timezone.cpp | 6 +- frameworks/intl/src/index_util.cpp | 2 +- frameworks/intl/src/locale_config.cpp | 107 +++++++++++++----- frameworks/intl/src/locale_info.cpp | 2 +- frameworks/intl/src/multi_users.cpp | 12 +- frameworks/intl/src/number_format.cpp | 2 +- frameworks/intl/src/plural_rules.cpp | 2 +- frameworks/intl/src/preferred_language.cpp | 4 +- frameworks/intl/src/relative_time_format.cpp | 2 +- .../intl/src/simple_date_time_format.cpp | 2 +- frameworks/intl/src/simple_number_format.cpp | 2 +- frameworks/intl/src/system_locale_manager.cpp | 4 +- .../intl/test/unittest/locale_config_test.cpp | 21 ++++ .../intl/test/unittest/locale_config_test.h | 1 + .../js/kits/src/entity_recognizer_addon.cpp | 2 +- interfaces/js/kits/src/i18n_addon.cpp | 2 +- .../kits/src/system_locale_manager_addon.cpp | 4 +- 21 files changed, 145 insertions(+), 60 deletions(-) diff --git a/frameworks/intl/include/locale_config.h b/frameworks/intl/include/locale_config.h index ea06ace4..9fb8343a 100644 --- a/frameworks/intl/include/locale_config.h +++ b/frameworks/intl/include/locale_config.h @@ -98,6 +98,7 @@ public: static std::string GetTemperatureName(const TemperatureType& type); static WeekDay GetFirstDayOfWeek(); static std::string GetEffectiveLanguage(); + static std::string GetEffectiveLocale(); static std::string GetSystemLanguage(); static std::string GetSystemRegion(); static std::string GetSystemLocale(); @@ -128,6 +129,7 @@ public: static std::string GetSimplifiedSystemLanguage(); static std::string GetUnicodeWrappedFilePath(const std::string &path, const char delimiter, std::shared_ptr localeInfo, std::string &invalidField); + static std::string ComputeEffectiveLanguage(const std::string& localeTag, const std::string& language); static const std::string HOUR_EVENT_DATA; static const std::string LANGUAGE_KEY; static const std::string LOCALE_KEY; @@ -139,7 +141,6 @@ private: static void Split(const std::string &src, const std::string &sep, std::vector &dest); static void Split(const std::string &src, const std::string &sep, std::unordered_set &dest); static I18nErrorCode UpdateSystemLocale(const std::string &language); - static std::string ComputeEffectiveLanguage(const std::string& locale, const std::string& language); static constexpr uint32_t LANGUAGE_LEN = 2; static constexpr uint32_t LOCALE_ITEM_COUNT = 3; static constexpr uint32_t SCRIPT_OFFSET = 2; @@ -211,14 +212,22 @@ private: #ifdef SUPPORT_GRAPHICS /** - * @brief Provided for updating i18n configuration include language and hour24. + * @brief Provided for updating i18n configuration include locale and language. * - * @param key Indicats configuration key. - * @param value Indicates configuration value. + * @param locale Indicats update locale. + * @param language Indicates update language. * @param userId Indicats update user id. - * @param isSwitchUser Indicates whether user switch. */ - static void UpdateConfiguration(const char *key, const std::string &value, int32_t userId); + static void UpdateConfigurationLocaleAndLanguage(const std::string& locale, const std::string& language, + int32_t userId); + + /** + * @brief Provided for updating i18n configuration include is24Hour. + * + * @param locale Indicats update is24Hour. + * @param userId Indicats update user id. + */ + static void UpdateConfiguration24Hour(const std::string& is24Hour, int32_t userId); /** * @brief Provided for public i18n event include locale-change event and time-change event. diff --git a/frameworks/intl/include/multi_users.h b/frameworks/intl/include/multi_users.h index 37a043fc..d7b2f211 100644 --- a/frameworks/intl/include/multi_users.h +++ b/frameworks/intl/include/multi_users.h @@ -47,7 +47,8 @@ private: static I18nErrorCode SetParamFromPreferences(const std::string& paramKey, const std::string& paramValue); #ifdef SUPPORT_GRAPHICS - static void UpdateConfiguration(const std::string& locale, const std::string& is24Hour, int32_t userId); + static void UpdateConfiguration(const std::string& locale, const std::string& language, + const std::string& is24Hour, int32_t userId); static I18nErrorCode PublishCommonEvent(int32_t userId); #endif diff --git a/frameworks/intl/src/collator.cpp b/frameworks/intl/src/collator.cpp index 32e44686..46ff6ac7 100644 --- a/frameworks/intl/src/collator.cpp +++ b/frameworks/intl/src/collator.cpp @@ -74,7 +74,7 @@ Collator::Collator(std::vector &localeTags, std::map &c delete dateFormat; dateFormat = nullptr; } - return InitWithLocale(LocaleConfig::GetSystemLocale(), configs); + return InitWithLocale(LocaleConfig::GetEffectiveLocale(), configs); } void DateTimeFormat::InitDateFormatWithoutConfigs(UErrorCode &status) diff --git a/frameworks/intl/src/i18n_timezone.cpp b/frameworks/intl/src/i18n_timezone.cpp index 8ceee76a..9ad59e62 100644 --- a/frameworks/intl/src/i18n_timezone.cpp +++ b/frameworks/intl/src/i18n_timezone.cpp @@ -580,13 +580,13 @@ std::string I18nTimeZone::GetDisplayName() if (timezone == nullptr) { return ""; } - std::string localeStr = LocaleConfig::GetSystemLocale(); + std::string localeStr = LocaleConfig::GetEffectiveLocale(); return GetDisplayName(localeStr, false); } std::string I18nTimeZone::GetDisplayName(bool isDST) { - std::string localeStr = LocaleConfig::GetSystemLocale(); + std::string localeStr = LocaleConfig::GetEffectiveLocale(); return GetDisplayName(localeStr, isDST); } @@ -605,7 +605,7 @@ std::string I18nTimeZone::GetDisplayName( if (LocaleConfig::IsValidTag(localeStr)) { locale = icu::Locale::forLanguageTag(localeStr, status); } else { - std::string systemLocale = LocaleConfig::GetSystemLocale(); + std::string systemLocale = LocaleConfig::GetEffectiveLocale(); locale = icu::Locale::forLanguageTag(systemLocale, status); } if (U_FAILURE(status)) { diff --git a/frameworks/intl/src/index_util.cpp b/frameworks/intl/src/index_util.cpp index 55fec8b9..9e156338 100644 --- a/frameworks/intl/src/index_util.cpp +++ b/frameworks/intl/src/index_util.cpp @@ -28,7 +28,7 @@ IndexUtil::IndexUtil(const std::string &localeTag) { UErrorCode status = U_ZERO_ERROR; if (localeTag.empty()) { - std::string sysLocale = LocaleConfig::GetSystemLocale(); + std::string sysLocale = LocaleConfig::GetEffectiveLocale(); if (sysLocale.empty()) { HILOG_ERROR_I18N("IndexUtil::IndexUtil: get system locale failed."); return; diff --git a/frameworks/intl/src/locale_config.cpp b/frameworks/intl/src/locale_config.cpp index 9bb6cd31..214b898d 100644 --- a/frameworks/intl/src/locale_config.cpp +++ b/frameworks/intl/src/locale_config.cpp @@ -397,6 +397,21 @@ std::string LocaleConfig::GetEffectiveLanguage() return ComputeEffectiveLanguage(systemLocale, systemLanguage); } +std::string LocaleConfig::GetEffectiveLocale() +{ + std::string systemLocale = ReadSystemParameter(LOCALE_KEY.c_str(), CONFIG_LEN); + if (!systemLocale.empty()) { + return systemLocale; + } + HILOG_ERROR_I18N("LocaleConfig::GetEffectiveLocale: Read system locale failed."); + systemLocale = ReadSystemParameter(DEFAULT_LOCALE_KEY, CONFIG_LEN); + if (systemLocale.empty()) { + HILOG_ERROR_I18N("LocaleConfig::GetEffectiveLocale: Read default system locale failed."); + return "zh-Hans-CN"; + } + return systemLocale; +} + string LocaleConfig::GetSystemLanguage() { std::string systemLanguage = ReadSystemParameter(LANGUAGE_KEY.c_str(), CONFIG_LEN); @@ -447,15 +462,10 @@ string LocaleConfig::GetCountry(const string& parameter) string LocaleConfig::GetSystemLocale() { - std::string systemLocale = ReadSystemParameter(LOCALE_KEY.c_str(), CONFIG_LEN); - if (!systemLocale.empty()) { - return systemLocale; - } - HILOG_ERROR_I18N("LocaleConfig::GetSystemLocale: Read system locale failed."); - systemLocale = ReadSystemParameter(DEFAULT_LOCALE_KEY, CONFIG_LEN); - if (systemLocale.empty()) { - HILOG_ERROR_I18N("LocaleConfig::GetSystemLocale: Read default system locale failed."); - return "zh-Hans-CN"; + std::string systemLocale = GetEffectiveLocale(); + size_t pos = systemLocale.find(EXT_PARAM_KEY); + if (pos != std::string::npos) { + systemLocale = systemLocale.substr(0, pos); } return systemLocale; } @@ -1273,7 +1283,7 @@ std::string LocaleConfig::GetValidLocale(const std::string &localeTag) parseExtension(extension, extensionMap); } - std::string systemLocaleTag = GetSystemLocale(); + std::string systemLocaleTag = GetEffectiveLocale(); std::string defaultExtension = ""; found = systemLocaleTag.find(EXT_PARAM_KEY); if (found != std::string::npos) { @@ -1322,7 +1332,7 @@ bool LocaleConfig::Is24HourClock() return false; } if (is24Hour.compare("default") == 0) { - std::string systemLocale = GetSystemLocale(); + std::string systemLocale = GetEffectiveLocale(); return Is24HourLocale(systemLocale); } if (is24Hour.compare("true") == 0) { @@ -1393,7 +1403,7 @@ bool LocaleConfig::HasDesignator(const std::string& pattern, const char designat bool LocaleConfig::GetUsingLocalDigit() { - std::string locale = GetSystemLocale(); + std::string locale = GetEffectiveLocale(); LocaleInfo localeInfo(locale); std::string language = localeInfo.GetLanguage(); if (localDigitMap.find(language) == localDigitMap.end()) { @@ -1472,11 +1482,16 @@ void LocaleConfig::QueryUpgradeLocale() } } -std::string LocaleConfig::ComputeEffectiveLanguage(const std::string& locale, const std::string& language) +std::string LocaleConfig::ComputeEffectiveLanguage(const std::string& localeTag, const std::string& language) { - if (locale.empty()) { + if (localeTag.empty()) { HILOG_ERROR_I18N("LocaleConfig::ComputeEffectiveLanguage: locale is empty."); - return locale; + return localeTag; + } + std::string locale = localeTag; + size_t pos = localeTag.find(EXT_PARAM_KEY); + if (pos != std::string::npos) { + locale = localeTag.substr(0, pos); } size_t prefixLength = 2; if (locale.compare(0, prefixLength, "es") != 0 && locale.compare(0, prefixLength, "pt") != 0) { @@ -1530,7 +1545,7 @@ I18nErrorCode LocaleConfig::SetSystemLocale(const std::string &localeTag, int32_ MultiUsers::SaveLocale(localId, localeTag); #endif #ifdef SUPPORT_GRAPHICS - UpdateConfiguration(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, effectiveLanguage, userId); + UpdateConfigurationLocaleAndLanguage(localeTag, effectiveLanguage, userId); return PublishCommonEvent(EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED, userId); #else return I18nErrorCode::SUCCESS; @@ -1562,7 +1577,7 @@ I18nErrorCode LocaleConfig::Set24HourClock(const std::string &option, int32_t us MultiUsers::SaveIs24Hour(localId, option); #endif #ifdef SUPPORT_GRAPHICS - UpdateConfiguration(AAFwk::GlobalConfigurationKey::SYSTEM_HOUR, option, userId); + UpdateConfiguration24Hour(option, userId); return PublishCommonEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED, userId); #else return I18nErrorCode::SUCCESS; @@ -1572,7 +1587,7 @@ I18nErrorCode LocaleConfig::Set24HourClock(const std::string &option, int32_t us I18nErrorCode LocaleConfig::SetUsingLocalDigit(bool flag, int32_t userId) { // check whether current language support local digit. - std::string localeTag = GetSystemLocale(); + std::string localeTag = GetEffectiveLocale(); std::string languageTag = localeTag.substr(0, 2); // obtain 2 length language code. auto it = localDigitMap.find(languageTag); if (it == localDigitMap.end()) { @@ -1591,7 +1606,7 @@ I18nErrorCode LocaleConfig::SetTemperatureType(const TemperatureType& type, int3 return I18nErrorCode::INVALID_TEMPERATURE_TYPE; } std::string option = temperatureTypeToName[type]; - std::string localeTag = GetSystemLocale(); + std::string localeTag = GetEffectiveLocale(); if (localeTag.empty()) { HILOG_ERROR_I18N("LocaleConfig::SetTemperatureType: get system locale failed."); return I18nErrorCode::UPDATE_TEMPERATURE_TYPE_FAILED; @@ -1602,7 +1617,7 @@ I18nErrorCode LocaleConfig::SetTemperatureType(const TemperatureType& type, int3 TemperatureType LocaleConfig::GetTemperatureType() { - std::string localeTag = GetSystemLocale(); + std::string localeTag = GetEffectiveLocale(); if (localeTag.empty()) { return TemperatureType::CELSIUS; } @@ -1640,7 +1655,7 @@ I18nErrorCode LocaleConfig::SetFirstDayOfWeek(const WeekDay& type, int32_t userI return I18nErrorCode::INVALID_WEEK_DAY; } std::string option = weekDayToName[type]; - std::string localeTag = GetSystemLocale(); + std::string localeTag = GetEffectiveLocale(); if (localeTag.empty()) { HILOG_ERROR_I18N("LocaleConfig::SetFirstDayOfWeek: get system locale failed."); return I18nErrorCode::UPDATE_WEEK_DAY_FAILED; @@ -1651,7 +1666,7 @@ I18nErrorCode LocaleConfig::SetFirstDayOfWeek(const WeekDay& type, int32_t userI WeekDay LocaleConfig::GetFirstDayOfWeek() { - std::string localeTag = GetSystemLocale(); + std::string localeTag = GetEffectiveLocale(); if (localeTag.empty()) { HILOG_ERROR_I18N("LocaleConfig::GetFirstDayOfWeek: get system locale failed."); return WeekDay::MON; @@ -1681,18 +1696,54 @@ WeekDay LocaleConfig::GetFirstDayOfWeek() } #ifdef SUPPORT_GRAPHICS -void LocaleConfig::UpdateConfiguration(const char *key, const std::string &value, int32_t userId) + +void LocaleConfig::UpdateConfigurationLocaleAndLanguage(const std::string& locale, const std::string& language, + int32_t userId) { + if (locale.empty() && language.empty()) { + HILOG_ERROR_I18N("LocaleConfig::UpdateConfigurationLocaleAndLanguage: locale and language are empty."); + return; + } AppExecFwk::Configuration configuration; - configuration.AddItem(key, value); + if (!locale.empty()) { + configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE, locale); + } + if (!language.empty()) { + configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, language); + } auto appMgrClient = std::make_unique(); + if (appMgrClient == nullptr) { + HILOG_ERROR_I18N("LocaleConfig::UpdateConfigurationLocaleAndLanguage: Get appMgrClient failed."); + return; + } AppExecFwk::AppMgrResultCode status = appMgrClient->UpdateConfiguration(configuration, userId); if (status != AppExecFwk::AppMgrResultCode::RESULT_OK) { - HILOG_ERROR_I18N("LocaleConfig::UpdateConfiguration: Update configuration userId %{public}d failed.", userId); + HILOG_ERROR_I18N("LocaleConfig::UpdateConfigurationLocaleAndLanguage: Update configuration " + "userId %{public}d failed.", userId); + return; + } + HILOG_INFO_I18N("LocaleConfig::UpdateConfigurationLocaleAndLanguage: update userId %{public}d, locale %{public}s, " + "language %{public}s.", userId, locale.c_str(), language.c_str()); +} + +void LocaleConfig::UpdateConfiguration24Hour(const std::string& is24Hour, int32_t userId) +{ + AppExecFwk::Configuration configuration; + configuration.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_HOUR, is24Hour); + auto appMgrClient = std::make_unique(); + if (appMgrClient == nullptr) { + HILOG_ERROR_I18N("LocaleConfig::UpdateConfiguration24Hour: Get appMgrClient failed."); + return; + } + AppExecFwk::AppMgrResultCode status = appMgrClient->UpdateConfiguration(configuration, userId); + if (status != AppExecFwk::AppMgrResultCode::RESULT_OK) { + HILOG_ERROR_I18N("LocaleConfig::UpdateConfiguration24Hour: Update configuration userId %{public}d failed.", + userId); return; } - HILOG_INFO_I18N("LocaleConfig::UpdateLanguageConfiguration: update userId %{public}d.", userId); + HILOG_INFO_I18N("LocaleConfig::UpdateConfiguration24Hour: update userId %{public}d, is24Hour %{public}s.", + userId, is24Hour.c_str()); } I18nErrorCode LocaleConfig::PublishCommonEvent(const std::string &eventType, int32_t userId) @@ -1736,7 +1787,7 @@ std::string LocaleConfig::UpdateLanguageOfLocale(const std::string &languageTag) std::string langTag = languageLocale.getLanguage(); std::string scriptTag = languageLocale.getScript(); // Compute region and extend param part from current system locale. - std::string systemLocaleTag = GetSystemLocale(); + std::string systemLocaleTag = GetEffectiveLocale(); icu::Locale systemLocale = icu::Locale::forLanguageTag(systemLocaleTag.c_str(), status); if (U_FAILURE(status)) { HILOG_ERROR_I18N("LocaleConfig::UpdateSystemLocale init icu Locale for locale %{public}s failed.", @@ -1773,7 +1824,7 @@ std::string LocaleConfig::CreateLocale(const std::string &languageTag, const std std::string LocaleConfig::UpdateRegionOfLocale(const std::string ®ionTag) { - std::string localeTag = GetSystemLocale(); + std::string localeTag = GetEffectiveLocale(); // if current system locale is null, contruct a locale from region tag. if (localeTag.empty()) { return CreateLocaleFromRegion(regionTag); diff --git a/frameworks/intl/src/locale_info.cpp b/frameworks/intl/src/locale_info.cpp index fba8c79c..d9b78854 100644 --- a/frameworks/intl/src/locale_info.cpp +++ b/frameworks/intl/src/locale_info.cpp @@ -64,7 +64,7 @@ void LocaleInfo::InitLocaleInfo(const std::string &localeTag, std::map(); diff --git a/frameworks/intl/src/number_format.cpp b/frameworks/intl/src/number_format.cpp index cc5e5f9a..2564fa95 100644 --- a/frameworks/intl/src/number_format.cpp +++ b/frameworks/intl/src/number_format.cpp @@ -143,7 +143,7 @@ NumberFormat::NumberFormat(const std::vector &localeTags, std::map< } } if (!createSuccess) { - std::string systemLocale = LocaleConfig::GetSystemLocale(); + std::string systemLocale = LocaleConfig::GetEffectiveLocale(); localeInfo = std::make_unique(systemLocale, configs); CreateRelativeTimeFormat(systemLocale); if (localeInfo != nullptr && localeInfo->InitSuccess()) { diff --git a/frameworks/intl/src/plural_rules.cpp b/frameworks/intl/src/plural_rules.cpp index c1494b22..f6bfee6f 100644 --- a/frameworks/intl/src/plural_rules.cpp +++ b/frameworks/intl/src/plural_rules.cpp @@ -104,7 +104,7 @@ void PluralRules::InitPluralRules(std::vector &localeTags, { UPluralType uPluralType = (type == "cardinal") ? UPLURAL_TYPE_CARDINAL : UPLURAL_TYPE_ORDINAL; UErrorCode status = UErrorCode::U_ZERO_ERROR; - localeTags.push_back(LocaleConfig::GetSystemLocale()); + localeTags.push_back(LocaleConfig::GetEffectiveLocale()); for (size_t i = 0; i < localeTags.size(); i++) { std::string curLocale = localeTags[i]; locale = icu::Locale::forLanguageTag(icu::StringPiece(curLocale), status); diff --git a/frameworks/intl/src/preferred_language.cpp b/frameworks/intl/src/preferred_language.cpp index b0395299..e7dbaa9f 100644 --- a/frameworks/intl/src/preferred_language.cpp +++ b/frameworks/intl/src/preferred_language.cpp @@ -183,11 +183,11 @@ std::string PreferredLanguage::GetAppPreferredLanguage() if (preferences == nullptr) { HILOG_ERROR_I18N( "PreferredLanguage::GetAppPreferredLanguage get i18n preferences failed, return system language."); - return LocaleConfig::GetSystemLocale(); + return LocaleConfig::GetEffectiveLanguage(); } std::string res = preferences->GetString(PreferredLanguage::APP_LANGUAGE_KEY, ""); if (res.length() == 0 || res.compare("default") == 0) { - return LocaleConfig::GetSystemLocale(); + return LocaleConfig::GetEffectiveLanguage(); } return res; } diff --git a/frameworks/intl/src/relative_time_format.cpp b/frameworks/intl/src/relative_time_format.cpp index 1b23e2d7..e4b2e4d0 100644 --- a/frameworks/intl/src/relative_time_format.cpp +++ b/frameworks/intl/src/relative_time_format.cpp @@ -86,7 +86,7 @@ RelativeTimeFormat::RelativeTimeFormat(const std::vector &localeTag } } if (!createSuccess) { - localeInfo = std::make_unique(LocaleConfig::GetSystemLocale(), configs); + localeInfo = std::make_unique(LocaleConfig::GetEffectiveLocale(), configs); if (localeInfo != nullptr && localeInfo->InitSuccess()) { locale = localeInfo->GetLocale(); localeBaseName = localeInfo->GetBaseName(); diff --git a/frameworks/intl/src/simple_date_time_format.cpp b/frameworks/intl/src/simple_date_time_format.cpp index 23f511fc..5b8a901c 100644 --- a/frameworks/intl/src/simple_date_time_format.cpp +++ b/frameworks/intl/src/simple_date_time_format.cpp @@ -39,7 +39,7 @@ SimpleDateTimeFormat::SimpleDateTimeFormat(const std::string& skeletonOrPattern, std::string locale; if (localeInfo == nullptr) { - locale = LocaleConfig::GetSystemLocale(); + locale = LocaleConfig::GetEffectiveLocale(); } else { locale = localeInfo->GetBaseName(); } diff --git a/frameworks/intl/src/simple_number_format.cpp b/frameworks/intl/src/simple_number_format.cpp index 84430250..b06ca60e 100644 --- a/frameworks/intl/src/simple_number_format.cpp +++ b/frameworks/intl/src/simple_number_format.cpp @@ -35,7 +35,7 @@ SimpleNumberFormat::SimpleNumberFormat(const std::string& skeleton, std::shared_ } std::string locale; if (localeInfo == nullptr) { - locale = LocaleConfig::GetSystemLocale(); + locale = LocaleConfig::GetEffectiveLocale(); } else { locale = localeInfo->GetBaseName(); } diff --git a/frameworks/intl/src/system_locale_manager.cpp b/frameworks/intl/src/system_locale_manager.cpp index 02962b2d..c9e2bfef 100644 --- a/frameworks/intl/src/system_locale_manager.cpp +++ b/frameworks/intl/src/system_locale_manager.cpp @@ -314,7 +314,7 @@ std::vector SystemLocaleManager::GetTimezoneCityInfoArray(I18n return result; } result = GetTimezoneCityInfoArray(); - SortTimezoneCityItemList(LocaleConfig::GetSystemLocale(), result); + SortTimezoneCityItemList(LocaleConfig::GetEffectiveLocale(), result); return result; } @@ -322,7 +322,7 @@ std::vector SystemLocaleManager::GetTimezoneCityInfoArray() { std::vector result; std::unordered_set zoneCityIds = I18nTimeZone::GetAvailableZoneCityIDs(); - std::string locale = LocaleConfig::GetSystemLocale(); + std::string locale = LocaleConfig::GetEffectiveLocale(); std::string localeBaseName = I18nTimeZone::GetLocaleBaseName(locale); std::map displayNameMap = I18nTimeZone::FindCityDisplayNameMap(localeBaseName); std::map tzMap; diff --git a/frameworks/intl/test/unittest/locale_config_test.cpp b/frameworks/intl/test/unittest/locale_config_test.cpp index 033804b4..b7a52909 100644 --- a/frameworks/intl/test/unittest/locale_config_test.cpp +++ b/frameworks/intl/test/unittest/locale_config_test.cpp @@ -489,6 +489,27 @@ HWTEST_F(LocaleConfigTest, LocaleConfigFuncTest022, TestSize.Level1) EXPECT_EQ(ret, LocaleConfigTest::simplifiedLanguage[index]); } } + +/** + * @tc.name: LocaleConfigFuncTest023 + * @tc.desc: Test LocaleConfig GetEffectiveLanguage and GetEffectiveLocale + * @tc.type: FUNC + */ +HWTEST_F(LocaleConfigTest, LocaleConfigFuncTest023, TestSize.Level1) +{ + std::string systemLanguage = LocaleConfig::GetSystemLanguage(); + std::string systemLocale = LocaleConfig::GetSystemLocale(); + std::string language = "zh-Hans"; + std::string locale = "zh-Hans-CN-u-mu-celsius"; + LocaleConfig::SetSystemLanguage(language); + LocaleConfig::SetSystemLocale(locale); + std::string effectiveLanguage = LocaleConfig::GetEffectiveLanguage(); + std::string effectiveLocale = LocaleConfig::GetEffectiveLocale(); + EXPECT_EQ(effectiveLanguage, "zh-Hans-CN"); + EXPECT_EQ(effectiveLocale, "zh-Hans-CN-u-mu-celsius"); + LocaleConfig::SetSystemLanguage(systemLanguage); + LocaleConfig::SetSystemLocale(systemLocale); +} } // namespace I18n } // namespace Global } // namespace OHOS \ No newline at end of file diff --git a/frameworks/intl/test/unittest/locale_config_test.h b/frameworks/intl/test/unittest/locale_config_test.h index 05f8b7a8..82908ba4 100644 --- a/frameworks/intl/test/unittest/locale_config_test.h +++ b/frameworks/intl/test/unittest/locale_config_test.h @@ -41,6 +41,7 @@ int LocaleConfigFuncTest019(void); int LocaleConfigFuncTest020(void); int LocaleConfigFuncTest021(void); int LocaleConfigFuncTest022(void); +int LocaleConfigFuncTest023(void); } // namespace I18n } // namespace Global } // namespace OHOS diff --git a/interfaces/js/kits/src/entity_recognizer_addon.cpp b/interfaces/js/kits/src/entity_recognizer_addon.cpp index 5c7506c0..d553bfc0 100644 --- a/interfaces/js/kits/src/entity_recognizer_addon.cpp +++ b/interfaces/js/kits/src/entity_recognizer_addon.cpp @@ -72,7 +72,7 @@ napi_value EntityRecognizerAddon::constructor(napi_env env, napi_callback_info i } std::string localeStr; if (argc < 1) { - localeStr = LocaleConfig::GetSystemLocale(); + localeStr = LocaleConfig::GetEffectiveLocale(); } else { napi_valuetype valueType = napi_valuetype::napi_undefined; status = napi_typeof(env, argv[0], &valueType); diff --git a/interfaces/js/kits/src/i18n_addon.cpp b/interfaces/js/kits/src/i18n_addon.cpp index 41fd2692..1fbffe56 100644 --- a/interfaces/js/kits/src/i18n_addon.cpp +++ b/interfaces/js/kits/src/i18n_addon.cpp @@ -324,7 +324,7 @@ int I18nAddon::GetParamOfGetTimePeriodName(napi_env env, napi_callback_info info return -1; } if (valueType == napi_valuetype::napi_null || valueType == napi_valuetype::napi_undefined) { - tag = LocaleConfig::GetSystemLocale(); + tag = LocaleConfig::GetEffectiveLocale(); } else if (valueType == napi_valuetype::napi_string) { int code = 0; tag = VariableConvertor::GetString(env, argv[1], code); diff --git a/interfaces/js/kits/src/system_locale_manager_addon.cpp b/interfaces/js/kits/src/system_locale_manager_addon.cpp index fade8db2..05ea662f 100644 --- a/interfaces/js/kits/src/system_locale_manager_addon.cpp +++ b/interfaces/js/kits/src/system_locale_manager_addon.cpp @@ -265,14 +265,14 @@ void SystemLocaleManagerAddon::GetSortOptionsFromJsParam(napi_env env, napi_valu bool isUseLocalName = true; bool isSuggestedFirst = true; if (jsOptions == nullptr) { - localeTag = LocaleConfig::GetSystemLocale(); + localeTag = LocaleConfig::GetEffectiveLocale(); if (isRegion) { isUseLocalName = false; } } else { VariableConvertor::GetOptionValue(env, jsOptions, "locale", localeTag); if (localeTag.length() == 0) { - localeTag = LocaleConfig::GetSystemLocale(); + localeTag = LocaleConfig::GetEffectiveLocale(); } bool isSuccess = VariableConvertor::GetBoolOptionValue(env, jsOptions, "isUseLocalName", isUseLocalName); if (!isSuccess && isRegion) { -- Gitee