diff --git a/frameworks/intl/include/locale_config.h b/frameworks/intl/include/locale_config.h index ea06ace48863e33e45bb89f882682d7789f75b53..9fb8343acc760178cc43e92057b88b6944fc590e 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 37a043fc474140100eddafdb021b8b039310570e..d7b2f2115757268fd993b03378790f45a219f18c 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 32e44686aa35dd0904235dedd5ab8b904c12d8b1..46ff6ac7799bb243d76eea6903f5dbe93207a151 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 8ceee76ae578f4fe6e61c9a1f69fe58bbc719627..9ad59e62f63f35c7e18c81f945be5aeb1cfb3e05 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 55fec8b99fe059231794b9b4b913feb34b313a01..9e156338e729edd11517afd8cf79c216d8a45cc5 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 9bb6cd310187ea1b869c11650ec359b7f0d64666..214b898db027764f379d64887fdbb0061396ebbc 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 fba8c79c3c6ef88932ab94beeea870c973d1f9d4..d9b788546cf1038a0e69a52bdfadb5d8ec1a941d 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 cc5e5f9a372332ea163d6bf022d0d28ff2aeaf2d..2564fa951717e28848c784ec029a6aae5d74eef6 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 c1494b22fe9d64c91ac52a9bfcb16ae346b624d3..f6bfee6f924fe8e5eab39529121733b4ba39c440 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 b0395299a43252063c242af44e7d74348f3eb83e..e7dbaa9f837084eec849ca0afb74a23cd99843aa 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 1b23e2d73fe2a7290229d92fded92fc251da9739..e4b2e4d014908965b76d6858246abc20db297b52 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 23f511fc5a7733055228679f9e68ef7f70b9bf71..5b8a901cda8cde47c902cc73af58a8e53d34bc05 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 84430250e42c112db471cf28df579a6d91a73ecd..b06ca60ea7700b759ed9304480e5d8be0ff7cf5f 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 02962b2db1ffba2714b47d902411cb7bd6130fca..c9e2bfef2511178a85dc4620f92b1eb878fabfa6 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 033804b4a357968ab1549d2f787bc296f0f70104..b7a5290922b061e421df1ad0e873d19d1283ef7a 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 05f8b7a8c40cb1b9ed98cb4013a729d32ec11040..82908ba4897186e56476a1de29baeabd0032d60b 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 5c7506c0e50720a62729a81f27762ad9b06ec9ff..d553bfc09dfee99c48f6e80e180455386b10ec76 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 41fd269294b6c1c89c4e7e04393636c8cf5b037a..1fbffe568455b5b4669dc438ab68d25c58c6f016 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 fade8db2c42b6e9fb9e297ffdf4caaef397ebfa6..05ea662f3b2447af7cb5d09cfe2ee0883394973c 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) {