上级 a1cee298
......@@ -36,8 +36,8 @@ public:
std::string GetCollation() const;
std::string GetHourCycle() const;
std::string GetNumberingSystem() const;
LocaleInfo *Maximize();
LocaleInfo *Minimize();
std::string Maximize();
std::string Minimize();
std::string GetNumeric() const;
std::string GetCaseFirst() const;
std::string ToString() const;
......
......@@ -220,7 +220,7 @@ Locale LocaleInfo::GetLocale() const
return locale;
}
LocaleInfo *LocaleInfo::Maximize()
std::string LocaleInfo::Maximize()
{
UErrorCode status = U_ZERO_ERROR;
Locale curLocale = locale;
......@@ -230,12 +230,12 @@ LocaleInfo *LocaleInfo::Maximize()
std::string curBaseName = (curLocale.getBaseName() == nullptr) ? "" : curLocale.getBaseName();
std::replace(curBaseName.begin(), curBaseName.end(), '_', '-');
std::string localeTag = curBaseName + restConfigs;
return new LocaleInfo(localeTag, configs);
return localeTag;
}
return this;
return finalLocaleTag;
}
LocaleInfo *LocaleInfo::Minimize()
std::string LocaleInfo::Minimize()
{
UErrorCode status = U_ZERO_ERROR;
Locale curLocale = locale;
......@@ -245,9 +245,9 @@ LocaleInfo *LocaleInfo::Minimize()
std::string curBaseName = (curLocale.getBaseName() == nullptr) ? "" : curLocale.getBaseName();
std::replace(curBaseName.begin(), curBaseName.end(), '_', '-');
std::string localeTag = curBaseName + restConfigs;
return new LocaleInfo(localeTag, configs);
return localeTag;
}
return this;
return finalLocaleTag;
}
bool LocaleInfo::Init()
......
......@@ -260,7 +260,7 @@ void NumberFormat::GetResolvedOptions(std::map<std::string, std::string> &map)
map.insert(std::make_pair("currencyDisplay", currencyDisplayString));
}
if (!signDisplayString.empty()) {
map.insert(std::make_pair("signDisplaysignDisplay", signDisplayString));
map.insert(std::make_pair("signDisplay", signDisplayString));
}
if (!compactDisplay.empty()) {
map.insert(std::make_pair("compactDisplay", compactDisplay));
......
......@@ -109,15 +109,11 @@ HWTEST_F(IntlTest, IntlFuncTest002, TestSize.Level1)
EXPECT_EQ(loc->GetCalendar(), "japanese");
EXPECT_EQ(loc->GetHourCycle(), "h11");
EXPECT_EQ(loc->GetNumberingSystem(), "jpan");
LocaleInfo *minLoc = loc->Minimize();
EXPECT_EQ(minLoc->GetScript(), "");
LocaleInfo *maxLoc = loc->Maximize();
EXPECT_EQ(maxLoc->GetScript(), "Jpan");
EXPECT_EQ(loc->Minimize(), "ja-u-hc-h11-nu-jpan-ca-japanese-co-emoji-kn-true");
EXPECT_EQ(loc->Maximize(), "ja-Jpan-JP-u-hc-h11-nu-jpan-ca-japanese-co-emoji-kn-true");
EXPECT_EQ(loc->GetNumeric(), "true");
EXPECT_EQ(loc->GetCaseFirst(), "");
delete loc;
delete minLoc;
delete maxLoc;
}
/**
......
......@@ -68,6 +68,8 @@ private:
static napi_value GetNumeric(napi_env env, napi_callback_info info);
static napi_value GetCaseFirst(napi_env env, napi_callback_info info);
static napi_value ToString(napi_env env, napi_callback_info info);
static napi_value Maximize(napi_env env, napi_callback_info info);
static napi_value Minimize(napi_env env, napi_callback_info info);
static napi_value FormatDateTime(napi_env env, napi_callback_info info);
static napi_value FormatDateTimeRange(napi_env env, napi_callback_info info);
......
......@@ -32,6 +32,7 @@ namespace I18n {
static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "IntlJs" };
using namespace OHOS::HiviewDFX;
static napi_ref *g_constructor = nullptr;
IntlAddon::IntlAddon() : env_(nullptr), wrapper_(nullptr) {}
......@@ -63,6 +64,8 @@ napi_value IntlAddon::InitLocale(napi_env env, napi_value exports)
DECLARE_NAPI_GETTER("numeric", GetNumeric),
DECLARE_NAPI_GETTER("caseFirst", GetCaseFirst),
DECLARE_NAPI_FUNCTION("toString", ToString),
DECLARE_NAPI_FUNCTION("minimize", Minimize),
DECLARE_NAPI_FUNCTION("maximize", Maximize),
};
napi_value constructor;
......@@ -78,6 +81,16 @@ napi_value IntlAddon::InitLocale(napi_env env, napi_value exports)
HiLog::Error(LABEL, "Set property failed when InitLocale");
return nullptr;
}
g_constructor = new (std::nothrow) napi_ref;
if (g_constructor == nullptr) {
HiLog::Error(LABEL, "Failed to create ref at init");
return nullptr;
}
status = napi_create_reference(env, constructor, 1, g_constructor);
if (status != napi_ok) {
HiLog::Error(LABEL, "Failed to create reference at init");
return nullptr;
}
return exports;
}
......@@ -137,7 +150,7 @@ void GetOptionValue(napi_env env, napi_value options, const std::string &optionN
napi_valuetype type = napi_undefined;
napi_status status = napi_typeof(env, options, &type);
if (status != napi_ok && type != napi_object) {
HiLog::Error(LABEL, "Set option failed, option is not an object");
HiLog::Error(LABEL, "Get option failed, option is not an object");
return;
}
bool hasProperty = false;
......@@ -867,11 +880,11 @@ napi_value IntlAddon::GetNumeric(napi_env env, napi_callback_info info)
return nullptr;
}
std::string value = obj->locale_->GetNumeric();
bool optionBoolValue = (value == "true");
napi_value result;
status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
status = napi_get_boolean(env, optionBoolValue, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create base name string failed");
HiLog::Error(LABEL, "Create numeric boolean value failed");
return nullptr;
}
return result;
......@@ -889,11 +902,10 @@ napi_value IntlAddon::GetCaseFirst(napi_env env, napi_callback_info info)
return nullptr;
}
std::string value = obj->locale_->GetCaseFirst();
bool optionBoolValue = (value == "true");
napi_value result;
status = napi_get_boolean(env, optionBoolValue, &result);
status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create case first boolean value failed");
HiLog::Error(LABEL, "Create caseFirst string failed");
return nullptr;
}
return result;
......@@ -921,6 +933,74 @@ napi_value IntlAddon::ToString(napi_env env, napi_callback_info info)
return result;
}
napi_value IntlAddon::Maximize(napi_env env, napi_callback_info info)
{
// No parameters are needed to get the language.
GET_PARAMS(env, info, 0);
IntlAddon *obj = nullptr;
napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
if (status != napi_ok || obj == nullptr || obj->locale_ == nullptr) {
HiLog::Error(LABEL, "Get Locale object failed");
return nullptr;
}
std::string localeTag = obj->locale_->Maximize();
napi_value constructor;
status = napi_get_reference_value(env, *g_constructor, &constructor);
if (status != napi_ok) {
HiLog::Error(LABEL, "Get locale constructor reference failed");
return nullptr;
}
napi_value result = nullptr;
napi_value arg = nullptr;
status = napi_create_string_utf8(env, localeTag.c_str(), NAPI_AUTO_LENGTH, &arg);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create localeTag string failed");
return nullptr;
}
status = napi_new_instance(env, constructor, 1, &arg, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create new locale instance failed");
return nullptr;
}
return result;
}
napi_value IntlAddon::Minimize(napi_env env, napi_callback_info info)
{
// No parameters are needed to get the language.
GET_PARAMS(env, info, 0);
IntlAddon *obj = nullptr;
napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
if (status != napi_ok || obj == nullptr || obj->locale_ == nullptr) {
HiLog::Error(LABEL, "Get Locale object failed");
return nullptr;
}
std::string localeTag = obj->locale_->Minimize();
napi_value constructor;
status = napi_get_reference_value(env, *g_constructor, &constructor);
if (status != napi_ok) {
HiLog::Error(LABEL, "Get locale constructor reference failed");
return nullptr;
}
napi_value result = nullptr;
napi_value arg = nullptr;
status = napi_create_string_utf8(env, localeTag.c_str(), NAPI_AUTO_LENGTH, &arg);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create localeTag string failed");
return nullptr;
}
status = napi_new_instance(env, constructor, 1, &arg, &result);
if (status != napi_ok) {
HiLog::Error(LABEL, "Create new locale instance failed");
return nullptr;
}
return result;
}
void SetOptionProperties(napi_env env, napi_value &result, std::map<std::string, std::string> &options,
const std::string &option)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册