provide default value for SystemLocaleManager

Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
sunyaozu 2023-07-13 10:34:39 +08:00
parent b730f0f3c8
commit 4566d84d0d
2 changed files with 24 additions and 31 deletions

View File

@ -206,7 +206,7 @@ private:
static napi_value GetLanguageInfoArray(napi_env env, napi_callback_info info); static napi_value GetLanguageInfoArray(napi_env env, napi_callback_info info);
static napi_value getCountryInfoArray(napi_env env, napi_callback_info info); static napi_value getCountryInfoArray(napi_env env, napi_callback_info info);
static bool GetStringArrayFromJsParam(napi_env env, napi_value &jsArray, std::vector<std::string> &strArray); static bool GetStringArrayFromJsParam(napi_env env, napi_value &jsArray, std::vector<std::string> &strArray);
static bool GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options); static void GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options, bool isRegion);
static bool GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName, bool &boolVal); static bool GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName, bool &boolVal);
static napi_value CreateLocaleItemArray(napi_env env, const std::vector<LocaleItem> &localeItemList); static napi_value CreateLocaleItemArray(napi_env env, const std::vector<LocaleItem> &localeItemList);
static napi_value CreateLocaleItem(napi_env env, const LocaleItem &localeItem); static napi_value CreateLocaleItem(napi_env env, const LocaleItem &localeItem);

View File

@ -4022,10 +4022,7 @@ napi_value I18nAddon::GetLanguageInfoArray(napi_env env, napi_callback_info info
return nullptr; return nullptr;
} }
SortOptions options; SortOptions options;
isSuccess = GetSortOptionsFromJsParam(env, argv[1], options); GetSortOptionsFromJsParam(env, argv[1], options, false);
if (!isSuccess) {
return nullptr;
}
I18nAddon *obj = nullptr; I18nAddon *obj = nullptr;
status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
if (status != napi_ok || !obj || !obj->systemLocaleManager_) { if (status != napi_ok || !obj || !obj->systemLocaleManager_) {
@ -4055,10 +4052,7 @@ napi_value I18nAddon::getCountryInfoArray(napi_env env, napi_callback_info info)
return nullptr; return nullptr;
} }
SortOptions options; SortOptions options;
isSuccess = GetSortOptionsFromJsParam(env, argv[1], options); GetSortOptionsFromJsParam(env, argv[1], options, true);
if (!isSuccess) {
return nullptr;
}
I18nAddon *obj = nullptr; I18nAddon *obj = nullptr;
status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
if (status != napi_ok || !obj || !obj->systemLocaleManager_) { if (status != napi_ok || !obj || !obj->systemLocaleManager_) {
@ -4166,29 +4160,30 @@ bool I18nAddon::GetStringArrayFromJsParam(napi_env env, napi_value &jsArray, std
return true; return true;
} }
bool I18nAddon::GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options) void I18nAddon::GetSortOptionsFromJsParam(napi_env env, napi_value &jsOptions, SortOptions &options, bool isRegion)
{ {
if (jsOptions == nullptr) {
HiLog::Error(LABEL, "SortOptions js param not found.");
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
return false;
}
std::string localeTag; std::string localeTag;
GetOptionValue(env, jsOptions, "locale", localeTag); bool isUseLocalName = true;
bool isSuggestedFirst = true;
if (jsOptions == nullptr) {
localeTag = LocaleConfig::GetSystemLocale();
if (isRegion) {
isUseLocalName = false;
}
} else {
GetOptionValue(env, jsOptions, "locale", localeTag);
if (localeTag.length() == 0) {
localeTag = LocaleConfig::GetSystemLocale();
}
bool isSuccess = GetBoolOptionValue(env, jsOptions, "isUseLocalName", isUseLocalName);
if (!isSuccess && isRegion) {
isUseLocalName = false;
}
GetBoolOptionValue(env, jsOptions, "isSuggestedFirst", isSuggestedFirst);
}
options.localeTag = localeTag; options.localeTag = localeTag;
bool isUseLocalName;
bool isSuccess = GetBoolOptionValue(env, jsOptions, "isUseLocalName", isUseLocalName);
if (!isSuccess) {
return false;
}
options.isUseLocalName = isUseLocalName; options.isUseLocalName = isUseLocalName;
bool isSuggestedFirst;
isSuccess = GetBoolOptionValue(env, jsOptions, "isSuggestedFirst", isSuggestedFirst);
if (!isSuccess) {
return false;
}
options.isSuggestedFirst = isSuggestedFirst; options.isSuggestedFirst = isSuggestedFirst;
return true;
} }
bool I18nAddon::GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName, bool &boolVal) bool I18nAddon::GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName, bool &boolVal)
@ -4203,15 +4198,13 @@ bool I18nAddon::GetBoolOptionValue(napi_env env, napi_value &options, const std:
bool hasProperty = false; bool hasProperty = false;
status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty); status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty);
if (status != napi_ok || !hasProperty) { if (status != napi_ok || !hasProperty) {
HiLog::Error(LABEL, "option don't have property %{public}s", optionName.c_str()); HiLog::Info(LABEL, "option don't have property %{public}s", optionName.c_str());
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
return false; return false;
} }
napi_value optionValue = nullptr; napi_value optionValue = nullptr;
status = napi_get_named_property(env, options, optionName.c_str(), &optionValue); status = napi_get_named_property(env, options, optionName.c_str(), &optionValue);
if (status != napi_ok) { if (status != napi_ok) {
HiLog::Error(LABEL, "get option %{public}s failed", optionName.c_str()); HiLog::Info(LABEL, "get option %{public}s failed", optionName.c_str());
ErrorUtil::NapiThrow(env, I18N_NOT_VALID, true);
return false; return false;
} }
napi_get_value_bool(env, optionValue, &boolVal); napi_get_value_bool(env, optionValue, &boolVal);