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 5cac429408d70a4e37d39b2e2b9ce4c3cb3a12fa..8c082167a52c76b286f7efc1b8e741c3f6ffd21a 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 cb86cab8e0c55088c4bdf0f3cb07685371a1001e..b8ffcd2dc40edd390806952bdebfb00d79d57a99 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 ce8478c4295c358e1abe4a0686f90a93f2d587be..405f0845f9e093e28162d38c59d8a55a58007200 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 fb5972d09d7ace17fde3118c9848f7512a365c2c..eb8e1df85828f7dca0e4ac4a79e45cad850f9268 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; }