diff --git a/frameworks/js/napi/inner/napi_common/BUILD.gn b/frameworks/js/napi/inner/napi_common/BUILD.gn index f1975653555ee860867cdae640ab7f8b76481fcb..eab4e35d7b8df8a43309cb4941a47dd01b25135e 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 7629889f4ee9bc65f7fa79b997547a97b5c69e0e..7815b5b7bec825b84b8d7e0dedf87e75b67034d3 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 40210b1de30c333d0e82e6f6f4cdbc9139789d41..45a6b88b879f2f495876591e44bda19d8fdd9357 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 c781ca15c371fe52c35925aaa812a744ce7dc362..bb7672c53d4ea15a4ba8d061c2c6b98bbc30d20d 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 6f4c1ae42aee99b63a746e716731c4e7c7a8ece8..a1180eeec63081f40f3ba16ba1ad1349cdd10c5d 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 45831ff8001811c05b876e26fa10344ea10802b4..9e70f48b41bfe9aa943bcb835c9f318dfc55ee41 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 4b15f0300e4b04fb6292f0e5f428dd19b2de9abd..8ca6f0c2dd971ee1389c6209630eb2779849a378 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 86b19dd63195f2601a473d87e28483b9c78b29c3..1bf64171c933a1a3145a7b27e63ae7918a06419d 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 18412fa0cfbbd13cf9f38392f04e57a8976d90ee..231934867558708e2eeae6c6fd427746298ce2c9 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 24a5b62347dfc6f5a35eedcdcbf691dbbc19c9ae..60f966570401e744769e7737859f70f25d06bfef 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 d81634e4c72c30a6f53c1387a79f1df9a93d84d3..08a34a8c31f929b3b43c04eabba0749d9ffb617d 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 b3fdcf676e730c8a23bf8341685c2b88466ab208..8f9e1586f3dc2cb0d024f5dba38d6a2faf01284e 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 397054358bd89696d87f7f646514a0a102d1b5db..0aea3979f46cc9074ff93ee0f07143ad3dc19d79 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 0baadfbaedab2dd43ad33c44b0ace15b45890149..0308ed3a4ecf4a6e551c0550d437065e75d9a287 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",