From d01a3640973c4a74bc621198d539987b79cbb226 Mon Sep 17 00:00:00 2001 From: LY Date: Mon, 26 May 2025 21:46:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E5=9B=BD=E9=99=85=E5=8C=96?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E8=83=BD=E5=8A=9B=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: LY --- frameworks/js/napi/inner/napi_common/BUILD.gn | 1 + .../inner/napi_common/napi_common_util.cpp | 59 +------------------ .../napi/inner/napi_common/napi_common_util.h | 2 - .../application_configuration_manager.cpp | 43 -------------- frameworks/native/appkit/app/main_thread.cpp | 2 +- .../native/appkit/app/ohos_application.cpp | 25 ++++---- .../runtime/js_data_struct_converter.cpp | 39 +----------- .../application_configuration_manager.h | 1 - .../ability_service_extension_test/BUILD.gn | 1 + .../app_service_extension_test/BUILD.gn | 1 + .../cj_ability_stage_object_test/BUILD.gn | 1 + test/unittest/cj_ability_stage_test/BUILD.gn | 1 + test/unittest/cj_ui_ability_test/BUILD.gn | 1 + .../BUILD.gn | 8 +++ 14 files changed, 31 insertions(+), 154 deletions(-) diff --git a/frameworks/js/napi/inner/napi_common/BUILD.gn b/frameworks/js/napi/inner/napi_common/BUILD.gn index f1975653555..eab4e35d7b8 100644 --- a/frameworks/js/napi/inner/napi_common/BUILD.gn +++ b/frameworks/js/napi/inner/napi_common/BUILD.gn @@ -58,6 +58,7 @@ ohos_shared_library("napi_common") { "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", "hilog:libhilog", + "i18n:intl_napi", "ipc:ipc_core", "ipc:ipc_napi", "json:nlohmann_json_static", diff --git a/frameworks/js/napi/inner/napi_common/napi_common_util.cpp b/frameworks/js/napi/inner/napi_common/napi_common_util.cpp index 7629889f4ee..7815b5b7bec 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_util.cpp +++ b/frameworks/js/napi/inner/napi_common/napi_common_util.cpp @@ -17,6 +17,7 @@ #include #include "hilog_tag_wrapper.h" +#include "intl_locale_addon.h" #include "napi_common_data.h" #include "napi_common_error.h" #include "securec.h" @@ -1255,42 +1256,7 @@ std::vector ConvertStringVector(napi_env env, napi_value jsValue) napi_value WrapLocaleToJS(napi_env env, const std::string &locale) { - napi_value global = nullptr; - napi_status status = napi_get_global(env, &global); - if (status != napi_ok || global == nullptr) { - TAG_LOGE(AAFwkTag::JSNAPI, "Load global failed"); - return nullptr; - } - - napi_value intl = nullptr; - status = napi_get_named_property(env, global, "Intl", &intl); - if (status != napi_ok || intl == nullptr) { - TAG_LOGE(AAFwkTag::JSNAPI, "Load Intl failed"); - return nullptr; - } - - napi_value localeConstructor = nullptr; - status = napi_get_named_property(env, intl, "Locale", &localeConstructor); - if (status != napi_ok || localeConstructor == nullptr) { - TAG_LOGE(AAFwkTag::JSNAPI, "Load Intl.Locale constructor failed"); - return nullptr; - } - - napi_value localeJS = WrapStringToJS(env, locale); - if (localeJS == nullptr) { - TAG_LOGE(AAFwkTag::JSNAPI, "Wrap locale failed"); - return nullptr; - } - - size_t argc = 1; - napi_value argv[1] = { localeJS }; - napi_value intlLocale = nullptr; - status = napi_new_instance(env, localeConstructor, argc, argv, &intlLocale); - if (status != napi_ok || intlLocale == nullptr) { - TAG_LOGE(AAFwkTag::JSNAPI, "Create Intl.Locale instance failed"); - return nullptr; - } - return intlLocale; + return OHOS::Global::I18n::IntlLocaleAddon::WrapLocaleToJS(env, locale); } bool UnwrapLocaleByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, std::string &value) @@ -1299,26 +1265,7 @@ bool UnwrapLocaleByPropertyName(napi_env env, napi_value jsObject, const char *p if (jsValue == nullptr) { return false; } - return UnwrapLocaleFromJS(env, jsValue, value); -} - -bool UnwrapLocaleFromJS(napi_env env, napi_value jsValue, std::string &value) -{ - napi_value func = nullptr; - napi_status status = napi_get_named_property(env, jsValue, "toString", &func); - if (status != napi_ok || func == nullptr) { - TAG_LOGE(AAFwkTag::JSNAPI, "Get function toString failed"); - return false; - } - - napi_value locale = nullptr; - status = napi_call_function(env, jsValue, func, 0, nullptr, &locale); - if (status != napi_ok || locale == nullptr) { - TAG_LOGE(AAFwkTag::JSNAPI, "Call function toString failed"); - return false; - } - - return UnwrapStringFromJS2(env, locale, value); + return OHOS::Global::I18n::IntlLocaleAddon::UnwrapLocaleFromJS(env, jsValue, value); } } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/js/napi/inner/napi_common/napi_common_util.h b/frameworks/js/napi/inner/napi_common/napi_common_util.h index 40210b1de30..45a6b88b879 100644 --- a/frameworks/js/napi/inner/napi_common/napi_common_util.h +++ b/frameworks/js/napi/inner/napi_common/napi_common_util.h @@ -271,8 +271,6 @@ std::vector ConvertStringVector(napi_env env, napi_value jsValue); napi_value WrapLocaleToJS(napi_env env, const std::string &locale); bool UnwrapLocaleByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, std::string &value); - -bool UnwrapLocaleFromJS(napi_env env, napi_value jsValue, std::string &value); } // namespace AppExecFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME_NAPI_COMMON_UTIL_H diff --git a/frameworks/native/ability/native/application_configuration_manager.cpp b/frameworks/native/ability/native/application_configuration_manager.cpp index c781ca15c37..bb7672c53d4 100644 --- a/frameworks/native/ability/native/application_configuration_manager.cpp +++ b/frameworks/native/ability/native/application_configuration_manager.cpp @@ -20,7 +20,6 @@ #include #include "application_configuration_manager.h" #include "configuration_utils.h" -#include "unicode/locid.h" namespace OHOS { namespace AbilityRuntime { @@ -128,47 +127,5 @@ std::vector> ApplicationConfi }); return values; } - -std::string ApplicationConfigurationManager::GetUpdatedLocale(const std::string& systemLocale, - const std::string& systemLanguage) -{ - if (systemLocale.empty() || systemLanguage.empty()) { - return ""; - } - - UErrorCode status = U_ZERO_ERROR; - icu::Locale locale = icu::Locale::forLanguageTag(systemLocale.c_str(), status); - if (U_FAILURE(status)) { - return ""; - } - - icu::Locale language = icu::Locale::forLanguageTag(systemLanguage.c_str(), status); - if (U_FAILURE(status)) { - return ""; - } - - std::string extendParamTag; - size_t pos = systemLocale.find("-u-"); - if (pos != std::string::npos) { - extendParamTag = systemLocale.substr(pos); - } - - std::string languageTag = language.getLanguage(); - std::string scriptTag = language.getScript(); - std::string regionTag = locale.getCountry(); - - std::string effectiveLocale = languageTag; - std::string splitor = "-"; - if (!scriptTag.empty()) { - effectiveLocale += splitor + scriptTag; - } - if (!regionTag.empty()) { - effectiveLocale += splitor + regionTag; - } - if (!extendParamTag.empty()) { - effectiveLocale += extendParamTag; - } - return effectiveLocale; -} } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index 6f4c1ae42ae..a1180eeec63 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -1765,7 +1765,7 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con appConfig.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, preferredLanguage); std::string locale = appConfig.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE); appConfig.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE, - AbilityRuntime::ApplicationConfigurationManager::GetUpdatedLocale(locale, preferredLanguage)); + Global::I18n::LocaleConfig::ComputeUpdatedLocale(locale, preferredLanguage)); } HandleConfigByPlugin(appConfig, bundleInfo); std::string systemSizeScale = appConfig.GetItem(AAFwk::GlobalConfigurationKey::APP_FONT_SIZE_SCALE); diff --git a/frameworks/native/appkit/app/ohos_application.cpp b/frameworks/native/appkit/app/ohos_application.cpp index 45831ff8001..9e70f48b41b 100644 --- a/frameworks/native/appkit/app/ohos_application.cpp +++ b/frameworks/native/appkit/app/ohos_application.cpp @@ -35,6 +35,7 @@ #include "hilog_tag_wrapper.h" #include "hitrace_meter.h" #include "iservice_registry.h" +#include "locale_config.h" #include "runtime.h" #include "js_runtime.h" #include "startup_manager.h" @@ -1020,25 +1021,21 @@ bool OHOSApplication::IsUpdateLanguageNeeded(Configuration &config, AbilityRunti return true; } -bool OHOSApplication::IsUpdateLocaleNeeded(const Configuration& updatedConfig, Configuration &config) +bool OHOSApplication::IsUpdateLocaleNeeded(const Configuration& config, Configuration &updatedConfig) { - std::string language = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); - std::string locale = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE); - if (language.empty() && locale.empty()) { + std::string updatedLanguage = updatedConfig.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + std::string updatedLocale = updatedConfig.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE); + if (updatedLocale.empty() && updatedLocale.empty()) { TAG_LOGW(AAFwkTag::APPKIT, "language and locale empty"); return false; } - std::string updatedLocale; - if (!language.empty() && !locale.empty()) { - updatedLocale = ApplicationConfigurationManager::GetUpdatedLocale(locale, language); - } else if (!language.empty()) { - updatedLocale = ApplicationConfigurationManager::GetUpdatedLocale( - updatedConfig.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE), language); - } else { - updatedLocale = ApplicationConfigurationManager::GetUpdatedLocale(locale, - updatedConfig.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE)); + std::string systemLanguage = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); + std::string systemLocale = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE); + std::string modifiedLocale = OHOS::Global::I18n::LocaleConfig::GetUpdatedLocale(systemLocale, systemLanguage, + updatedLocale, updatedLanguage); + if (!modifiedLocale.empty()) { + updatedConfig.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE, modifiedLocale); } - config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE, updatedLocale); return true; } diff --git a/frameworks/native/runtime/js_data_struct_converter.cpp b/frameworks/native/runtime/js_data_struct_converter.cpp index 4b15f0300e4..8ca6f0c2dd9 100644 --- a/frameworks/native/runtime/js_data_struct_converter.cpp +++ b/frameworks/native/runtime/js_data_struct_converter.cpp @@ -17,6 +17,7 @@ #include "common_func.h" #include "configuration_convertor.h" +#include "data_converter.h" #include "hilog_tag_wrapper.h" #include "js_app_process_state.h" #include "js_runtime.h" @@ -186,43 +187,7 @@ napi_value CreateJsHapModuleInfo(napi_env env, const AppExecFwk::HapModuleInfo& napi_value CreateJsLocale(napi_env env, const std::string &locale) { - napi_value global = nullptr; - napi_status status = napi_get_global(env, &global); - if (status != napi_ok || global == nullptr) { - TAG_LOGE(AAFwkTag::JSRUNTIME, "Load global failed"); - return nullptr; - } - - napi_value intl = nullptr; - status = napi_get_named_property(env, global, "Intl", &intl); - if (status != napi_ok || intl == nullptr) { - TAG_LOGE(AAFwkTag::JSRUNTIME, "Load Intl failed"); - return nullptr; - } - - napi_value localeConstructor = nullptr; - status = napi_get_named_property(env, intl, "Locale", &localeConstructor); - if (status != napi_ok || localeConstructor == nullptr) { - TAG_LOGE(AAFwkTag::JSRUNTIME, "Load Intl.Locale constructor failed"); - return nullptr; - } - - napi_value localeJS = nullptr; - status = napi_create_string_utf8(env, locale.c_str(), NAPI_AUTO_LENGTH, &localeJS); - if (status != napi_ok || localeJS == nullptr) { - TAG_LOGE(AAFwkTag::JSRUNTIME, "Create string failed"); - return nullptr; - } - - size_t argc = 1; - napi_value argv[1] = { localeJS }; - napi_value intlLocale = nullptr; - status = napi_new_instance(env, localeConstructor, argc, argv, &intlLocale); - if (status != napi_ok || intlLocale == nullptr) { - TAG_LOGE(AAFwkTag::JSRUNTIME, "Create Intl.Locale instance failed"); - return nullptr; - } - return intlLocale; + return OHOS::Global::I18n::DataConverter::CreateIntlLocale(env, locale); } } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/kits/native/ability/native/application_configuration_manager.h b/interfaces/kits/native/ability/native/application_configuration_manager.h index 86b19dd6319..1bf64171c93 100644 --- a/interfaces/kits/native/ability/native/application_configuration_manager.h +++ b/interfaces/kits/native/ability/native/application_configuration_manager.h @@ -52,7 +52,6 @@ public: void DeleteIgnoreContext(std::shared_ptr context); std::vector> GetIgnoreContext(); std::vector> GetIgnoreResource(); - static std::string GetUpdatedLocale(const std::string& systemLocale, const std::string& systemLanguage); private: SetLevel languageSetLevel_ = SetLevel::System; diff --git a/test/unittest/ability_service_extension_test/BUILD.gn b/test/unittest/ability_service_extension_test/BUILD.gn index 18412fa0cfb..23193486755 100644 --- a/test/unittest/ability_service_extension_test/BUILD.gn +++ b/test/unittest/ability_service_extension_test/BUILD.gn @@ -70,6 +70,7 @@ ohos_unittest("ability_service_extension_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "ipc:ipc_napi", diff --git a/test/unittest/app_service_extension_test/BUILD.gn b/test/unittest/app_service_extension_test/BUILD.gn index 24a5b62347d..60f96657040 100644 --- a/test/unittest/app_service_extension_test/BUILD.gn +++ b/test/unittest/app_service_extension_test/BUILD.gn @@ -70,6 +70,7 @@ ohos_unittest("app_service_extension_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "ipc:ipc_napi", diff --git a/test/unittest/cj_ability_stage_object_test/BUILD.gn b/test/unittest/cj_ability_stage_object_test/BUILD.gn index d81634e4c72..08a34a8c31f 100644 --- a/test/unittest/cj_ability_stage_object_test/BUILD.gn +++ b/test/unittest/cj_ability_stage_object_test/BUILD.gn @@ -80,6 +80,7 @@ ohos_unittest("cj_ability_stage_object_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", diff --git a/test/unittest/cj_ability_stage_test/BUILD.gn b/test/unittest/cj_ability_stage_test/BUILD.gn index b3fdcf676e7..8f9e1586f3d 100644 --- a/test/unittest/cj_ability_stage_test/BUILD.gn +++ b/test/unittest/cj_ability_stage_test/BUILD.gn @@ -85,6 +85,7 @@ ohos_unittest("cj_ability_stage_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", diff --git a/test/unittest/cj_ui_ability_test/BUILD.gn b/test/unittest/cj_ui_ability_test/BUILD.gn index 397054358bd..0aea3979f46 100644 --- a/test/unittest/cj_ui_ability_test/BUILD.gn +++ b/test/unittest/cj_ui_ability_test/BUILD.gn @@ -86,6 +86,7 @@ ohos_unittest("cj_ui_ability_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", diff --git a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn index 0baadfbaeda..0308ed3a4ec 100644 --- a/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn +++ b/test/unittest/frameworks_kits_appkit_native_test/BUILD.gn @@ -136,6 +136,7 @@ ohos_unittest("application_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", @@ -201,6 +202,7 @@ ohos_unittest("context_impl_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", @@ -260,6 +262,7 @@ ohos_unittest("context_impl_second_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", @@ -319,6 +322,7 @@ ohos_unittest("context_impl_third_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", @@ -371,6 +375,7 @@ ohos_unittest("context_container_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", @@ -569,6 +574,7 @@ ohos_unittest("context_deal_test") { "form_fwk:form_manager", "googletest:gmock_main", "googletest:gtest_main", + "i18n:intl_util", "hilog:libhilog", "hitrace:hitrace_meter", "init:libbegetutil", @@ -627,6 +633,7 @@ ohos_unittest("application_impl_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", @@ -772,6 +779,7 @@ ohos_unittest("form_extension_context_test") { "googletest:gtest_main", "hilog:libhilog", "hitrace:hitrace_meter", + "i18n:intl_util", "init:libbegetutil", "ipc:ipc_core", "napi:ace_napi", -- Gitee