mirror of
https://gitee.com/openharmony/global_i18n
synced 2024-11-28 01:32:49 +00:00
provide normalizer interfaces
Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
parent
b15651b693
commit
957a12bb49
@ -80,6 +80,7 @@ ohos_shared_library("intl_util") {
|
||||
"src/date_time_format.cpp",
|
||||
"src/i18n_break_iterator.cpp",
|
||||
"src/i18n_calendar.cpp",
|
||||
"src/i18n_normalizer.cpp",
|
||||
"src/i18n_timezone.cpp",
|
||||
"src/index_util.cpp",
|
||||
"src/locale_config.cpp",
|
||||
|
37
frameworks/intl/include/i18n_normalizer.h
Normal file
37
frameworks/intl/include/i18n_normalizer.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_GLOBAL_I18N_I18N_NORMALIZER_H
|
||||
#define OHOS_GLOBAL_I18N_I18N_NORMALIZER_H
|
||||
|
||||
#include "i18n_types.h"
|
||||
#include "normalizer2.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
class I18nNormalizer {
|
||||
public:
|
||||
I18nNormalizer(I18nNormalizerMode mode, I18nErrorCode &errorCode);
|
||||
~I18nNormalizer();
|
||||
std::string Normalize(const char *text, int32_t length, I18nErrorCode &errorCode);
|
||||
|
||||
private:
|
||||
const icu::Normalizer2 *normalizer = nullptr;
|
||||
};
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
||||
#endif
|
36
frameworks/intl/include/i18n_types.h
Normal file
36
frameworks/intl/include/i18n_types.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef OHOS_GLOBAL_I18N_I18N_TYPES_H
|
||||
#define OHOS_GLOBAL_I18N_I18N_TYPES_H
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
enum I18nErrorCode {
|
||||
FAILED = -1,
|
||||
SUCCESS = 0,
|
||||
};
|
||||
|
||||
enum I18nNormalizerMode {
|
||||
NFC = 1,
|
||||
NFD,
|
||||
NFKC,
|
||||
NFKD
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
57
frameworks/intl/src/i18n_normalizer.cpp
Normal file
57
frameworks/intl/src/i18n_normalizer.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "i18n_normalizer.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
I18nNormalizer::I18nNormalizer(I18nNormalizerMode mode, I18nErrorCode &errorCode)
|
||||
{
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
if (mode == I18nNormalizerMode::NFC) {
|
||||
normalizer = icu::Normalizer2::getNFCInstance(status);
|
||||
} else if (mode == I18nNormalizerMode::NFD) {
|
||||
normalizer = icu::Normalizer2::getNFDInstance(status);
|
||||
} else if (mode == I18nNormalizerMode::NFKC) {
|
||||
normalizer = icu::Normalizer2::getNFKCInstance(status);
|
||||
} else {
|
||||
// mode == I18nNormalizerMode::NFKD
|
||||
normalizer = icu::Normalizer2::getNFKDInstance(status);
|
||||
}
|
||||
if (U_FAILURE(status)) {
|
||||
errorCode = I18nErrorCode::FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
I18nNormalizer::~I18nNormalizer()
|
||||
{
|
||||
}
|
||||
|
||||
std::string I18nNormalizer::Normalize(const char *text, int32_t length, I18nErrorCode &errorCode)
|
||||
{
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
icu::UnicodeString input(text, length);
|
||||
icu::UnicodeString output = normalizer->normalize(input, status);
|
||||
if (U_FAILURE(status)) {
|
||||
errorCode = I18nErrorCode::FAILED;
|
||||
return "";
|
||||
}
|
||||
std::string result;
|
||||
output.toUTF8String(result);
|
||||
return result;
|
||||
}
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
@ -14,6 +14,7 @@
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "i18n_normalizer.h"
|
||||
#include "locale_config.h"
|
||||
#include "preferred_language.h"
|
||||
|
||||
@ -61,6 +62,126 @@ HWTEST_F(I18nTest, I18nFuncTest001, TestSize.Level1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: I18nFuncTest002
|
||||
* @tc.desc: Test I18n Normalizer
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(I18nTest, I18nFuncTest002, TestSize.Level1)
|
||||
{
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
I18nNormalizer normalizer(I18nNormalizerMode::NFD, errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
|
||||
string text = "\uFB01"; // \uFB01 is fi
|
||||
string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 3); // 3 is the NFD normalized length of fi.
|
||||
EXPECT_EQ(normalizedText, "\uFB01");
|
||||
|
||||
text = "\u0032\u2075";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 4); // 4 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u0032\u2075");
|
||||
|
||||
text = "\u1E9B\u0323";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 6); // 6 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u017F\u0323\u0307");
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: I18nFuncTest003
|
||||
* @tc.desc: Test I18n Normalizer
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(I18nTest, I18nFuncTest003, TestSize.Level1)
|
||||
{
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
I18nNormalizer normalizer(I18nNormalizerMode::NFC, errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
|
||||
string text = "\uFB01"; // \uFB01 is fi
|
||||
string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 3); // 3 is the NFC normalized length of fi.
|
||||
EXPECT_EQ(normalizedText, "\uFB01");
|
||||
|
||||
text = "\u0032\u2075";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 4); // 4 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u0032\u2075");
|
||||
|
||||
text = "\u1E9B\u0323";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 5); // 5 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u1E9B\u0323");
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: I18nFuncTest004
|
||||
* @tc.desc: Test I18n Normalizer
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(I18nTest, I18nFuncTest004, TestSize.Level1)
|
||||
{
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
I18nNormalizer normalizer(I18nNormalizerMode::NFKD, errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
|
||||
string text = "\uFB01"; // \uFB01 is fi
|
||||
string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 2); // 2 is the NFKD normalized length of fi.
|
||||
EXPECT_EQ(normalizedText, "\u0066\u0069");
|
||||
|
||||
text = "\u0032\u2075";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 2); // 2 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u0032\u0035");
|
||||
|
||||
text = "\u1E9B\u0323";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 5); // 5 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u0073\u0323\u0307");
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: I18nFuncTest005
|
||||
* @tc.desc: Test I18n Normalizer
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(I18nTest, I18nFuncTest005, TestSize.Level1)
|
||||
{
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
I18nNormalizer normalizer(I18nNormalizerMode::NFKC, errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
|
||||
string text = "\uFB01"; // \uFB01 is fi
|
||||
string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 2); // 2 is the NFKC normalized length of fi.
|
||||
EXPECT_EQ(normalizedText, "\u0066\u0069");
|
||||
|
||||
text = "\u0032\u2075";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 2); // 2 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u0032\u0035");
|
||||
|
||||
text = "\u1E9B\u0323";
|
||||
normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
|
||||
EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
|
||||
EXPECT_EQ(normalizedText.length(), 3); // 3 is the expected normalized text length.
|
||||
EXPECT_EQ(normalizedText, "\u1E69");
|
||||
}
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
@ -20,6 +20,10 @@ namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
int I18nFuncTest001(void);
|
||||
int I18nFuncTest002(void);
|
||||
int I18nFuncTest003(void);
|
||||
int I18nFuncTest004(void);
|
||||
int I18nFuncTest005(void);
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
} // namespace OHOS
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "napi/native_api.h"
|
||||
#include "i18n_break_iterator.h"
|
||||
#include "i18n_calendar.h"
|
||||
#include "i18n_normalizer.h"
|
||||
#include "i18n_timezone.h"
|
||||
#include "index_util.h"
|
||||
#include "napi/native_node_api.h"
|
||||
@ -102,6 +103,7 @@ public:
|
||||
static napi_value InitCharacter(napi_env env, napi_value exports);
|
||||
static napi_value InitUtil(napi_env env, napi_value exports);
|
||||
static napi_value System(napi_env env, napi_value exports);
|
||||
static napi_value InitI18nNormalizer(napi_env env, napi_value exports);
|
||||
|
||||
private:
|
||||
static void CreateInitProperties(napi_property_descriptor *properties);
|
||||
@ -186,6 +188,23 @@ private:
|
||||
|
||||
static bool ParseStringParam(napi_env env, napi_value argv, bool throwError, std::string &strParam);
|
||||
|
||||
static napi_status SetEnumValue(napi_env env, napi_value enumObj, const char* enumName, int32_t enumVal);
|
||||
static napi_value CreateI18NNormalizerModeEnum(napi_env env);
|
||||
static napi_value CreateI18nUtilObject(napi_env env);
|
||||
static napi_value CreateI18nNormalizerObject(napi_env env);
|
||||
static napi_value GetI18nNormalizerInstance(napi_env env, napi_callback_info info);
|
||||
static napi_value I18nNormalizerConstructor(napi_env env, napi_callback_info info);
|
||||
static napi_value Normalize(napi_env env, napi_callback_info info);
|
||||
|
||||
static const int32_t NORMALIZER_MODE_NFC = 1;
|
||||
static const int32_t NORMALIZER_MODE_NFD = 2;
|
||||
static const int32_t NORMALIZER_MODE_NFKC = 3;
|
||||
static const int32_t NORMALIZER_MODE_NFKD = 4;
|
||||
static const char *NORMALIZER_MODE_NFC_NAME;
|
||||
static const char *NORMALIZER_MODE_NFD_NAME;
|
||||
static const char *NORMALIZER_MODE_NFKC_NAME;
|
||||
static const char *NORMALIZER_MODE_NFKD_NAME;
|
||||
|
||||
napi_env env_;
|
||||
std::unique_ptr<PhoneNumberFormat> phonenumberfmt_ = nullptr;
|
||||
std::unique_ptr<I18nCalendar> calendar_ = nullptr;
|
||||
@ -193,6 +212,7 @@ private:
|
||||
std::unique_ptr<I18nBreakIterator> brkiter_ = nullptr;
|
||||
std::unique_ptr<IndexUtil> indexUtil_ = nullptr;
|
||||
std::unique_ptr<I18nTimeZone> timezone_ = nullptr;
|
||||
std::unique_ptr<I18nNormalizer> normalizer_ = nullptr;
|
||||
};
|
||||
} // namespace I18n
|
||||
} // namespace Global
|
||||
|
@ -32,11 +32,14 @@ namespace OHOS {
|
||||
namespace Global {
|
||||
namespace I18n {
|
||||
static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "I18nJs" };
|
||||
using namespace OHOS::HiviewDFX;
|
||||
|
||||
static thread_local napi_ref* g_constructor = nullptr;
|
||||
static thread_local napi_ref* g_brkConstructor = nullptr;
|
||||
static thread_local napi_ref* g_timezoneConstructor = nullptr;
|
||||
static thread_local napi_ref g_indexUtilConstructor = nullptr;
|
||||
static thread_local napi_ref* g_transConstructor = nullptr;
|
||||
static thread_local napi_ref* g_normalizerConstructor = nullptr;
|
||||
static std::unordered_map<std::string, UCalendarDateFields> g_fieldsMap {
|
||||
{ "era", UCAL_ERA },
|
||||
{ "year", UCAL_YEAR },
|
||||
@ -76,7 +79,11 @@ static std::unordered_map<std::string, CalendarType> g_typeMap {
|
||||
{ "japanese", CalendarType::JAPANESE },
|
||||
{ "persion", CalendarType::PERSIAN },
|
||||
};
|
||||
using namespace OHOS::HiviewDFX;
|
||||
|
||||
const char *I18nAddon::NORMALIZER_MODE_NFC_NAME = "NFC";
|
||||
const char *I18nAddon::NORMALIZER_MODE_NFD_NAME = "NFD";
|
||||
const char *I18nAddon::NORMALIZER_MODE_NFKC_NAME = "NFKC";
|
||||
const char *I18nAddon::NORMALIZER_MODE_NFKD_NAME = "NFKD";
|
||||
|
||||
I18nAddon::I18nAddon() : env_(nullptr) {}
|
||||
|
||||
@ -122,6 +129,96 @@ napi_value I18nAddon::CreateUnicodeObject(napi_env env)
|
||||
return character;
|
||||
}
|
||||
|
||||
napi_value I18nAddon::CreateI18nUtilObject(napi_env env)
|
||||
{
|
||||
napi_value i18nUtil = nullptr;
|
||||
napi_status status = napi_create_object(env, &i18nUtil);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to create I18nUtil object at init");
|
||||
return nullptr;
|
||||
}
|
||||
napi_property_descriptor i18nUtilProperties[] = {
|
||||
DECLARE_NAPI_FUNCTION("unitConvert", UnitConvert),
|
||||
DECLARE_NAPI_FUNCTION("getDateOrder", GetDateOrder)
|
||||
};
|
||||
status = napi_define_properties(env, i18nUtil, sizeof(i18nUtilProperties) / sizeof(napi_property_descriptor),
|
||||
i18nUtilProperties);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to set properties of I18nUtil at init");
|
||||
return nullptr;
|
||||
}
|
||||
return i18nUtil;
|
||||
}
|
||||
|
||||
napi_value I18nAddon::CreateI18nNormalizerObject(napi_env env)
|
||||
{
|
||||
napi_value i18nNormalizer = nullptr;
|
||||
napi_status status = napi_create_object(env, &i18nNormalizer);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to create I18nNormalizer object at init");
|
||||
return nullptr;
|
||||
}
|
||||
napi_property_descriptor i18nNormalizerProperties[] = {
|
||||
DECLARE_NAPI_FUNCTION("getInstance", GetI18nNormalizerInstance)
|
||||
};
|
||||
status = napi_define_properties(env, i18nNormalizer,
|
||||
sizeof(i18nNormalizerProperties) / sizeof(napi_property_descriptor), i18nNormalizerProperties);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to set properties of I18nNormalizer at init");
|
||||
return nullptr;
|
||||
}
|
||||
return i18nNormalizer;
|
||||
}
|
||||
|
||||
napi_status I18nAddon::SetEnumValue(napi_env env, napi_value enumObj, const char* enumName, int32_t enumVal)
|
||||
{
|
||||
napi_value name = nullptr;
|
||||
napi_status status = napi_create_string_utf8(env, enumName, NAPI_AUTO_LENGTH, &name);
|
||||
if (status != napi_ok) {
|
||||
return status;
|
||||
}
|
||||
napi_value value = nullptr;
|
||||
status = napi_create_int32(env, enumVal, &value);
|
||||
if (status != napi_ok) {
|
||||
return status;
|
||||
}
|
||||
status = napi_set_property(env, enumObj, name, value);
|
||||
if (status != napi_ok) {
|
||||
return status;
|
||||
}
|
||||
status = napi_set_property(env, enumObj, value, name);
|
||||
if (status != napi_ok) {
|
||||
return status;
|
||||
}
|
||||
return napi_ok;
|
||||
}
|
||||
|
||||
napi_value I18nAddon::CreateI18NNormalizerModeEnum(napi_env env)
|
||||
{
|
||||
napi_value i18nNormalizerModel = nullptr;
|
||||
napi_status status = napi_create_object(env, &i18nNormalizerModel);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
status = SetEnumValue(env, i18nNormalizerModel, NORMALIZER_MODE_NFC_NAME, NORMALIZER_MODE_NFC);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
status = SetEnumValue(env, i18nNormalizerModel, NORMALIZER_MODE_NFD_NAME, NORMALIZER_MODE_NFD);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
status = SetEnumValue(env, i18nNormalizerModel, NORMALIZER_MODE_NFKC_NAME, NORMALIZER_MODE_NFKC);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
status = SetEnumValue(env, i18nNormalizerModel, NORMALIZER_MODE_NFKD_NAME, NORMALIZER_MODE_NFKD);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
return i18nNormalizerModel;
|
||||
}
|
||||
|
||||
void I18nAddon::CreateInitProperties(napi_property_descriptor *properties)
|
||||
{
|
||||
properties[0] = DECLARE_NAPI_FUNCTION("getSystemLanguages", GetSystemLanguages); // 0 is properties index
|
||||
@ -163,21 +260,8 @@ void I18nAddon::CreateInitProperties(napi_property_descriptor *properties)
|
||||
napi_value I18nAddon::Init(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_status status = napi_ok;
|
||||
napi_value i18nUtil = nullptr;
|
||||
status = napi_create_object(env, &i18nUtil);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to create util object at init");
|
||||
return nullptr;
|
||||
}
|
||||
napi_property_descriptor i18nUtilProperties[] = {
|
||||
DECLARE_NAPI_FUNCTION("unitConvert", UnitConvert),
|
||||
DECLARE_NAPI_FUNCTION("getDateOrder", GetDateOrder)
|
||||
};
|
||||
status = napi_define_properties(env, i18nUtil,
|
||||
sizeof(i18nUtilProperties) / sizeof(napi_property_descriptor),
|
||||
i18nUtilProperties);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to set properties of i18nUtil at init");
|
||||
napi_value i18nUtil = CreateI18nUtilObject(env);
|
||||
if (i18nUtil == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
napi_value unicode = CreateUnicodeObject(env);
|
||||
@ -196,7 +280,15 @@ napi_value I18nAddon::Init(napi_env env, napi_value exports)
|
||||
if (!system) {
|
||||
return nullptr;
|
||||
}
|
||||
size_t propertiesNums = 30;
|
||||
napi_value i18nNormalizer = CreateI18nNormalizerObject(env);
|
||||
if (i18nNormalizer == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
napi_value i18nNormalizerMode = CreateI18NNormalizerModeEnum(env);
|
||||
if (i18nNormalizerMode == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
size_t propertiesNums = 32;
|
||||
napi_property_descriptor properties[propertiesNums];
|
||||
CreateInitProperties(properties);
|
||||
properties[13] = DECLARE_NAPI_PROPERTY("I18NUtil", i18nUtil); // 13 is properties index
|
||||
@ -204,6 +296,8 @@ napi_value I18nAddon::Init(napi_env env, napi_value exports)
|
||||
properties[24] = DECLARE_NAPI_PROPERTY("Transliterator", transliterator); // 24 is properties index
|
||||
properties[27] = DECLARE_NAPI_PROPERTY("TimeZone", timezone); // 27 is properties index
|
||||
properties[29] = DECLARE_NAPI_PROPERTY("System", system); // 29 is properties index
|
||||
properties[30] = DECLARE_NAPI_PROPERTY("Normalizer", i18nNormalizer); // 30 is properties index
|
||||
properties[31] = DECLARE_NAPI_PROPERTY("NormalizerMode", i18nNormalizerMode); // 31 is properties index
|
||||
status = napi_define_properties(env, exports, propertiesNums, properties);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to set properties at init");
|
||||
@ -3694,6 +3788,146 @@ napi_value I18nAddon::InitUtil(napi_env env, napi_value exports)
|
||||
return exports;
|
||||
}
|
||||
|
||||
napi_value I18nAddon::GetI18nNormalizerInstance(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = 1;
|
||||
napi_value argv[1] = { nullptr };
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
|
||||
|
||||
napi_value constructor = nullptr;
|
||||
status = napi_get_reference_value(env, *g_normalizerConstructor, &constructor);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to create reference of normalizer Constructor");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value result = nullptr;
|
||||
status = napi_new_instance(env, constructor, argc, argv, &result);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "create normalizer instance failed");
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value I18nAddon::InitI18nNormalizer(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_property_descriptor properties[] = {
|
||||
DECLARE_NAPI_FUNCTION("normalize", Normalize)
|
||||
};
|
||||
napi_value constructor = nullptr;
|
||||
napi_status status = napi_define_class(env, "Normalizer", NAPI_AUTO_LENGTH, I18nNormalizerConstructor, nullptr,
|
||||
sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to define class Normalizer at Init");
|
||||
return nullptr;
|
||||
}
|
||||
g_normalizerConstructor = new (std::nothrow) napi_ref;
|
||||
if (!g_normalizerConstructor) {
|
||||
HiLog::Error(LABEL, "Failed to create Normalizer ref at init");
|
||||
return nullptr;
|
||||
}
|
||||
status = napi_create_reference(env, constructor, 1, g_normalizerConstructor);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Failed to create reference g_normalizerConstructor at init.");
|
||||
return nullptr;
|
||||
}
|
||||
return exports;
|
||||
}
|
||||
|
||||
napi_value I18nAddon::I18nNormalizerConstructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = 1;
|
||||
napi_value argv[1] = { nullptr };
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
if (argv[0] == nullptr) {
|
||||
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
|
||||
}
|
||||
napi_valuetype valueType = napi_valuetype::napi_undefined;
|
||||
napi_typeof(env, argv[0], &valueType);
|
||||
|
||||
if (valueType != napi_valuetype::napi_number) {
|
||||
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
|
||||
}
|
||||
int32_t normalizerMode;
|
||||
status = napi_get_value_int32(env, argv[0], &normalizerMode);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
if (normalizerMode != NORMALIZER_MODE_NFC && normalizerMode != NORMALIZER_MODE_NFD &&
|
||||
normalizerMode != NORMALIZER_MODE_NFKC && normalizerMode != NORMALIZER_MODE_NFKD) {
|
||||
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
|
||||
}
|
||||
|
||||
std::unique_ptr<I18nAddon> obj = std::make_unique<I18nAddon>();
|
||||
status =
|
||||
napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nAddon::Destructor, nullptr, nullptr);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
I18nNormalizerMode mode = I18nNormalizerMode(normalizerMode);
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
obj->normalizer_ = std::make_unique<I18nNormalizer>(mode, errorCode);
|
||||
if (errorCode != I18nErrorCode::SUCCESS || !obj->normalizer_) {
|
||||
return nullptr;
|
||||
}
|
||||
obj.release();
|
||||
return thisVar;
|
||||
}
|
||||
|
||||
napi_value I18nAddon::Normalize(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argc = 1;
|
||||
napi_value argv[1] = { 0 };
|
||||
napi_value thisVar = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
|
||||
if (status != napi_ok) {
|
||||
return nullptr;
|
||||
}
|
||||
if (argv[0] == nullptr) {
|
||||
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
|
||||
}
|
||||
napi_valuetype valueType = napi_valuetype::napi_undefined;
|
||||
napi_typeof(env, argv[0], &valueType);
|
||||
if (valueType != napi_valuetype::napi_string) {
|
||||
HiLog::Error(LABEL, "Invalid parameter type");
|
||||
ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, true);
|
||||
}
|
||||
int32_t code = 0;
|
||||
std::string text = GetString(env, argv[0], code);
|
||||
if (code != 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
I18nAddon *obj = nullptr;
|
||||
status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
|
||||
if (status != napi_ok || obj == nullptr || obj->normalizer_ == nullptr) {
|
||||
HiLog::Error(LABEL, "Get Normalizer object failed");
|
||||
return nullptr;
|
||||
}
|
||||
I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
|
||||
std::string normalizedText = obj->normalizer_->Normalize(text.c_str(), static_cast<int32_t>(text.length()),
|
||||
errorCode);
|
||||
if (errorCode != I18nErrorCode::SUCCESS) {
|
||||
return nullptr;
|
||||
}
|
||||
napi_value result = nullptr;
|
||||
status = napi_create_string_utf8(env, normalizedText.c_str(), NAPI_AUTO_LENGTH, &result);
|
||||
if (status != napi_ok) {
|
||||
HiLog::Error(LABEL, "Create result failed");
|
||||
return nullptr;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
napi_value Init(napi_env env, napi_value exports)
|
||||
{
|
||||
napi_value val = I18nAddon::Init(env, exports);
|
||||
@ -3705,6 +3939,7 @@ napi_value Init(napi_env env, napi_value exports)
|
||||
val = I18nAddon::InitTransliterator(env, val);
|
||||
val = I18nAddon::InitCharacter(env, val);
|
||||
val = I18nAddon::InitUtil(env, val);
|
||||
val = I18nAddon::InitI18nNormalizer(env, val);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user