From 8b2943a0ed92372e0691cfbbab6bb10bf602e110 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Wed, 13 Aug 2025 16:10:29 +0800 Subject: [PATCH] add locale Signed-off-by: zhangzezhong --- .../ani/ani_common/include/ani_common_util.h | 1 + .../src/ani_common_configuration.cpp | 11 +++++ .../ani/ani_common/src/ani_common_util.cpp | 41 +++++++++++++++++++ .../ets/@ohos.app.ability.Configuration.ets | 2 + 4 files changed, 55 insertions(+) diff --git a/frameworks/ets/ani/ani_common/include/ani_common_util.h b/frameworks/ets/ani/ani_common/include/ani_common_util.h index 5cac429408d..8c082167a52 100644 --- a/frameworks/ets/ani/ani_common/include/ani_common_util.h +++ b/frameworks/ets/ani/ani_common/include/ani_common_util.h @@ -86,6 +86,7 @@ bool SetRefProperty(ani_env *env, ani_object param, const char *name, ani_ref va bool GetStaticFieldString(ani_env *env, ani_class classObj, const char *fieldName, std::string &value); bool IsValidProperty(ani_env *env, ani_ref param); bool CheckCallerIsSystemApp(); +ani_object WrapLocale(ani_env *env, const std::string &locale); } // namespace AppExecFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME_ANI_COMMON_UTIL_H diff --git a/frameworks/ets/ani/ani_common/src/ani_common_configuration.cpp b/frameworks/ets/ani/ani_common/src/ani_common_configuration.cpp index cb86cab8e0c..b8ffcd2dc40 100644 --- a/frameworks/ets/ani/ani_common/src/ani_common_configuration.cpp +++ b/frameworks/ets/ani/ani_common/src/ani_common_configuration.cpp @@ -43,6 +43,9 @@ void SetBasicConfiguration(ani_env *env, ani_object object, const AppExecFwk::Co std::string str = configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE); env->Object_SetPropertyByName_Ref(object, "language", GetAniString(env, str)); + ani_object localeObj = WrapLocale(env, configuration.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE)); + env->Object_SetPropertyByName_Ref(object, "locale", localeObj); + ani_enum_item colorModeItem = nullptr; OHOS::AAFwk::AniEnumConvertUtil::EnumConvert_NativeToEts(env, COLOR_MODE_ENUM_NAME, @@ -140,6 +143,14 @@ bool UnwrapConfiguration(ani_env *env, ani_object param, Configuration &config) return false; } } + std::string locale { "" }; + if (GetFieldStringByName(env, param, "locale", locale)) { + TAG_LOGD(AAFwkTag::ANI, "The parsed locale part %{public}s", locale.c_str()); + if (!config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LOCALE, locale)) { + TAG_LOGE(AAFwkTag::ANI, "locale parsing failed"); + return false; + } + } ani_double fontSizeScale = 0.0; if (GetFieldDoubleByName(env, param, "fontSizeScale", fontSizeScale)) { diff --git a/frameworks/ets/ani/ani_common/src/ani_common_util.cpp b/frameworks/ets/ani/ani_common/src/ani_common_util.cpp index ce8478c4295..405f0845f9e 100644 --- a/frameworks/ets/ani/ani_common/src/ani_common_util.cpp +++ b/frameworks/ets/ani/ani_common/src/ani_common_util.cpp @@ -1424,5 +1424,46 @@ bool CheckCallerIsSystemApp() } return true; } + +ani_object WrapLocale(ani_env *env, const std::string &locale) +{ + TAG_LOGD(AAFwkTag::ANI, "WrapLocale called"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::JSNAPI, "env is nullptr"); + return nullptr; + } + if (locale.empty()) { + TAG_LOGE(AAFwkTag::ANI, "Locale string is empty"); + return nullptr; + } + ani_class localClass = nullptr; + ani_status status = ANI_ERROR; + if ((status = env->FindClass("Lstd/core/Intl/Locale;", &localClass)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "Find class failed, status: %{public}d", status); + return nullptr; + } + ani_method localCtor = nullptr; + if ((status = env->Class_FindMethod(localClass, "", + "X{C{std.core.Intl.Locale}C{std.core.String}}C{std.core.Intl.LocaleOptions}:", &localCtor)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "Find Locale constructor failed, status: %{public}d", status); + return nullptr; + } + ani_ref undefinedRef; + if ((status = env->GetUndefined(&undefinedRef)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "GetUndefined failed, status: %{public}d", status); + return nullptr; + } + ani_object localStrObj = GetAniString(env, locale); + if (localStrObj == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "Get ani string failed"); + return nullptr; + } + ani_object localeObj = nullptr; + if ((status = env->Object_New(localClass, localCtor, &localeObj, localStrObj, undefinedRef)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "Object_New failed, status: %{public}d", status); + return nullptr; + } + return localeObj; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/ets/ets/@ohos.app.ability.Configuration.ets b/frameworks/ets/ets/@ohos.app.ability.Configuration.ets index fb5972d09d7..eb8e1df8582 100644 --- a/frameworks/ets/ets/@ohos.app.ability.Configuration.ets +++ b/frameworks/ets/ets/@ohos.app.ability.Configuration.ets @@ -27,6 +27,7 @@ export interface Configuration { fontWeightScale?: double; mcc?: string; mnc?: string; + locale?: Intl.Locale; } class ConfigurationImpl implements Configuration { @@ -41,4 +42,5 @@ class ConfigurationImpl implements Configuration { fontWeightScale?: double; mcc?: string; mnc?: string; + locale?: Intl.Locale; } -- Gitee