19596a2c1Sopenharmony_ci/* 29596a2c1Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 39596a2c1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49596a2c1Sopenharmony_ci * you may not use this file except in compliance with the License. 59596a2c1Sopenharmony_ci * You may obtain a copy of the License at 69596a2c1Sopenharmony_ci * 79596a2c1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89596a2c1Sopenharmony_ci * 99596a2c1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109596a2c1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119596a2c1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129596a2c1Sopenharmony_ci * See the License for the specific language governing permissions and 139596a2c1Sopenharmony_ci * limitations under the License. 149596a2c1Sopenharmony_ci */ 159596a2c1Sopenharmony_ci#include <unordered_map> 169596a2c1Sopenharmony_ci#include <vector> 179596a2c1Sopenharmony_ci 189596a2c1Sopenharmony_ci#include "error_util.h" 199596a2c1Sopenharmony_ci#include "i18n_hilog.h" 209596a2c1Sopenharmony_ci#include "holiday_manager_addon.h" 219596a2c1Sopenharmony_ci#include "entity_recognizer_addon.h" 229596a2c1Sopenharmony_ci#include "i18n_calendar_addon.h" 239596a2c1Sopenharmony_ci#include "i18n_normalizer_addon.h" 249596a2c1Sopenharmony_ci#include "i18n_system_addon.h" 259596a2c1Sopenharmony_ci#include "i18n_timezone_addon.h" 269596a2c1Sopenharmony_ci#include "i18n_unicode_addon.h" 279596a2c1Sopenharmony_ci#include "js_utils.h" 289596a2c1Sopenharmony_ci#include "locale_info.h" 299596a2c1Sopenharmony_ci#include "locale_matcher.h" 309596a2c1Sopenharmony_ci#include "node_api.h" 319596a2c1Sopenharmony_ci#include "system_locale_manager_addon.h" 329596a2c1Sopenharmony_ci#include "unicode/datefmt.h" 339596a2c1Sopenharmony_ci#include "unicode/locid.h" 349596a2c1Sopenharmony_ci#include "unicode/smpdtfmt.h" 359596a2c1Sopenharmony_ci#include "unicode/translit.h" 369596a2c1Sopenharmony_ci#include "utils.h" 379596a2c1Sopenharmony_ci#include "variable_convertor.h" 389596a2c1Sopenharmony_ci#include "i18n_addon.h" 399596a2c1Sopenharmony_ci#include "date_time_sequence.h" 409596a2c1Sopenharmony_ci 419596a2c1Sopenharmony_cinamespace OHOS { 429596a2c1Sopenharmony_cinamespace Global { 439596a2c1Sopenharmony_cinamespace I18n { 449596a2c1Sopenharmony_cistatic thread_local napi_ref* g_brkConstructor = nullptr; 459596a2c1Sopenharmony_cistatic thread_local napi_ref g_indexUtilConstructor = nullptr; 469596a2c1Sopenharmony_cistatic thread_local napi_ref* g_transConstructor = nullptr; 479596a2c1Sopenharmony_ci 489596a2c1Sopenharmony_ciI18nAddon::I18nAddon() : env_(nullptr) {} 499596a2c1Sopenharmony_ci 509596a2c1Sopenharmony_ciI18nAddon::~I18nAddon() 519596a2c1Sopenharmony_ci{ 529596a2c1Sopenharmony_ci PhoneNumberFormat::CloseDynamicHandler(); 539596a2c1Sopenharmony_ci} 549596a2c1Sopenharmony_ci 559596a2c1Sopenharmony_civoid I18nAddon::Destructor(napi_env env, void *nativeObject, void *hint) 569596a2c1Sopenharmony_ci{ 579596a2c1Sopenharmony_ci if (!nativeObject) { 589596a2c1Sopenharmony_ci return; 599596a2c1Sopenharmony_ci } 609596a2c1Sopenharmony_ci delete reinterpret_cast<I18nAddon *>(nativeObject); 619596a2c1Sopenharmony_ci nativeObject = nullptr; 629596a2c1Sopenharmony_ci} 639596a2c1Sopenharmony_ci 649596a2c1Sopenharmony_cinapi_value I18nAddon::InitI18nUtil(napi_env env, napi_value exports) 659596a2c1Sopenharmony_ci{ 669596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 679596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("unitConvert", UnitConvert), 689596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("getDateOrder", GetDateOrder), 699596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("getTimePeriodName", GetTimePeriodName), 709596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("getBestMatchLocale", GetBestMatchLocale), 719596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("getThreeLetterLanguage", GetThreeLetterLanguage), 729596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("getThreeLetterRegion", GetThreeLetterRegion) 739596a2c1Sopenharmony_ci }; 749596a2c1Sopenharmony_ci napi_value constructor = nullptr; 759596a2c1Sopenharmony_ci napi_status status = napi_define_class(env, "I18NUtil", NAPI_AUTO_LENGTH, JSUtils::DefaultConstructor, nullptr, 769596a2c1Sopenharmony_ci sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 779596a2c1Sopenharmony_ci if (status != napi_ok) { 789596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nUtil: Define class failed when InitI18NUtil."); 799596a2c1Sopenharmony_ci return nullptr; 809596a2c1Sopenharmony_ci } 819596a2c1Sopenharmony_ci 829596a2c1Sopenharmony_ci status = napi_set_named_property(env, exports, "I18NUtil", constructor); 839596a2c1Sopenharmony_ci if (status != napi_ok) { 849596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nUtil: Set property failed when InitI18NUtil."); 859596a2c1Sopenharmony_ci return nullptr; 869596a2c1Sopenharmony_ci } 879596a2c1Sopenharmony_ci return exports; 889596a2c1Sopenharmony_ci} 899596a2c1Sopenharmony_ci 909596a2c1Sopenharmony_cinapi_value I18nAddon::Init(napi_env env, napi_value exports) 919596a2c1Sopenharmony_ci{ 929596a2c1Sopenharmony_ci napi_status initStatus = napi_ok; 939596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 949596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getDisplayLanguage", I18nSystemAddon::GetDisplayLanguage), 959596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getDisplayCountry", I18nSystemAddon::GetDisplayCountry), 969596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getSystemLanguage", I18nSystemAddon::GetSystemLanguage), 979596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getSystemRegion", I18nSystemAddon::GetSystemRegion), 989596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getSystemLocale", I18nSystemAddon::GetSystemLocale), 999596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getCalendar", I18nCalendarAddon::GetCalendar), 1009596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("isRTL", IsRTL), 1019596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getLineInstance", GetLineInstance), 1029596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getInstance", GetIndexUtil), 1039596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("addPreferredLanguage", I18nSystemAddon::AddPreferredLanguage), 1049596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("removePreferredLanguage", I18nSystemAddon::RemovePreferredLanguage), 1059596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getPreferredLanguageList", I18nSystemAddon::GetPreferredLanguageList), 1069596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getFirstPreferredLanguage", I18nSystemAddon::GetFirstPreferredLanguage), 1079596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("is24HourClock", I18nSystemAddon::Is24HourClock), 1089596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("set24HourClock", I18nSystemAddon::Set24HourClock), 1099596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getTimeZone", I18nTimeZoneAddon::GetI18nTimeZone), 1109596a2c1Sopenharmony_ci DECLARE_NAPI_PROPERTY("NormalizerMode", I18nNormalizerAddon::CreateI18NNormalizerModeEnum(env, initStatus)) 1119596a2c1Sopenharmony_ci }; 1129596a2c1Sopenharmony_ci initStatus = napi_define_properties(env, exports, sizeof(properties) / sizeof(napi_property_descriptor), 1139596a2c1Sopenharmony_ci properties); 1149596a2c1Sopenharmony_ci if (initStatus != napi_ok) { 1159596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to set properties at init"); 1169596a2c1Sopenharmony_ci return nullptr; 1179596a2c1Sopenharmony_ci } 1189596a2c1Sopenharmony_ci return exports; 1199596a2c1Sopenharmony_ci} 1209596a2c1Sopenharmony_ci 1219596a2c1Sopenharmony_civoid GetOptionMap(napi_env env, napi_value option, std::map<std::string, std::string> &map) 1229596a2c1Sopenharmony_ci{ 1239596a2c1Sopenharmony_ci if (VariableConvertor::CheckNapiValueType(env, option)) { 1249596a2c1Sopenharmony_ci size_t len; 1259596a2c1Sopenharmony_ci napi_get_value_string_utf8(env, option, nullptr, 0, &len); 1269596a2c1Sopenharmony_ci std::vector<char> styleBuf(len + 1); 1279596a2c1Sopenharmony_ci napi_status status = napi_get_value_string_utf8(env, option, styleBuf.data(), len + 1, &len); 1289596a2c1Sopenharmony_ci if (status != napi_ok) { 1299596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetOptionMap: Failed to get string item"); 1309596a2c1Sopenharmony_ci return; 1319596a2c1Sopenharmony_ci } 1329596a2c1Sopenharmony_ci map.insert(std::make_pair("unitDisplay", styleBuf.data())); 1339596a2c1Sopenharmony_ci } 1349596a2c1Sopenharmony_ci} 1359596a2c1Sopenharmony_ci 1369596a2c1Sopenharmony_cinapi_value I18nAddon::UnitConvert(napi_env env, napi_callback_info info) 1379596a2c1Sopenharmony_ci{ 1389596a2c1Sopenharmony_ci size_t argc = 5; 1399596a2c1Sopenharmony_ci napi_value argv[5] = { 0 }; 1409596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 1419596a2c1Sopenharmony_ci void *data = nullptr; 1429596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 1439596a2c1Sopenharmony_ci if (status != napi_ok) { 1449596a2c1Sopenharmony_ci return nullptr; 1459596a2c1Sopenharmony_ci } 1469596a2c1Sopenharmony_ci std::string fromUnit; 1479596a2c1Sopenharmony_ci VariableConvertor::GetOptionValue(env, argv[0], "unit", fromUnit); 1489596a2c1Sopenharmony_ci std::string fromMeasSys; 1499596a2c1Sopenharmony_ci VariableConvertor::GetOptionValue(env, argv[0], "measureSystem", fromMeasSys); 1509596a2c1Sopenharmony_ci std::string toUnit; 1519596a2c1Sopenharmony_ci VariableConvertor::GetOptionValue(env, argv[1], "unit", toUnit); 1529596a2c1Sopenharmony_ci std::string toMeasSys; 1539596a2c1Sopenharmony_ci VariableConvertor::GetOptionValue(env, argv[1], "measureSystem", toMeasSys); 1549596a2c1Sopenharmony_ci double number = 0; 1559596a2c1Sopenharmony_ci napi_get_value_double(env, argv[2], &number); // 2 is the index of value 1569596a2c1Sopenharmony_ci int convertStatus = Convert(number, fromUnit, fromMeasSys, toUnit, toMeasSys); 1579596a2c1Sopenharmony_ci size_t len; 1589596a2c1Sopenharmony_ci napi_get_value_string_utf8(env, argv[3], nullptr, 0, &len); // 3 is the index of value 1599596a2c1Sopenharmony_ci std::vector<char> localeBuf(len + 1); 1609596a2c1Sopenharmony_ci // 3 is the index of value 1619596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[3], localeBuf.data(), len + 1, &len); 1629596a2c1Sopenharmony_ci if (status != napi_ok) { 1639596a2c1Sopenharmony_ci return nullptr; 1649596a2c1Sopenharmony_ci } 1659596a2c1Sopenharmony_ci std::vector<std::string> localeTags; 1669596a2c1Sopenharmony_ci localeTags.push_back(localeBuf.data()); 1679596a2c1Sopenharmony_ci std::map<std::string, std::string> map = {}; 1689596a2c1Sopenharmony_ci map.insert(std::make_pair("style", "unit")); 1699596a2c1Sopenharmony_ci if (!convertStatus) { 1709596a2c1Sopenharmony_ci map.insert(std::make_pair("unit", fromUnit)); 1719596a2c1Sopenharmony_ci } else { 1729596a2c1Sopenharmony_ci map.insert(std::make_pair("unit", toUnit)); 1739596a2c1Sopenharmony_ci } 1749596a2c1Sopenharmony_ci // 4 is the index of value 1759596a2c1Sopenharmony_ci GetOptionMap(env, argv[4], map); 1769596a2c1Sopenharmony_ci std::unique_ptr<NumberFormat> numberFmt = nullptr; 1779596a2c1Sopenharmony_ci numberFmt = std::make_unique<NumberFormat>(localeTags, map); 1789596a2c1Sopenharmony_ci std::string value = numberFmt->Format(number); 1799596a2c1Sopenharmony_ci napi_value result; 1809596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result); 1819596a2c1Sopenharmony_ci if (status != napi_ok) { 1829596a2c1Sopenharmony_ci HILOG_ERROR_I18N("UnitConvert: Failed to create string item"); 1839596a2c1Sopenharmony_ci return nullptr; 1849596a2c1Sopenharmony_ci } 1859596a2c1Sopenharmony_ci return result; 1869596a2c1Sopenharmony_ci} 1879596a2c1Sopenharmony_ci 1889596a2c1Sopenharmony_cinapi_value I18nAddon::GetDateOrder(napi_env env, napi_callback_info info) 1899596a2c1Sopenharmony_ci{ 1909596a2c1Sopenharmony_ci size_t argc = 1; 1919596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 1929596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 1939596a2c1Sopenharmony_ci void *data = nullptr; 1949596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 1959596a2c1Sopenharmony_ci if (status != napi_ok) { 1969596a2c1Sopenharmony_ci return nullptr; 1979596a2c1Sopenharmony_ci } 1989596a2c1Sopenharmony_ci size_t len = 0; 1999596a2c1Sopenharmony_ci napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 2009596a2c1Sopenharmony_ci std::vector<char> languageBuf(len + 1); 2019596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], languageBuf.data(), len + 1, &len); 2029596a2c1Sopenharmony_ci if (status != napi_ok) { 2039596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to get locale string for GetDateOrder"); 2049596a2c1Sopenharmony_ci return nullptr; 2059596a2c1Sopenharmony_ci } 2069596a2c1Sopenharmony_ci std::string languageTag = languageBuf.data(); 2079596a2c1Sopenharmony_ci std::string value = DateTimeSequence::GetDateOrder(languageTag); 2089596a2c1Sopenharmony_ci napi_value result; 2099596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result); 2109596a2c1Sopenharmony_ci if (status != napi_ok) { 2119596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetDateOrder Failed to create string item"); 2129596a2c1Sopenharmony_ci return nullptr; 2139596a2c1Sopenharmony_ci } 2149596a2c1Sopenharmony_ci return result; 2159596a2c1Sopenharmony_ci} 2169596a2c1Sopenharmony_ci 2179596a2c1Sopenharmony_cinapi_value I18nAddon::GetTimePeriodName(napi_env env, napi_callback_info info) 2189596a2c1Sopenharmony_ci{ 2199596a2c1Sopenharmony_ci napi_value result; 2209596a2c1Sopenharmony_ci int32_t hour; 2219596a2c1Sopenharmony_ci std::string localeTag; 2229596a2c1Sopenharmony_ci if (GetParamOfGetTimePeriodName(env, info, localeTag, hour) == -1) { 2239596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetTimePeriodName param error"); 2249596a2c1Sopenharmony_ci napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &result); 2259596a2c1Sopenharmony_ci return result; 2269596a2c1Sopenharmony_ci } 2279596a2c1Sopenharmony_ci 2289596a2c1Sopenharmony_ci UErrorCode icuStatus = U_ZERO_ERROR; 2299596a2c1Sopenharmony_ci icu::Locale locale = icu::Locale::forLanguageTag(localeTag.data(), icuStatus); 2309596a2c1Sopenharmony_ci if (U_FAILURE(icuStatus) || !IsValidLocaleTag(locale)) { 2319596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_VALID, "locale", "a valid locale", true); 2329596a2c1Sopenharmony_ci return nullptr; 2339596a2c1Sopenharmony_ci } 2349596a2c1Sopenharmony_ci icu::SimpleDateFormat* formatter = dynamic_cast<icu::SimpleDateFormat*> 2359596a2c1Sopenharmony_ci (icu::DateFormat::createDateInstance(icu::DateFormat::EStyle::kDefault, locale)); 2369596a2c1Sopenharmony_ci if (!formatter) { 2379596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetTimePeriodName Failed to create SimpleDateFormat"); 2389596a2c1Sopenharmony_ci napi_create_string_utf8(env, "", NAPI_AUTO_LENGTH, &result); 2399596a2c1Sopenharmony_ci return result; 2409596a2c1Sopenharmony_ci } 2419596a2c1Sopenharmony_ci formatter->applyPattern("B"); 2429596a2c1Sopenharmony_ci 2439596a2c1Sopenharmony_ci std::string temp; 2449596a2c1Sopenharmony_ci icu::UnicodeString name; 2459596a2c1Sopenharmony_ci icu::Calendar *calendar = icu::Calendar::createInstance(locale, icuStatus); 2469596a2c1Sopenharmony_ci calendar->set(UCalendarDateFields::UCAL_HOUR_OF_DAY, hour); 2479596a2c1Sopenharmony_ci formatter->format(calendar->getTime(icuStatus), name); 2489596a2c1Sopenharmony_ci name.toUTF8String(temp); 2499596a2c1Sopenharmony_ci napi_create_string_utf8(env, PseudoLocalizationProcessor(temp).c_str(), NAPI_AUTO_LENGTH, &result); 2509596a2c1Sopenharmony_ci delete formatter; 2519596a2c1Sopenharmony_ci delete calendar; 2529596a2c1Sopenharmony_ci return result; 2539596a2c1Sopenharmony_ci} 2549596a2c1Sopenharmony_ci 2559596a2c1Sopenharmony_ciint I18nAddon::GetParamOfGetTimePeriodName(napi_env env, napi_callback_info info, std::string &tag, int32_t &hour) 2569596a2c1Sopenharmony_ci{ 2579596a2c1Sopenharmony_ci size_t argc = 2; 2589596a2c1Sopenharmony_ci napi_value argv[2] = { 0 }; 2599596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 2609596a2c1Sopenharmony_ci void *data = nullptr; 2619596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 2629596a2c1Sopenharmony_ci if (status != napi_ok) { 2639596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetTimePeriodName can't get parameters from getTimePerioudName."); 2649596a2c1Sopenharmony_ci return -1; 2659596a2c1Sopenharmony_ci } else if (argc < 1) { 2669596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "hour", "", true); 2679596a2c1Sopenharmony_ci return -1; 2689596a2c1Sopenharmony_ci } 2699596a2c1Sopenharmony_ci 2709596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 2719596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 2729596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_number) { 2739596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "hour", "number", true); 2749596a2c1Sopenharmony_ci return -1; 2759596a2c1Sopenharmony_ci } 2769596a2c1Sopenharmony_ci status = napi_get_value_int32(env, argv[0], &hour); 2779596a2c1Sopenharmony_ci if (status != napi_ok) { 2789596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetTimePeriodName can't get number from js param"); 2799596a2c1Sopenharmony_ci return -1; 2809596a2c1Sopenharmony_ci } 2819596a2c1Sopenharmony_ci 2829596a2c1Sopenharmony_ci valueType = napi_valuetype::napi_undefined; 2839596a2c1Sopenharmony_ci napi_typeof(env, argv[1], &valueType); 2849596a2c1Sopenharmony_ci if (valueType == napi_valuetype::napi_null || valueType == napi_valuetype::napi_undefined) { 2859596a2c1Sopenharmony_ci tag = LocaleConfig::GetSystemLocale(); 2869596a2c1Sopenharmony_ci } else if (valueType == napi_valuetype::napi_string) { 2879596a2c1Sopenharmony_ci int code = 0; 2889596a2c1Sopenharmony_ci tag = VariableConvertor::GetString(env, argv[1], code); 2899596a2c1Sopenharmony_ci if (code) { 2909596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetTimePeriodName can't get string from js param"); 2919596a2c1Sopenharmony_ci return -1; 2929596a2c1Sopenharmony_ci } 2939596a2c1Sopenharmony_ci } else { 2949596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale", "string", true); 2959596a2c1Sopenharmony_ci return -1; 2969596a2c1Sopenharmony_ci } 2979596a2c1Sopenharmony_ci return 0; 2989596a2c1Sopenharmony_ci} 2999596a2c1Sopenharmony_ci 3009596a2c1Sopenharmony_ciLocaleInfo* ProcessJsParamLocale(napi_env env, napi_value argv) 3019596a2c1Sopenharmony_ci{ 3029596a2c1Sopenharmony_ci int32_t code = 0; 3039596a2c1Sopenharmony_ci std::string localeTag = VariableConvertor::GetString(env, argv, code); 3049596a2c1Sopenharmony_ci if (code != 0) { 3059596a2c1Sopenharmony_ci HILOG_ERROR_I18N("ProcessJsParamLocale: Failed to obtain the parameter."); 3069596a2c1Sopenharmony_ci return nullptr; 3079596a2c1Sopenharmony_ci } 3089596a2c1Sopenharmony_ci UErrorCode icuStatus = U_ZERO_ERROR; 3099596a2c1Sopenharmony_ci icu::Locale locale = icu::Locale::forLanguageTag(localeTag.data(), icuStatus); 3109596a2c1Sopenharmony_ci if (U_FAILURE(icuStatus) || !IsValidLocaleTag(locale)) { 3119596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_VALID, "locale", "a valid locale", true); 3129596a2c1Sopenharmony_ci return nullptr; 3139596a2c1Sopenharmony_ci } 3149596a2c1Sopenharmony_ci return new LocaleInfo(localeTag); 3159596a2c1Sopenharmony_ci} 3169596a2c1Sopenharmony_ci 3179596a2c1Sopenharmony_cibool ProcessJsParamLocaleList(napi_env env, napi_value argv, std::vector<LocaleInfo*> &candidateLocales, 3189596a2c1Sopenharmony_ci LocaleInfo *requestLocale) 3199596a2c1Sopenharmony_ci{ 3209596a2c1Sopenharmony_ci std::vector<std::string> localeTagList; 3219596a2c1Sopenharmony_ci if (!VariableConvertor::GetStringArrayFromJsParam(env, argv, "localeList", localeTagList)) { 3229596a2c1Sopenharmony_ci HILOG_ERROR_I18N("ProcessJsParamLocaleList: Failed to obtain the parameter."); 3239596a2c1Sopenharmony_ci return false; 3249596a2c1Sopenharmony_ci } 3259596a2c1Sopenharmony_ci if (localeTagList.size() == 0) { 3269596a2c1Sopenharmony_ci return true; 3279596a2c1Sopenharmony_ci } 3289596a2c1Sopenharmony_ci for (auto it = localeTagList.begin(); it != localeTagList.end(); ++it) { 3299596a2c1Sopenharmony_ci UErrorCode icuStatus = U_ZERO_ERROR; 3309596a2c1Sopenharmony_ci icu::Locale locale = icu::Locale::forLanguageTag(it->data(), icuStatus); 3319596a2c1Sopenharmony_ci if (U_FAILURE(icuStatus) || !IsValidLocaleTag(locale)) { 3329596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetBestMatchLocale param localeList Invalid: %{public}s.", it->data()); 3339596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_VALID, "locale of localeList", "a valid locale", true); 3349596a2c1Sopenharmony_ci return false; 3359596a2c1Sopenharmony_ci } 3369596a2c1Sopenharmony_ci LocaleInfo *temp = new LocaleInfo(*it); 3379596a2c1Sopenharmony_ci if (LocaleMatcher::Match(requestLocale, temp)) { 3389596a2c1Sopenharmony_ci candidateLocales.push_back(temp); 3399596a2c1Sopenharmony_ci } else { 3409596a2c1Sopenharmony_ci delete temp; 3419596a2c1Sopenharmony_ci } 3429596a2c1Sopenharmony_ci } 3439596a2c1Sopenharmony_ci return true; 3449596a2c1Sopenharmony_ci} 3459596a2c1Sopenharmony_ci 3469596a2c1Sopenharmony_civoid ReleaseParam(LocaleInfo *locale, std::vector<LocaleInfo*> &candidateLocales) 3479596a2c1Sopenharmony_ci{ 3489596a2c1Sopenharmony_ci delete locale; 3499596a2c1Sopenharmony_ci for (auto it = candidateLocales.begin(); it != candidateLocales.end(); ++it) { 3509596a2c1Sopenharmony_ci delete *it; 3519596a2c1Sopenharmony_ci } 3529596a2c1Sopenharmony_ci} 3539596a2c1Sopenharmony_ci 3549596a2c1Sopenharmony_cinapi_value I18nAddon::GetBestMatchLocale(napi_env env, napi_callback_info info) 3559596a2c1Sopenharmony_ci{ 3569596a2c1Sopenharmony_ci size_t argc = 2; 3579596a2c1Sopenharmony_ci napi_value argv[2] = { nullptr }; 3589596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 3599596a2c1Sopenharmony_ci void *data = nullptr; 3609596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 3619596a2c1Sopenharmony_ci if (status != napi_ok || argc < 2) { // 2 is the request param num. 3629596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale or localeList", "", true); 3639596a2c1Sopenharmony_ci return nullptr; 3649596a2c1Sopenharmony_ci } 3659596a2c1Sopenharmony_ci LocaleInfo *requestLocale = ProcessJsParamLocale(env, argv[0]); 3669596a2c1Sopenharmony_ci if (requestLocale == nullptr) { 3679596a2c1Sopenharmony_ci return nullptr; 3689596a2c1Sopenharmony_ci } 3699596a2c1Sopenharmony_ci std::vector<LocaleInfo*> candidateLocales; 3709596a2c1Sopenharmony_ci bool isValidParam = ProcessJsParamLocaleList(env, argv[1], candidateLocales, requestLocale); 3719596a2c1Sopenharmony_ci if (!isValidParam) { 3729596a2c1Sopenharmony_ci ReleaseParam(requestLocale, candidateLocales); 3739596a2c1Sopenharmony_ci return nullptr; 3749596a2c1Sopenharmony_ci } 3759596a2c1Sopenharmony_ci std::string bestMatchLocaleTag = ""; 3769596a2c1Sopenharmony_ci if (candidateLocales.size() > 0) { 3779596a2c1Sopenharmony_ci LocaleInfo *bestMatch = candidateLocales[0]; 3789596a2c1Sopenharmony_ci for (size_t i = 1; i < candidateLocales.size(); ++i) { 3799596a2c1Sopenharmony_ci if (LocaleMatcher::IsMoreSuitable(bestMatch, candidateLocales[i], requestLocale) < 0) { 3809596a2c1Sopenharmony_ci bestMatch = candidateLocales[i]; 3819596a2c1Sopenharmony_ci } 3829596a2c1Sopenharmony_ci } 3839596a2c1Sopenharmony_ci bestMatchLocaleTag = bestMatch->ToString(); 3849596a2c1Sopenharmony_ci } 3859596a2c1Sopenharmony_ci ReleaseParam(requestLocale, candidateLocales); 3869596a2c1Sopenharmony_ci napi_value result = nullptr; 3879596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, bestMatchLocaleTag.c_str(), NAPI_AUTO_LENGTH, &result); 3889596a2c1Sopenharmony_ci if (status != napi_ok) { 3899596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Create format stirng failed."); 3909596a2c1Sopenharmony_ci return nullptr; 3919596a2c1Sopenharmony_ci } 3929596a2c1Sopenharmony_ci return result; 3939596a2c1Sopenharmony_ci} 3949596a2c1Sopenharmony_ci 3959596a2c1Sopenharmony_cinapi_value I18nAddon::GetThreeLetterLanguage(napi_env env, napi_callback_info info) 3969596a2c1Sopenharmony_ci{ 3979596a2c1Sopenharmony_ci size_t argc = 1; 3989596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 3999596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 4009596a2c1Sopenharmony_ci void *data = nullptr; 4019596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 4029596a2c1Sopenharmony_ci if (status != napi_ok) { 4039596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetThreeLetterLanguage napi get param error."); 4049596a2c1Sopenharmony_ci return nullptr; 4059596a2c1Sopenharmony_ci } else if (argc < 1) { 4069596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale", "", true); 4079596a2c1Sopenharmony_ci return nullptr; 4089596a2c1Sopenharmony_ci } 4099596a2c1Sopenharmony_ci 4109596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 4119596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 4129596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 4139596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale", "string", true); 4149596a2c1Sopenharmony_ci return nullptr; 4159596a2c1Sopenharmony_ci } 4169596a2c1Sopenharmony_ci 4179596a2c1Sopenharmony_ci int32_t code = 0; 4189596a2c1Sopenharmony_ci std::string languageTag = VariableConvertor::GetString(env, argv[0], code); 4199596a2c1Sopenharmony_ci if (code != 0) { 4209596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetThreeLetterLanguage: Failed to obtain the parameter."); 4219596a2c1Sopenharmony_ci return nullptr; 4229596a2c1Sopenharmony_ci } 4239596a2c1Sopenharmony_ci 4249596a2c1Sopenharmony_ci std::string language = GetISO3Language(languageTag); 4259596a2c1Sopenharmony_ci 4269596a2c1Sopenharmony_ci napi_value result; 4279596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, language.c_str(), NAPI_AUTO_LENGTH, &result); 4289596a2c1Sopenharmony_ci if (status != napi_ok || language == "") { 4299596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetThreeLetterLanguage create string fail or empty"); 4309596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_VALID, "locale", "a valid locale", true); 4319596a2c1Sopenharmony_ci return nullptr; 4329596a2c1Sopenharmony_ci } 4339596a2c1Sopenharmony_ci return result; 4349596a2c1Sopenharmony_ci} 4359596a2c1Sopenharmony_ci 4369596a2c1Sopenharmony_cinapi_value I18nAddon::GetThreeLetterRegion(napi_env env, napi_callback_info info) 4379596a2c1Sopenharmony_ci{ 4389596a2c1Sopenharmony_ci size_t argc = 1; 4399596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 4409596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 4419596a2c1Sopenharmony_ci void *data = nullptr; 4429596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 4439596a2c1Sopenharmony_ci if (status != napi_ok) { 4449596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetThreeLetterRegion: Failed to obtain the parameter."); 4459596a2c1Sopenharmony_ci return nullptr; 4469596a2c1Sopenharmony_ci } else if (argc < 1) { 4479596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale", "", true); 4489596a2c1Sopenharmony_ci return nullptr; 4499596a2c1Sopenharmony_ci } 4509596a2c1Sopenharmony_ci 4519596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 4529596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 4539596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 4549596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, "locale", "string", true); 4559596a2c1Sopenharmony_ci return nullptr; 4569596a2c1Sopenharmony_ci } 4579596a2c1Sopenharmony_ci 4589596a2c1Sopenharmony_ci int32_t code = 0; 4599596a2c1Sopenharmony_ci std::string regionTag = VariableConvertor::GetString(env, argv[0], code); 4609596a2c1Sopenharmony_ci if (code != 0) { 4619596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetThreeLetterRegion: Failed to obtain the parameter."); 4629596a2c1Sopenharmony_ci return nullptr; 4639596a2c1Sopenharmony_ci } 4649596a2c1Sopenharmony_ci 4659596a2c1Sopenharmony_ci std::string country = GetISO3Country(regionTag); 4669596a2c1Sopenharmony_ci 4679596a2c1Sopenharmony_ci napi_value result; 4689596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, country.c_str(), NAPI_AUTO_LENGTH, &result); 4699596a2c1Sopenharmony_ci if (status != napi_ok || country == "") { 4709596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetThreeLetterRegion create string fail or empty"); 4719596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_VALID, "locale", "a valid locale", true); 4729596a2c1Sopenharmony_ci return nullptr; 4739596a2c1Sopenharmony_ci } 4749596a2c1Sopenharmony_ci return result; 4759596a2c1Sopenharmony_ci} 4769596a2c1Sopenharmony_ci 4779596a2c1Sopenharmony_cinapi_value I18nAddon::InitI18nTransliterator(napi_env env, napi_value exports) 4789596a2c1Sopenharmony_ci{ 4799596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 4809596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("transform", Transform), 4819596a2c1Sopenharmony_ci }; 4829596a2c1Sopenharmony_ci napi_value constructor = nullptr; 4839596a2c1Sopenharmony_ci napi_status status = napi_define_class(env, "Transliterator", NAPI_AUTO_LENGTH, I18nTransliteratorConstructor, 4849596a2c1Sopenharmony_ci nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 4859596a2c1Sopenharmony_ci if (status != napi_ok) { 4869596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nTransliterator: Failed to define transliterator class at Init"); 4879596a2c1Sopenharmony_ci return nullptr; 4889596a2c1Sopenharmony_ci } 4899596a2c1Sopenharmony_ci exports = I18nAddon::InitTransliterator(env, exports); 4909596a2c1Sopenharmony_ci g_transConstructor = new (std::nothrow) napi_ref; 4919596a2c1Sopenharmony_ci if (!g_transConstructor) { 4929596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nTransliterator: Failed to create trans ref at init"); 4939596a2c1Sopenharmony_ci return nullptr; 4949596a2c1Sopenharmony_ci } 4959596a2c1Sopenharmony_ci status = napi_create_reference(env, constructor, 1, g_transConstructor); 4969596a2c1Sopenharmony_ci if (status != napi_ok) { 4979596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nTransliterator: Failed to create trans reference at init"); 4989596a2c1Sopenharmony_ci return nullptr; 4999596a2c1Sopenharmony_ci } 5009596a2c1Sopenharmony_ci return exports; 5019596a2c1Sopenharmony_ci} 5029596a2c1Sopenharmony_ci 5039596a2c1Sopenharmony_cinapi_value I18nAddon::InitTransliterator(napi_env env, napi_value exports) 5049596a2c1Sopenharmony_ci{ 5059596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 5069596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("getAvailableIDs", GetAvailableIDs), 5079596a2c1Sopenharmony_ci DECLARE_NAPI_STATIC_FUNCTION("getInstance", GetTransliteratorInstance) 5089596a2c1Sopenharmony_ci }; 5099596a2c1Sopenharmony_ci napi_value constructor = nullptr; 5109596a2c1Sopenharmony_ci napi_status status = napi_define_class(env, "I18nTransliterator", NAPI_AUTO_LENGTH, JSUtils::DefaultConstructor, 5119596a2c1Sopenharmony_ci nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 5129596a2c1Sopenharmony_ci if (status != napi_ok) { 5139596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitTransliterator: Failed to define class Transliterator."); 5149596a2c1Sopenharmony_ci return nullptr; 5159596a2c1Sopenharmony_ci } 5169596a2c1Sopenharmony_ci status = napi_set_named_property(env, exports, "Transliterator", constructor); 5179596a2c1Sopenharmony_ci if (status != napi_ok) { 5189596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitTransliterator: Set property failed When InitTransliterator."); 5199596a2c1Sopenharmony_ci return nullptr; 5209596a2c1Sopenharmony_ci } 5219596a2c1Sopenharmony_ci return exports; 5229596a2c1Sopenharmony_ci} 5239596a2c1Sopenharmony_ci 5249596a2c1Sopenharmony_cinapi_value I18nAddon::I18nTransliteratorConstructor(napi_env env, napi_callback_info info) 5259596a2c1Sopenharmony_ci{ 5269596a2c1Sopenharmony_ci size_t argc = 1; 5279596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 5289596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 5299596a2c1Sopenharmony_ci void *data = nullptr; 5309596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 5319596a2c1Sopenharmony_ci if (status != napi_ok) { 5329596a2c1Sopenharmony_ci return nullptr; 5339596a2c1Sopenharmony_ci } 5349596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 5359596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 5369596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 5379596a2c1Sopenharmony_ci HILOG_ERROR_I18N("I18nTransliteratorConstructor: Parameter type does not match"); 5389596a2c1Sopenharmony_ci return nullptr; 5399596a2c1Sopenharmony_ci } 5409596a2c1Sopenharmony_ci int32_t code = 0; 5419596a2c1Sopenharmony_ci std::string idTag = VariableConvertor::GetString(env, argv[0], code); 5429596a2c1Sopenharmony_ci if (code) { 5439596a2c1Sopenharmony_ci return nullptr; 5449596a2c1Sopenharmony_ci } 5459596a2c1Sopenharmony_ci std::unique_ptr<I18nAddon> obj = nullptr; 5469596a2c1Sopenharmony_ci obj = std::make_unique<I18nAddon>(); 5479596a2c1Sopenharmony_ci status = 5489596a2c1Sopenharmony_ci napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nAddon::Destructor, nullptr, nullptr); 5499596a2c1Sopenharmony_ci if (status != napi_ok) { 5509596a2c1Sopenharmony_ci HILOG_ERROR_I18N("I18nTransliteratorConstructor: TransliteratorConstructor: Wrap II18nAddon failed"); 5519596a2c1Sopenharmony_ci return nullptr; 5529596a2c1Sopenharmony_ci } 5539596a2c1Sopenharmony_ci if (!obj->InitTransliteratorContext(env, info, idTag)) { 5549596a2c1Sopenharmony_ci obj.release(); 5559596a2c1Sopenharmony_ci return nullptr; 5569596a2c1Sopenharmony_ci } 5579596a2c1Sopenharmony_ci obj.release(); 5589596a2c1Sopenharmony_ci return thisVar; 5599596a2c1Sopenharmony_ci} 5609596a2c1Sopenharmony_ci 5619596a2c1Sopenharmony_cibool I18nAddon::InitTransliteratorContext(napi_env env, napi_callback_info info, const std::string &idTag) 5629596a2c1Sopenharmony_ci{ 5639596a2c1Sopenharmony_ci UErrorCode status = U_ZERO_ERROR; 5649596a2c1Sopenharmony_ci icu::UnicodeString unistr = icu::UnicodeString::fromUTF8(idTag); 5659596a2c1Sopenharmony_ci icu::Transliterator *trans = icu::Transliterator::createInstance(unistr, UTransDirection::UTRANS_FORWARD, status); 5669596a2c1Sopenharmony_ci if (U_FAILURE(status) || (trans == nullptr)) { 5679596a2c1Sopenharmony_ci return false; 5689596a2c1Sopenharmony_ci } 5699596a2c1Sopenharmony_ci transliterator_ = std::unique_ptr<icu::Transliterator>(trans); 5709596a2c1Sopenharmony_ci return transliterator_ != nullptr; 5719596a2c1Sopenharmony_ci} 5729596a2c1Sopenharmony_ci 5739596a2c1Sopenharmony_cinapi_value I18nAddon::Transform(napi_env env, napi_callback_info info) 5749596a2c1Sopenharmony_ci{ 5759596a2c1Sopenharmony_ci size_t argc = 1; 5769596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 5779596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 5789596a2c1Sopenharmony_ci void *data = nullptr; 5799596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 5809596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 5819596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 5829596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->transliterator_) { 5839596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Get Transliterator object failed"); 5849596a2c1Sopenharmony_ci return nullptr; 5859596a2c1Sopenharmony_ci } 5869596a2c1Sopenharmony_ci if (!argv[0]) { 5879596a2c1Sopenharmony_ci return nullptr; 5889596a2c1Sopenharmony_ci } 5899596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 5909596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 5919596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 5929596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Transform: Parameter type does not match"); 5939596a2c1Sopenharmony_ci return nullptr; 5949596a2c1Sopenharmony_ci } 5959596a2c1Sopenharmony_ci size_t len = 0; 5969596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 5979596a2c1Sopenharmony_ci if (status != napi_ok) { 5989596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Transform: Get field length failed napi_get_value_string_utf8"); 5999596a2c1Sopenharmony_ci return nullptr; 6009596a2c1Sopenharmony_ci } 6019596a2c1Sopenharmony_ci std::vector<char> buf(len + 1); 6029596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len); 6039596a2c1Sopenharmony_ci if (status != napi_ok) { 6049596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Transform: Get string value failed"); 6059596a2c1Sopenharmony_ci return nullptr; 6069596a2c1Sopenharmony_ci } 6079596a2c1Sopenharmony_ci icu::UnicodeString unistr = icu::UnicodeString::fromUTF8(buf.data()); 6089596a2c1Sopenharmony_ci obj->transliterator_->transliterate(unistr); 6099596a2c1Sopenharmony_ci std::string temp; 6109596a2c1Sopenharmony_ci unistr.toUTF8String(temp); 6119596a2c1Sopenharmony_ci napi_value value; 6129596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, temp.c_str(), NAPI_AUTO_LENGTH, &value); 6139596a2c1Sopenharmony_ci if (status != napi_ok) { 6149596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Transform: Get field length failed napi_create_string_utf8"); 6159596a2c1Sopenharmony_ci return nullptr; 6169596a2c1Sopenharmony_ci } 6179596a2c1Sopenharmony_ci return value; 6189596a2c1Sopenharmony_ci} 6199596a2c1Sopenharmony_ci 6209596a2c1Sopenharmony_cinapi_value I18nAddon::GetAvailableIDs(napi_env env, napi_callback_info info) 6219596a2c1Sopenharmony_ci{ 6229596a2c1Sopenharmony_ci size_t argc = 0; 6239596a2c1Sopenharmony_ci napi_value *argv = nullptr; 6249596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 6259596a2c1Sopenharmony_ci void *data = nullptr; 6269596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 6279596a2c1Sopenharmony_ci if (status != napi_ok) { 6289596a2c1Sopenharmony_ci return nullptr; 6299596a2c1Sopenharmony_ci } 6309596a2c1Sopenharmony_ci UErrorCode icuStatus = U_ZERO_ERROR; 6319596a2c1Sopenharmony_ci icu::StringEnumeration *strenum = icu::Transliterator::getAvailableIDs(icuStatus); 6329596a2c1Sopenharmony_ci if (U_FAILURE(icuStatus)) { 6339596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to get available ids"); 6349596a2c1Sopenharmony_ci if (strenum) { 6359596a2c1Sopenharmony_ci delete strenum; 6369596a2c1Sopenharmony_ci } 6379596a2c1Sopenharmony_ci return nullptr; 6389596a2c1Sopenharmony_ci } 6399596a2c1Sopenharmony_ci 6409596a2c1Sopenharmony_ci napi_value result = nullptr; 6419596a2c1Sopenharmony_ci napi_create_array(env, &result); 6429596a2c1Sopenharmony_ci uint32_t i = 0; 6439596a2c1Sopenharmony_ci const char *temp = nullptr; 6449596a2c1Sopenharmony_ci if (strenum == nullptr) { 6459596a2c1Sopenharmony_ci return nullptr; 6469596a2c1Sopenharmony_ci } 6479596a2c1Sopenharmony_ci while ((temp = strenum->next(nullptr, icuStatus)) != nullptr) { 6489596a2c1Sopenharmony_ci if (U_FAILURE(icuStatus)) { 6499596a2c1Sopenharmony_ci break; 6509596a2c1Sopenharmony_ci } 6519596a2c1Sopenharmony_ci napi_value val = nullptr; 6529596a2c1Sopenharmony_ci napi_create_string_utf8(env, temp, strlen(temp), &val); 6539596a2c1Sopenharmony_ci napi_set_element(env, result, i, val); 6549596a2c1Sopenharmony_ci ++i; 6559596a2c1Sopenharmony_ci } 6569596a2c1Sopenharmony_ci delete strenum; 6579596a2c1Sopenharmony_ci return result; 6589596a2c1Sopenharmony_ci} 6599596a2c1Sopenharmony_ci 6609596a2c1Sopenharmony_cinapi_value I18nAddon::GetTransliteratorInstance(napi_env env, napi_callback_info info) 6619596a2c1Sopenharmony_ci{ 6629596a2c1Sopenharmony_ci size_t argc = 1; // retrieve 2 arguments 6639596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 6649596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 6659596a2c1Sopenharmony_ci void *data = nullptr; 6669596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 6679596a2c1Sopenharmony_ci napi_value constructor = nullptr; 6689596a2c1Sopenharmony_ci napi_status status = napi_get_reference_value(env, *g_transConstructor, &constructor); 6699596a2c1Sopenharmony_ci if (status != napi_ok) { 6709596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to create reference at GetCalendar"); 6719596a2c1Sopenharmony_ci return nullptr; 6729596a2c1Sopenharmony_ci } 6739596a2c1Sopenharmony_ci napi_value result = nullptr; 6749596a2c1Sopenharmony_ci status = napi_new_instance(env, constructor, 1, argv, &result); // 2 arguments 6759596a2c1Sopenharmony_ci if (status != napi_ok) { 6769596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Get Transliterator create instance failed"); 6779596a2c1Sopenharmony_ci return nullptr; 6789596a2c1Sopenharmony_ci } 6799596a2c1Sopenharmony_ci return result; 6809596a2c1Sopenharmony_ci} 6819596a2c1Sopenharmony_ci 6829596a2c1Sopenharmony_cinapi_value I18nAddon::IsRTL(napi_env env, napi_callback_info info) 6839596a2c1Sopenharmony_ci{ 6849596a2c1Sopenharmony_ci size_t argc = 1; 6859596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 6869596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 6879596a2c1Sopenharmony_ci void *data = nullptr; 6889596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 6899596a2c1Sopenharmony_ci if (status != napi_ok) { 6909596a2c1Sopenharmony_ci return nullptr; 6919596a2c1Sopenharmony_ci } 6929596a2c1Sopenharmony_ci size_t len = 0; 6939596a2c1Sopenharmony_ci napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 6949596a2c1Sopenharmony_ci std::vector<char> localeBuf(len + 1); 6959596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len); 6969596a2c1Sopenharmony_ci if (status != napi_ok) { 6979596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsRTL: Failed to get string item"); 6989596a2c1Sopenharmony_ci return nullptr; 6999596a2c1Sopenharmony_ci } 7009596a2c1Sopenharmony_ci bool isRTL = LocaleConfig::IsRTL(localeBuf.data()); 7019596a2c1Sopenharmony_ci napi_value result = nullptr; 7029596a2c1Sopenharmony_ci status = napi_get_boolean(env, isRTL, &result); 7039596a2c1Sopenharmony_ci if (status != napi_ok) { 7049596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsRTL failed"); 7059596a2c1Sopenharmony_ci return nullptr; 7069596a2c1Sopenharmony_ci } 7079596a2c1Sopenharmony_ci return result; 7089596a2c1Sopenharmony_ci} 7099596a2c1Sopenharmony_ci 7109596a2c1Sopenharmony_cinapi_value I18nAddon::InitPhoneNumberFormat(napi_env env, napi_value exports) 7119596a2c1Sopenharmony_ci{ 7129596a2c1Sopenharmony_ci napi_status status = napi_ok; 7139596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 7149596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("isValidNumber", IsValidPhoneNumber), 7159596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("format", FormatPhoneNumber), 7169596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getLocationName", GetLocationName) 7179596a2c1Sopenharmony_ci }; 7189596a2c1Sopenharmony_ci 7199596a2c1Sopenharmony_ci napi_value constructor; 7209596a2c1Sopenharmony_ci status = napi_define_class(env, "PhoneNumberFormat", NAPI_AUTO_LENGTH, PhoneNumberFormatConstructor, nullptr, 7219596a2c1Sopenharmony_ci sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 7229596a2c1Sopenharmony_ci if (status != napi_ok) { 7239596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitPhoneNumberFormat: Define class failed when InitPhoneNumberFormat"); 7249596a2c1Sopenharmony_ci return nullptr; 7259596a2c1Sopenharmony_ci } 7269596a2c1Sopenharmony_ci 7279596a2c1Sopenharmony_ci status = napi_set_named_property(env, exports, "PhoneNumberFormat", constructor); 7289596a2c1Sopenharmony_ci if (status != napi_ok) { 7299596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Set property failed when InitPhoneNumberFormat"); 7309596a2c1Sopenharmony_ci return nullptr; 7319596a2c1Sopenharmony_ci } 7329596a2c1Sopenharmony_ci return exports; 7339596a2c1Sopenharmony_ci} 7349596a2c1Sopenharmony_ci 7359596a2c1Sopenharmony_cinapi_value I18nAddon::PhoneNumberFormatConstructor(napi_env env, napi_callback_info info) 7369596a2c1Sopenharmony_ci{ 7379596a2c1Sopenharmony_ci size_t argc = 2; 7389596a2c1Sopenharmony_ci napi_value argv[2] = { 0 }; 7399596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 7409596a2c1Sopenharmony_ci void *data = nullptr; 7419596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 7429596a2c1Sopenharmony_ci if (status != napi_ok) { 7439596a2c1Sopenharmony_ci return nullptr; 7449596a2c1Sopenharmony_ci } 7459596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 7469596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 7479596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 7489596a2c1Sopenharmony_ci HILOG_ERROR_I18N("PhoneNumberFormatConstructor: Parameter type does not match"); 7499596a2c1Sopenharmony_ci return nullptr; 7509596a2c1Sopenharmony_ci } 7519596a2c1Sopenharmony_ci size_t len = 0; 7529596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 7539596a2c1Sopenharmony_ci if (status != napi_ok) { 7549596a2c1Sopenharmony_ci HILOG_ERROR_I18N("PhoneNumberFormatConstructor: Get country tag length failed"); 7559596a2c1Sopenharmony_ci return nullptr; 7569596a2c1Sopenharmony_ci } 7579596a2c1Sopenharmony_ci std::vector<char> country (len + 1); 7589596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], country.data(), len + 1, &len); 7599596a2c1Sopenharmony_ci if (status != napi_ok) { 7609596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Get country tag failed"); 7619596a2c1Sopenharmony_ci return nullptr; 7629596a2c1Sopenharmony_ci } 7639596a2c1Sopenharmony_ci std::map<std::string, std::string> options; 7649596a2c1Sopenharmony_ci std::string typeStr; 7659596a2c1Sopenharmony_ci VariableConvertor::GetOptionValue(env, argv[1], "type", typeStr); 7669596a2c1Sopenharmony_ci options.insert(std::make_pair("type", typeStr)); 7679596a2c1Sopenharmony_ci std::unique_ptr<I18nAddon> obj = nullptr; 7689596a2c1Sopenharmony_ci obj = std::make_unique<I18nAddon>(); 7699596a2c1Sopenharmony_ci status = napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), 7709596a2c1Sopenharmony_ci I18nAddon::Destructor, nullptr, nullptr); 7719596a2c1Sopenharmony_ci if (status != napi_ok) { 7729596a2c1Sopenharmony_ci HILOG_ERROR_I18N("PhoneNumberFormatConstructor: Wrap I18nAddon failed"); 7739596a2c1Sopenharmony_ci return nullptr; 7749596a2c1Sopenharmony_ci } 7759596a2c1Sopenharmony_ci if (!obj->InitPhoneNumberFormatContext(env, info, country.data(), options)) { 7769596a2c1Sopenharmony_ci return nullptr; 7779596a2c1Sopenharmony_ci } 7789596a2c1Sopenharmony_ci obj.release(); 7799596a2c1Sopenharmony_ci return thisVar; 7809596a2c1Sopenharmony_ci} 7819596a2c1Sopenharmony_ci 7829596a2c1Sopenharmony_cibool I18nAddon::InitPhoneNumberFormatContext(napi_env env, napi_callback_info info, const std::string &country, 7839596a2c1Sopenharmony_ci const std::map<std::string, std::string> &options) 7849596a2c1Sopenharmony_ci{ 7859596a2c1Sopenharmony_ci napi_value global = nullptr; 7869596a2c1Sopenharmony_ci napi_status status = napi_get_global(env, &global); 7879596a2c1Sopenharmony_ci if (status != napi_ok) { 7889596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitPhoneNumberFormatContext: Get global failed"); 7899596a2c1Sopenharmony_ci return false; 7909596a2c1Sopenharmony_ci } 7919596a2c1Sopenharmony_ci env_ = env; 7929596a2c1Sopenharmony_ci phonenumberfmt_ = PhoneNumberFormat::CreateInstance(country, options); 7939596a2c1Sopenharmony_ci 7949596a2c1Sopenharmony_ci return phonenumberfmt_ != nullptr; 7959596a2c1Sopenharmony_ci} 7969596a2c1Sopenharmony_ci 7979596a2c1Sopenharmony_cinapi_value I18nAddon::IsValidPhoneNumber(napi_env env, napi_callback_info info) 7989596a2c1Sopenharmony_ci{ 7999596a2c1Sopenharmony_ci size_t argc = 1; 8009596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 8019596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 8029596a2c1Sopenharmony_ci void *data = nullptr; 8039596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 8049596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 8059596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 8069596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 8079596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsValidPhoneNumber: Parameter type does not match"); 8089596a2c1Sopenharmony_ci return nullptr; 8099596a2c1Sopenharmony_ci } 8109596a2c1Sopenharmony_ci 8119596a2c1Sopenharmony_ci size_t len = 0; 8129596a2c1Sopenharmony_ci napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 8139596a2c1Sopenharmony_ci if (status != napi_ok) { 8149596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsValidPhoneNumber: Get phone number length failed"); 8159596a2c1Sopenharmony_ci return nullptr; 8169596a2c1Sopenharmony_ci } 8179596a2c1Sopenharmony_ci std::vector<char> buf(len + 1); 8189596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len); 8199596a2c1Sopenharmony_ci if (status != napi_ok) { 8209596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsValidPhoneNumber: Get phone number failed"); 8219596a2c1Sopenharmony_ci return nullptr; 8229596a2c1Sopenharmony_ci } 8239596a2c1Sopenharmony_ci 8249596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 8259596a2c1Sopenharmony_ci status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 8269596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->phonenumberfmt_) { 8279596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsValidPhoneNumber: GetPhoneNumberFormat object failed"); 8289596a2c1Sopenharmony_ci return nullptr; 8299596a2c1Sopenharmony_ci } 8309596a2c1Sopenharmony_ci 8319596a2c1Sopenharmony_ci bool isValid = obj->phonenumberfmt_->isValidPhoneNumber(buf.data()); 8329596a2c1Sopenharmony_ci 8339596a2c1Sopenharmony_ci napi_value result = nullptr; 8349596a2c1Sopenharmony_ci status = napi_get_boolean(env, isValid, &result); 8359596a2c1Sopenharmony_ci if (status != napi_ok) { 8369596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsValidPhoneNumber: Create boolean failed"); 8379596a2c1Sopenharmony_ci return nullptr; 8389596a2c1Sopenharmony_ci } 8399596a2c1Sopenharmony_ci 8409596a2c1Sopenharmony_ci return result; 8419596a2c1Sopenharmony_ci} 8429596a2c1Sopenharmony_ci 8439596a2c1Sopenharmony_cinapi_value I18nAddon::GetLocationName(napi_env env, napi_callback_info info) 8449596a2c1Sopenharmony_ci{ 8459596a2c1Sopenharmony_ci size_t argc = 2; 8469596a2c1Sopenharmony_ci napi_value argv[2] = {0, 0}; 8479596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 8489596a2c1Sopenharmony_ci void *data = nullptr; 8499596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 8509596a2c1Sopenharmony_ci 8519596a2c1Sopenharmony_ci int32_t code = 0; 8529596a2c1Sopenharmony_ci std::string number = VariableConvertor::GetString(env, argv[0], code); 8539596a2c1Sopenharmony_ci if (code) { 8549596a2c1Sopenharmony_ci return nullptr; 8559596a2c1Sopenharmony_ci } 8569596a2c1Sopenharmony_ci std::string language = VariableConvertor::GetString(env, argv[1], code); 8579596a2c1Sopenharmony_ci if (code) { 8589596a2c1Sopenharmony_ci return nullptr; 8599596a2c1Sopenharmony_ci } 8609596a2c1Sopenharmony_ci 8619596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 8629596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 8639596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->phonenumberfmt_) { 8649596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetLocationName: GetPhoneNumberFormat object failed"); 8659596a2c1Sopenharmony_ci return nullptr; 8669596a2c1Sopenharmony_ci } 8679596a2c1Sopenharmony_ci 8689596a2c1Sopenharmony_ci std::string resStr = obj->phonenumberfmt_->getLocationName(number.data(), language.data()); 8699596a2c1Sopenharmony_ci napi_value result = nullptr; 8709596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, resStr.c_str(), NAPI_AUTO_LENGTH, &result); 8719596a2c1Sopenharmony_ci if (status != napi_ok) { 8729596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Create result string failed"); 8739596a2c1Sopenharmony_ci return nullptr; 8749596a2c1Sopenharmony_ci } 8759596a2c1Sopenharmony_ci 8769596a2c1Sopenharmony_ci return result; 8779596a2c1Sopenharmony_ci} 8789596a2c1Sopenharmony_ci 8799596a2c1Sopenharmony_cinapi_value I18nAddon::FormatPhoneNumber(napi_env env, napi_callback_info info) 8809596a2c1Sopenharmony_ci{ 8819596a2c1Sopenharmony_ci size_t argc = 1; 8829596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 8839596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 8849596a2c1Sopenharmony_ci void *data = nullptr; 8859596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 8869596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 8879596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 8889596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 8899596a2c1Sopenharmony_ci HILOG_ERROR_I18N("FormatPhoneNumber: Parameter type does not match"); 8909596a2c1Sopenharmony_ci return nullptr; 8919596a2c1Sopenharmony_ci } 8929596a2c1Sopenharmony_ci 8939596a2c1Sopenharmony_ci size_t len = 0; 8949596a2c1Sopenharmony_ci napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 8959596a2c1Sopenharmony_ci if (status != napi_ok) { 8969596a2c1Sopenharmony_ci HILOG_ERROR_I18N("FormatPhoneNumber: Get phone number length failed"); 8979596a2c1Sopenharmony_ci return nullptr; 8989596a2c1Sopenharmony_ci } 8999596a2c1Sopenharmony_ci std::vector<char> buf(len + 1); 9009596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len); 9019596a2c1Sopenharmony_ci if (status != napi_ok) { 9029596a2c1Sopenharmony_ci HILOG_ERROR_I18N("FormatPhoneNumber: Get phone number failed"); 9039596a2c1Sopenharmony_ci return nullptr; 9049596a2c1Sopenharmony_ci } 9059596a2c1Sopenharmony_ci 9069596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 9079596a2c1Sopenharmony_ci status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 9089596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->phonenumberfmt_) { 9099596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Get PhoneNumberFormat object failed"); 9109596a2c1Sopenharmony_ci return nullptr; 9119596a2c1Sopenharmony_ci } 9129596a2c1Sopenharmony_ci 9139596a2c1Sopenharmony_ci std::string formattedPhoneNumber = obj->phonenumberfmt_->format(buf.data()); 9149596a2c1Sopenharmony_ci 9159596a2c1Sopenharmony_ci napi_value result = nullptr; 9169596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, formattedPhoneNumber.c_str(), NAPI_AUTO_LENGTH, &result); 9179596a2c1Sopenharmony_ci if (status != napi_ok) { 9189596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Create format phone number failed"); 9199596a2c1Sopenharmony_ci return nullptr; 9209596a2c1Sopenharmony_ci } 9219596a2c1Sopenharmony_ci return result; 9229596a2c1Sopenharmony_ci} 9239596a2c1Sopenharmony_ci 9249596a2c1Sopenharmony_cinapi_value I18nAddon::InitI18nIndexUtil(napi_env env, napi_value exports) 9259596a2c1Sopenharmony_ci{ 9269596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 9279596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getIndexList", GetIndexList), 9289596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("addLocale", AddLocale), 9299596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getIndex", GetIndex) 9309596a2c1Sopenharmony_ci }; 9319596a2c1Sopenharmony_ci 9329596a2c1Sopenharmony_ci napi_value constructor = nullptr; 9339596a2c1Sopenharmony_ci napi_status status = napi_define_class(env, "IndexUtil", NAPI_AUTO_LENGTH, I18nIndexUtilConstructor, nullptr, 9349596a2c1Sopenharmony_ci sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 9359596a2c1Sopenharmony_ci if (status != napi_ok) { 9369596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nIndexUtil: Define class failed when InitI18nIndexUtil."); 9379596a2c1Sopenharmony_ci return nullptr; 9389596a2c1Sopenharmony_ci } 9399596a2c1Sopenharmony_ci exports = I18nAddon::InitIndexUtil(env, exports); 9409596a2c1Sopenharmony_ci status = napi_create_reference(env, constructor, 1, &g_indexUtilConstructor); 9419596a2c1Sopenharmony_ci if (status != napi_ok) { 9429596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nIndexUtil: Failed to create reference at init."); 9439596a2c1Sopenharmony_ci return nullptr; 9449596a2c1Sopenharmony_ci } 9459596a2c1Sopenharmony_ci return exports; 9469596a2c1Sopenharmony_ci} 9479596a2c1Sopenharmony_ci 9489596a2c1Sopenharmony_cinapi_value I18nAddon::InitIndexUtil(napi_env env, napi_value exports) 9499596a2c1Sopenharmony_ci{ 9509596a2c1Sopenharmony_ci napi_property_descriptor properties[] = {}; 9519596a2c1Sopenharmony_ci napi_value constructor = nullptr; 9529596a2c1Sopenharmony_ci napi_status status = napi_define_class(env, "I18nIndexUtil", NAPI_AUTO_LENGTH, JSUtils::DefaultConstructor, 9539596a2c1Sopenharmony_ci nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 9549596a2c1Sopenharmony_ci if (status != napi_ok) { 9559596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitIndexUtil: Failed to define class IndexUtil."); 9569596a2c1Sopenharmony_ci return nullptr; 9579596a2c1Sopenharmony_ci } 9589596a2c1Sopenharmony_ci status = napi_set_named_property(env, exports, "IndexUtil", constructor); 9599596a2c1Sopenharmony_ci if (status != napi_ok) { 9609596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitIndexUtil: Set property failed When InitIndexUtil."); 9619596a2c1Sopenharmony_ci return nullptr; 9629596a2c1Sopenharmony_ci } 9639596a2c1Sopenharmony_ci return exports; 9649596a2c1Sopenharmony_ci} 9659596a2c1Sopenharmony_ci 9669596a2c1Sopenharmony_cinapi_value I18nAddon::I18nBreakIteratorConstructor(napi_env env, napi_callback_info info) 9679596a2c1Sopenharmony_ci{ 9689596a2c1Sopenharmony_ci size_t argc = 1; 9699596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 9709596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 9719596a2c1Sopenharmony_ci void *data = nullptr; 9729596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 9739596a2c1Sopenharmony_ci if (status != napi_ok) { 9749596a2c1Sopenharmony_ci return nullptr; 9759596a2c1Sopenharmony_ci } 9769596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 9779596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 9789596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 9799596a2c1Sopenharmony_ci HILOG_ERROR_I18N("BreakIteratorConstructor: Parameter type does not match"); 9809596a2c1Sopenharmony_ci return nullptr; 9819596a2c1Sopenharmony_ci } 9829596a2c1Sopenharmony_ci int32_t code = 0; 9839596a2c1Sopenharmony_ci std::string localeTag = VariableConvertor::GetString(env, argv[0], code); 9849596a2c1Sopenharmony_ci if (code) { 9859596a2c1Sopenharmony_ci return nullptr; 9869596a2c1Sopenharmony_ci } 9879596a2c1Sopenharmony_ci std::unique_ptr<I18nAddon> obj = nullptr; 9889596a2c1Sopenharmony_ci obj = std::make_unique<I18nAddon>(); 9899596a2c1Sopenharmony_ci status = 9909596a2c1Sopenharmony_ci napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nAddon::Destructor, nullptr, nullptr); 9919596a2c1Sopenharmony_ci if (status != napi_ok) { 9929596a2c1Sopenharmony_ci HILOG_ERROR_I18N("BreakIteratorConstructor: Wrap II18nAddon failed"); 9939596a2c1Sopenharmony_ci return nullptr; 9949596a2c1Sopenharmony_ci } 9959596a2c1Sopenharmony_ci obj->brkiter_ = std::make_unique<I18nBreakIterator>(localeTag); 9969596a2c1Sopenharmony_ci if (!obj->brkiter_) { 9979596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Wrap BreakIterator failed"); 9989596a2c1Sopenharmony_ci return nullptr; 9999596a2c1Sopenharmony_ci } 10009596a2c1Sopenharmony_ci obj.release(); 10019596a2c1Sopenharmony_ci return thisVar; 10029596a2c1Sopenharmony_ci} 10039596a2c1Sopenharmony_ci 10049596a2c1Sopenharmony_cinapi_value I18nAddon::InitI18nBreakIterator(napi_env env, napi_value exports) 10059596a2c1Sopenharmony_ci{ 10069596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 10079596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("current", Current), 10089596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("first", First), 10099596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("last", Last), 10109596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("next", Next), 10119596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("previous", Previous), 10129596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("setLineBreakText", SetText), 10139596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("following", Following), 10149596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("getLineBreakText", GetText), 10159596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("isBoundary", IsBoundary) 10169596a2c1Sopenharmony_ci }; 10179596a2c1Sopenharmony_ci napi_value constructor = nullptr; 10189596a2c1Sopenharmony_ci napi_status status = napi_define_class(env, "BreakIterator", NAPI_AUTO_LENGTH, I18nBreakIteratorConstructor, 10199596a2c1Sopenharmony_ci nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 10209596a2c1Sopenharmony_ci if (status != napi_ok) { 10219596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nBreakIterator: Failed to define class BreakIterator at Init"); 10229596a2c1Sopenharmony_ci return nullptr; 10239596a2c1Sopenharmony_ci } 10249596a2c1Sopenharmony_ci exports = I18nAddon::InitBreakIterator(env, exports); 10259596a2c1Sopenharmony_ci g_brkConstructor = new (std::nothrow) napi_ref; 10269596a2c1Sopenharmony_ci if (!g_brkConstructor) { 10279596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nBreakIterator: Failed to create brkiterator ref at init"); 10289596a2c1Sopenharmony_ci return nullptr; 10299596a2c1Sopenharmony_ci } 10309596a2c1Sopenharmony_ci status = napi_create_reference(env, constructor, 1, g_brkConstructor); 10319596a2c1Sopenharmony_ci if (status != napi_ok) { 10329596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitI18nBreakIterator: Failed to create reference g_brkConstructor at init"); 10339596a2c1Sopenharmony_ci return nullptr; 10349596a2c1Sopenharmony_ci } 10359596a2c1Sopenharmony_ci return exports; 10369596a2c1Sopenharmony_ci} 10379596a2c1Sopenharmony_ci 10389596a2c1Sopenharmony_cinapi_value I18nAddon::InitBreakIterator(napi_env env, napi_value exports) 10399596a2c1Sopenharmony_ci{ 10409596a2c1Sopenharmony_ci napi_property_descriptor properties[] = {}; 10419596a2c1Sopenharmony_ci napi_value constructor = nullptr; 10429596a2c1Sopenharmony_ci napi_status status = napi_define_class(env, "I18nBreakIterator", NAPI_AUTO_LENGTH, JSUtils::DefaultConstructor, 10439596a2c1Sopenharmony_ci nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 10449596a2c1Sopenharmony_ci if (status != napi_ok) { 10459596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitBreakIterator: Failed to define class BreakIterator."); 10469596a2c1Sopenharmony_ci return nullptr; 10479596a2c1Sopenharmony_ci } 10489596a2c1Sopenharmony_ci status = napi_set_named_property(env, exports, "BreakIterator", constructor); 10499596a2c1Sopenharmony_ci if (status != napi_ok) { 10509596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitBreakIterator: Set property failed When InitBreakIterator."); 10519596a2c1Sopenharmony_ci return nullptr; 10529596a2c1Sopenharmony_ci } 10539596a2c1Sopenharmony_ci return exports; 10549596a2c1Sopenharmony_ci} 10559596a2c1Sopenharmony_ci 10569596a2c1Sopenharmony_cinapi_value I18nAddon::GetLineInstance(napi_env env, napi_callback_info info) 10579596a2c1Sopenharmony_ci{ 10589596a2c1Sopenharmony_ci size_t argc = 1; 10599596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 10609596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 10619596a2c1Sopenharmony_ci void *data = nullptr; 10629596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 10639596a2c1Sopenharmony_ci napi_value constructor = nullptr; 10649596a2c1Sopenharmony_ci napi_status status = napi_get_reference_value(env, *g_brkConstructor, &constructor); 10659596a2c1Sopenharmony_ci if (status != napi_ok) { 10669596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to create reference at GetLineInstance"); 10679596a2c1Sopenharmony_ci return nullptr; 10689596a2c1Sopenharmony_ci } 10699596a2c1Sopenharmony_ci if (!argv[0]) { 10709596a2c1Sopenharmony_ci return nullptr; 10719596a2c1Sopenharmony_ci } 10729596a2c1Sopenharmony_ci napi_value result = nullptr; 10739596a2c1Sopenharmony_ci status = napi_new_instance(env, constructor, 1, argv, &result); // 1 arguments 10749596a2c1Sopenharmony_ci if (status != napi_ok) { 10759596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetLineInstance create instance failed"); 10769596a2c1Sopenharmony_ci return nullptr; 10779596a2c1Sopenharmony_ci } 10789596a2c1Sopenharmony_ci return result; 10799596a2c1Sopenharmony_ci} 10809596a2c1Sopenharmony_ci 10819596a2c1Sopenharmony_cinapi_value I18nAddon::Current(napi_env env, napi_callback_info info) 10829596a2c1Sopenharmony_ci{ 10839596a2c1Sopenharmony_ci size_t argc = 0; 10849596a2c1Sopenharmony_ci napi_value *argv = nullptr; 10859596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 10869596a2c1Sopenharmony_ci void *data = nullptr; 10879596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 10889596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 10899596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 10909596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 10919596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Current: Get BreakIterator object failed"); 10929596a2c1Sopenharmony_ci return nullptr; 10939596a2c1Sopenharmony_ci } 10949596a2c1Sopenharmony_ci int value = obj->brkiter_->Current(); 10959596a2c1Sopenharmony_ci napi_value result = nullptr; 10969596a2c1Sopenharmony_ci status = napi_create_int32(env, value, &result); 10979596a2c1Sopenharmony_ci if (status != napi_ok) { 10989596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Current: Create int32_t value failed"); 10999596a2c1Sopenharmony_ci return nullptr; 11009596a2c1Sopenharmony_ci } 11019596a2c1Sopenharmony_ci return result; 11029596a2c1Sopenharmony_ci} 11039596a2c1Sopenharmony_ci 11049596a2c1Sopenharmony_cinapi_value I18nAddon::First(napi_env env, napi_callback_info info) 11059596a2c1Sopenharmony_ci{ 11069596a2c1Sopenharmony_ci size_t argc = 0; 11079596a2c1Sopenharmony_ci napi_value *argv = nullptr; 11089596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 11099596a2c1Sopenharmony_ci void *data = nullptr; 11109596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 11119596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 11129596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 11139596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 11149596a2c1Sopenharmony_ci HILOG_ERROR_I18N("First: Get BreakIterator object failed"); 11159596a2c1Sopenharmony_ci return nullptr; 11169596a2c1Sopenharmony_ci } 11179596a2c1Sopenharmony_ci int value = obj->brkiter_->First(); 11189596a2c1Sopenharmony_ci napi_value result = nullptr; 11199596a2c1Sopenharmony_ci status = napi_create_int32(env, value, &result); 11209596a2c1Sopenharmony_ci if (status != napi_ok) { 11219596a2c1Sopenharmony_ci HILOG_ERROR_I18N("First: Create int32_t value failed"); 11229596a2c1Sopenharmony_ci return nullptr; 11239596a2c1Sopenharmony_ci } 11249596a2c1Sopenharmony_ci return result; 11259596a2c1Sopenharmony_ci} 11269596a2c1Sopenharmony_ci 11279596a2c1Sopenharmony_cinapi_value I18nAddon::Last(napi_env env, napi_callback_info info) 11289596a2c1Sopenharmony_ci{ 11299596a2c1Sopenharmony_ci size_t argc = 0; 11309596a2c1Sopenharmony_ci napi_value *argv = nullptr; 11319596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 11329596a2c1Sopenharmony_ci void *data = nullptr; 11339596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 11349596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 11359596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 11369596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 11379596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Last: Get BreakIterator object failed"); 11389596a2c1Sopenharmony_ci return nullptr; 11399596a2c1Sopenharmony_ci } 11409596a2c1Sopenharmony_ci int value = obj->brkiter_->Last(); 11419596a2c1Sopenharmony_ci napi_value result = nullptr; 11429596a2c1Sopenharmony_ci status = napi_create_int32(env, value, &result); 11439596a2c1Sopenharmony_ci if (status != napi_ok) { 11449596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Last: Create int32_t value failed"); 11459596a2c1Sopenharmony_ci return nullptr; 11469596a2c1Sopenharmony_ci } 11479596a2c1Sopenharmony_ci return result; 11489596a2c1Sopenharmony_ci} 11499596a2c1Sopenharmony_ci 11509596a2c1Sopenharmony_cinapi_value I18nAddon::Previous(napi_env env, napi_callback_info info) 11519596a2c1Sopenharmony_ci{ 11529596a2c1Sopenharmony_ci size_t argc = 0; 11539596a2c1Sopenharmony_ci napi_value *argv = nullptr; 11549596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 11559596a2c1Sopenharmony_ci void *data = nullptr; 11569596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 11579596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 11589596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 11599596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 11609596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Previous: Get BreakIterator object failed"); 11619596a2c1Sopenharmony_ci return nullptr; 11629596a2c1Sopenharmony_ci } 11639596a2c1Sopenharmony_ci int value = obj->brkiter_->Previous(); 11649596a2c1Sopenharmony_ci napi_value result = nullptr; 11659596a2c1Sopenharmony_ci status = napi_create_int32(env, value, &result); 11669596a2c1Sopenharmony_ci if (status != napi_ok) { 11679596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Previous: Create int32_t value failed"); 11689596a2c1Sopenharmony_ci return nullptr; 11699596a2c1Sopenharmony_ci } 11709596a2c1Sopenharmony_ci return result; 11719596a2c1Sopenharmony_ci} 11729596a2c1Sopenharmony_ci 11739596a2c1Sopenharmony_cinapi_value I18nAddon::Next(napi_env env, napi_callback_info info) 11749596a2c1Sopenharmony_ci{ 11759596a2c1Sopenharmony_ci size_t argc = 1; 11769596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 11779596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 11789596a2c1Sopenharmony_ci void *data = nullptr; 11799596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 11809596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 11819596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 11829596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 11839596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Next: Get BreakIterator object failed"); 11849596a2c1Sopenharmony_ci return nullptr; 11859596a2c1Sopenharmony_ci } 11869596a2c1Sopenharmony_ci int value = 1; 11879596a2c1Sopenharmony_ci if (VariableConvertor::CheckNapiValueType(env, argv[0])) { 11889596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 11899596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 11909596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_number) { 11919596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Next: Parameter type does not match"); 11929596a2c1Sopenharmony_ci return nullptr; 11939596a2c1Sopenharmony_ci } 11949596a2c1Sopenharmony_ci status = napi_get_value_int32(env, argv[0], &value); 11959596a2c1Sopenharmony_ci if (status != napi_ok) { 11969596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Next: Retrieve next value failed"); 11979596a2c1Sopenharmony_ci return nullptr; 11989596a2c1Sopenharmony_ci } 11999596a2c1Sopenharmony_ci } 12009596a2c1Sopenharmony_ci value = obj->brkiter_->Next(value); 12019596a2c1Sopenharmony_ci napi_value result = nullptr; 12029596a2c1Sopenharmony_ci status = napi_create_int32(env, value, &result); 12039596a2c1Sopenharmony_ci if (status != napi_ok) { 12049596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Next: Create int32_t value failed"); 12059596a2c1Sopenharmony_ci return nullptr; 12069596a2c1Sopenharmony_ci } 12079596a2c1Sopenharmony_ci return result; 12089596a2c1Sopenharmony_ci} 12099596a2c1Sopenharmony_ci 12109596a2c1Sopenharmony_cinapi_value I18nAddon::SetText(napi_env env, napi_callback_info info) 12119596a2c1Sopenharmony_ci{ 12129596a2c1Sopenharmony_ci size_t argc = 1; 12139596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 12149596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 12159596a2c1Sopenharmony_ci void *data = nullptr; 12169596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 12179596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 12189596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 12199596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 12209596a2c1Sopenharmony_ci HILOG_ERROR_I18N("SetText: Get BreakIterator object failed"); 12219596a2c1Sopenharmony_ci return nullptr; 12229596a2c1Sopenharmony_ci } 12239596a2c1Sopenharmony_ci if (!argv[0]) { 12249596a2c1Sopenharmony_ci return nullptr; 12259596a2c1Sopenharmony_ci } 12269596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 12279596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 12289596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 12299596a2c1Sopenharmony_ci HILOG_ERROR_I18N("SetText: Parameter type does not match"); 12309596a2c1Sopenharmony_ci return nullptr; 12319596a2c1Sopenharmony_ci } 12329596a2c1Sopenharmony_ci size_t len = 0; 12339596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 12349596a2c1Sopenharmony_ci if (status != napi_ok) { 12359596a2c1Sopenharmony_ci HILOG_ERROR_I18N("SetText: Get field length failed"); 12369596a2c1Sopenharmony_ci return nullptr; 12379596a2c1Sopenharmony_ci } 12389596a2c1Sopenharmony_ci std::vector<char> buf(len + 1); 12399596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len); 12409596a2c1Sopenharmony_ci if (status != napi_ok) { 12419596a2c1Sopenharmony_ci HILOG_ERROR_I18N("SetText: Get string value failed"); 12429596a2c1Sopenharmony_ci return nullptr; 12439596a2c1Sopenharmony_ci } 12449596a2c1Sopenharmony_ci obj->brkiter_->SetText(buf.data()); 12459596a2c1Sopenharmony_ci return nullptr; 12469596a2c1Sopenharmony_ci} 12479596a2c1Sopenharmony_ci 12489596a2c1Sopenharmony_cinapi_value I18nAddon::GetText(napi_env env, napi_callback_info info) 12499596a2c1Sopenharmony_ci{ 12509596a2c1Sopenharmony_ci size_t argc = 0; 12519596a2c1Sopenharmony_ci napi_value *argv = nullptr; 12529596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 12539596a2c1Sopenharmony_ci void *data = nullptr; 12549596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 12559596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 12569596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 12579596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 12589596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetText: Get BreakIterator object failed"); 12599596a2c1Sopenharmony_ci return nullptr; 12609596a2c1Sopenharmony_ci } 12619596a2c1Sopenharmony_ci napi_value value = nullptr; 12629596a2c1Sopenharmony_ci std::string temp; 12639596a2c1Sopenharmony_ci obj->brkiter_->GetText(temp); 12649596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, temp.c_str(), NAPI_AUTO_LENGTH, &value); 12659596a2c1Sopenharmony_ci if (status != napi_ok) { 12669596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetText: Get field length failed"); 12679596a2c1Sopenharmony_ci return nullptr; 12689596a2c1Sopenharmony_ci } 12699596a2c1Sopenharmony_ci return value; 12709596a2c1Sopenharmony_ci} 12719596a2c1Sopenharmony_ci 12729596a2c1Sopenharmony_cinapi_value I18nAddon::Following(napi_env env, napi_callback_info info) 12739596a2c1Sopenharmony_ci{ 12749596a2c1Sopenharmony_ci size_t argc = 1; 12759596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 12769596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 12779596a2c1Sopenharmony_ci void *data = nullptr; 12789596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 12799596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 12809596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 12819596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 12829596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Following: Get BreakIterator object failed"); 12839596a2c1Sopenharmony_ci return nullptr; 12849596a2c1Sopenharmony_ci } 12859596a2c1Sopenharmony_ci if (!argv[0]) { 12869596a2c1Sopenharmony_ci return nullptr; 12879596a2c1Sopenharmony_ci } 12889596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 12899596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 12909596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_number) { 12919596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Following: Parameter type does not match"); 12929596a2c1Sopenharmony_ci return nullptr; 12939596a2c1Sopenharmony_ci } 12949596a2c1Sopenharmony_ci int value; 12959596a2c1Sopenharmony_ci status = napi_get_value_int32(env, argv[0], &value); 12969596a2c1Sopenharmony_ci if (status != napi_ok) { 12979596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Following: Retrieve following value failed"); 12989596a2c1Sopenharmony_ci return nullptr; 12999596a2c1Sopenharmony_ci } 13009596a2c1Sopenharmony_ci value = obj->brkiter_->Following(value); 13019596a2c1Sopenharmony_ci napi_value result = nullptr; 13029596a2c1Sopenharmony_ci status = napi_create_int32(env, value, &result); 13039596a2c1Sopenharmony_ci if (status != napi_ok) { 13049596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Following: Create int32_t value failed"); 13059596a2c1Sopenharmony_ci return nullptr; 13069596a2c1Sopenharmony_ci } 13079596a2c1Sopenharmony_ci return result; 13089596a2c1Sopenharmony_ci} 13099596a2c1Sopenharmony_ci 13109596a2c1Sopenharmony_cinapi_value I18nAddon::IsBoundary(napi_env env, napi_callback_info info) 13119596a2c1Sopenharmony_ci{ 13129596a2c1Sopenharmony_ci size_t argc = 1; 13139596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 13149596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 13159596a2c1Sopenharmony_ci void *data = nullptr; 13169596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 13179596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 13189596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 13199596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->brkiter_) { 13209596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsBoundary: Get BreakIterator object failed"); 13219596a2c1Sopenharmony_ci return nullptr; 13229596a2c1Sopenharmony_ci } 13239596a2c1Sopenharmony_ci if (!argv[0]) { 13249596a2c1Sopenharmony_ci return nullptr; 13259596a2c1Sopenharmony_ci } 13269596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 13279596a2c1Sopenharmony_ci int value; 13289596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 13299596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_number) { 13309596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsBoundary: Parameter type does not match"); 13319596a2c1Sopenharmony_ci return nullptr; 13329596a2c1Sopenharmony_ci } 13339596a2c1Sopenharmony_ci status = napi_get_value_int32(env, argv[0], &value); 13349596a2c1Sopenharmony_ci if (status != napi_ok) { 13359596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsBoundary: Retrieve following value failed"); 13369596a2c1Sopenharmony_ci return nullptr; 13379596a2c1Sopenharmony_ci } 13389596a2c1Sopenharmony_ci bool boundary = obj->brkiter_->IsBoundary(value); 13399596a2c1Sopenharmony_ci napi_value result = nullptr; 13409596a2c1Sopenharmony_ci status = napi_get_boolean(env, boundary, &result); 13419596a2c1Sopenharmony_ci if (status != napi_ok) { 13429596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IsBoundary: Create boolean failed"); 13439596a2c1Sopenharmony_ci return nullptr; 13449596a2c1Sopenharmony_ci } 13459596a2c1Sopenharmony_ci return result; 13469596a2c1Sopenharmony_ci} 13479596a2c1Sopenharmony_ci 13489596a2c1Sopenharmony_cinapi_value I18nAddon::I18nIndexUtilConstructor(napi_env env, napi_callback_info info) 13499596a2c1Sopenharmony_ci{ 13509596a2c1Sopenharmony_ci size_t argc = 1; 13519596a2c1Sopenharmony_ci napi_value argv[1] = { nullptr }; 13529596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 13539596a2c1Sopenharmony_ci void *data = nullptr; 13549596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 13559596a2c1Sopenharmony_ci if (status != napi_ok) { 13569596a2c1Sopenharmony_ci return nullptr; 13579596a2c1Sopenharmony_ci } 13589596a2c1Sopenharmony_ci std::string localeTag = ""; 13599596a2c1Sopenharmony_ci if (argc > 0) { 13609596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 13619596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 13629596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 13639596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IndexUtilConstructor: Parameter type does not match"); 13649596a2c1Sopenharmony_ci return nullptr; 13659596a2c1Sopenharmony_ci } 13669596a2c1Sopenharmony_ci size_t len = 0; 13679596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 13689596a2c1Sopenharmony_ci if (status != napi_ok) { 13699596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IndexUtilConstructor: Get locale length failed"); 13709596a2c1Sopenharmony_ci return nullptr; 13719596a2c1Sopenharmony_ci } 13729596a2c1Sopenharmony_ci std::vector<char> localeBuf(len + 1); 13739596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], localeBuf.data(), len + 1, &len); 13749596a2c1Sopenharmony_ci if (status != napi_ok) { 13759596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IndexUtilConstructor: Get locale failed"); 13769596a2c1Sopenharmony_ci return nullptr; 13779596a2c1Sopenharmony_ci } 13789596a2c1Sopenharmony_ci localeTag = localeBuf.data(); 13799596a2c1Sopenharmony_ci } 13809596a2c1Sopenharmony_ci std::unique_ptr<I18nAddon> obj = nullptr; 13819596a2c1Sopenharmony_ci obj = std::make_unique<I18nAddon>(); 13829596a2c1Sopenharmony_ci status = 13839596a2c1Sopenharmony_ci napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nAddon::Destructor, nullptr, nullptr); 13849596a2c1Sopenharmony_ci if (status != napi_ok) { 13859596a2c1Sopenharmony_ci HILOG_ERROR_I18N("IndexUtilConstructor: Wrap II18nAddon failed"); 13869596a2c1Sopenharmony_ci return nullptr; 13879596a2c1Sopenharmony_ci } 13889596a2c1Sopenharmony_ci if (!obj->InitIndexUtilContext(env, info, localeTag)) { 13899596a2c1Sopenharmony_ci return nullptr; 13909596a2c1Sopenharmony_ci } 13919596a2c1Sopenharmony_ci obj.release(); 13929596a2c1Sopenharmony_ci return thisVar; 13939596a2c1Sopenharmony_ci} 13949596a2c1Sopenharmony_ci 13959596a2c1Sopenharmony_cibool I18nAddon::InitIndexUtilContext(napi_env env, napi_callback_info info, const std::string &localeTag) 13969596a2c1Sopenharmony_ci{ 13979596a2c1Sopenharmony_ci napi_value global = nullptr; 13989596a2c1Sopenharmony_ci napi_status status = napi_get_global(env, &global); 13999596a2c1Sopenharmony_ci if (status != napi_ok) { 14009596a2c1Sopenharmony_ci HILOG_ERROR_I18N("InitIndexUtilContext: Get global failed"); 14019596a2c1Sopenharmony_ci return false; 14029596a2c1Sopenharmony_ci } 14039596a2c1Sopenharmony_ci env_ = env; 14049596a2c1Sopenharmony_ci indexUtil_ = std::make_unique<IndexUtil>(localeTag); 14059596a2c1Sopenharmony_ci return indexUtil_ != nullptr; 14069596a2c1Sopenharmony_ci} 14079596a2c1Sopenharmony_ci 14089596a2c1Sopenharmony_cinapi_value I18nAddon::GetIndexUtil(napi_env env, napi_callback_info info) 14099596a2c1Sopenharmony_ci{ 14109596a2c1Sopenharmony_ci size_t argc = 1; 14119596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 14129596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 14139596a2c1Sopenharmony_ci void *data = nullptr; 14149596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 14159596a2c1Sopenharmony_ci napi_value constructor = nullptr; 14169596a2c1Sopenharmony_ci napi_status status = napi_get_reference_value(env, g_indexUtilConstructor, &constructor); 14179596a2c1Sopenharmony_ci if (status != napi_ok) { 14189596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to create reference at GetIndexUtil"); 14199596a2c1Sopenharmony_ci return nullptr; 14209596a2c1Sopenharmony_ci } 14219596a2c1Sopenharmony_ci napi_value result = nullptr; 14229596a2c1Sopenharmony_ci if (!VariableConvertor::CheckNapiValueType(env, argv[0])) { 14239596a2c1Sopenharmony_ci status = napi_new_instance(env, constructor, 0, argv, &result); 14249596a2c1Sopenharmony_ci } else { 14259596a2c1Sopenharmony_ci status = napi_new_instance(env, constructor, 1, argv, &result); 14269596a2c1Sopenharmony_ci } 14279596a2c1Sopenharmony_ci if (status != napi_ok) { 14289596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Get calendar create instance failed"); 14299596a2c1Sopenharmony_ci return nullptr; 14309596a2c1Sopenharmony_ci } 14319596a2c1Sopenharmony_ci return result; 14329596a2c1Sopenharmony_ci} 14339596a2c1Sopenharmony_ci 14349596a2c1Sopenharmony_cinapi_value I18nAddon::GetIndexList(napi_env env, napi_callback_info info) 14359596a2c1Sopenharmony_ci{ 14369596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 14379596a2c1Sopenharmony_ci void *data = nullptr; 14389596a2c1Sopenharmony_ci napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); 14399596a2c1Sopenharmony_ci 14409596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 14419596a2c1Sopenharmony_ci napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 14429596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->indexUtil_) { 14439596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetIndexList: GetPhoneNumberFormat object failed"); 14449596a2c1Sopenharmony_ci return nullptr; 14459596a2c1Sopenharmony_ci } 14469596a2c1Sopenharmony_ci 14479596a2c1Sopenharmony_ci std::vector<std::string> indexList = obj->indexUtil_->GetIndexList(); 14489596a2c1Sopenharmony_ci napi_value result = nullptr; 14499596a2c1Sopenharmony_ci status = napi_create_array_with_length(env, indexList.size(), &result); 14509596a2c1Sopenharmony_ci if (status != napi_ok) { 14519596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to create array"); 14529596a2c1Sopenharmony_ci return nullptr; 14539596a2c1Sopenharmony_ci } 14549596a2c1Sopenharmony_ci for (size_t i = 0; i < indexList.size(); i++) { 14559596a2c1Sopenharmony_ci napi_value element = nullptr; 14569596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, indexList[i].c_str(), NAPI_AUTO_LENGTH, &element); 14579596a2c1Sopenharmony_ci if (status != napi_ok) { 14589596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetIndexList: Failed to create string item"); 14599596a2c1Sopenharmony_ci return nullptr; 14609596a2c1Sopenharmony_ci } 14619596a2c1Sopenharmony_ci status = napi_set_element(env, result, i, element); 14629596a2c1Sopenharmony_ci if (status != napi_ok) { 14639596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Failed to set array item"); 14649596a2c1Sopenharmony_ci return nullptr; 14659596a2c1Sopenharmony_ci } 14669596a2c1Sopenharmony_ci } 14679596a2c1Sopenharmony_ci return result; 14689596a2c1Sopenharmony_ci} 14699596a2c1Sopenharmony_ci 14709596a2c1Sopenharmony_cinapi_value I18nAddon::AddLocale(napi_env env, napi_callback_info info) 14719596a2c1Sopenharmony_ci{ 14729596a2c1Sopenharmony_ci size_t argc = 1; 14739596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 14749596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 14759596a2c1Sopenharmony_ci void *data = nullptr; 14769596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 14779596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 14789596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 14799596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 14809596a2c1Sopenharmony_ci HILOG_ERROR_I18N("AddLocale: Parameter type does not match"); 14819596a2c1Sopenharmony_ci return nullptr; 14829596a2c1Sopenharmony_ci } 14839596a2c1Sopenharmony_ci size_t len = 0; 14849596a2c1Sopenharmony_ci napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 14859596a2c1Sopenharmony_ci if (status != napi_ok) { 14869596a2c1Sopenharmony_ci HILOG_ERROR_I18N("AddLocale: Get locale length failed"); 14879596a2c1Sopenharmony_ci return nullptr; 14889596a2c1Sopenharmony_ci } 14899596a2c1Sopenharmony_ci std::vector<char> buf(len + 1); 14909596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len); 14919596a2c1Sopenharmony_ci if (status != napi_ok) { 14929596a2c1Sopenharmony_ci HILOG_ERROR_I18N("AddLocale: Get locale failed"); 14939596a2c1Sopenharmony_ci return nullptr; 14949596a2c1Sopenharmony_ci } 14959596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 14969596a2c1Sopenharmony_ci status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 14979596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->indexUtil_) { 14989596a2c1Sopenharmony_ci HILOG_ERROR_I18N("AddLocale: Get IndexUtil object failed"); 14999596a2c1Sopenharmony_ci return nullptr; 15009596a2c1Sopenharmony_ci } 15019596a2c1Sopenharmony_ci obj->indexUtil_->AddLocale(buf.data()); 15029596a2c1Sopenharmony_ci return nullptr; 15039596a2c1Sopenharmony_ci} 15049596a2c1Sopenharmony_ci 15059596a2c1Sopenharmony_cinapi_value I18nAddon::GetIndex(napi_env env, napi_callback_info info) 15069596a2c1Sopenharmony_ci{ 15079596a2c1Sopenharmony_ci size_t argc = 1; 15089596a2c1Sopenharmony_ci napi_value argv[1] = { 0 }; 15099596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 15109596a2c1Sopenharmony_ci void *data = nullptr; 15119596a2c1Sopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 15129596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 15139596a2c1Sopenharmony_ci napi_typeof(env, argv[0], &valueType); 15149596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_string) { 15159596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetIndex: Parameter type does not match"); 15169596a2c1Sopenharmony_ci return nullptr; 15179596a2c1Sopenharmony_ci } 15189596a2c1Sopenharmony_ci size_t len = 0; 15199596a2c1Sopenharmony_ci napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); 15209596a2c1Sopenharmony_ci if (status != napi_ok) { 15219596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetIndex: Get String length failed"); 15229596a2c1Sopenharmony_ci return nullptr; 15239596a2c1Sopenharmony_ci } 15249596a2c1Sopenharmony_ci std::vector<char> buf(len + 1); 15259596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len); 15269596a2c1Sopenharmony_ci if (status != napi_ok) { 15279596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Get String failed"); 15289596a2c1Sopenharmony_ci return nullptr; 15299596a2c1Sopenharmony_ci } 15309596a2c1Sopenharmony_ci I18nAddon *obj = nullptr; 15319596a2c1Sopenharmony_ci status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj)); 15329596a2c1Sopenharmony_ci if (status != napi_ok || !obj || !obj->indexUtil_) { 15339596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetIndex: Get IndexUtil object failed"); 15349596a2c1Sopenharmony_ci return nullptr; 15359596a2c1Sopenharmony_ci } 15369596a2c1Sopenharmony_ci std::string index = obj->indexUtil_->GetIndex(buf.data()); 15379596a2c1Sopenharmony_ci napi_value result = nullptr; 15389596a2c1Sopenharmony_ci status = napi_create_string_utf8(env, index.c_str(), NAPI_AUTO_LENGTH, &result); 15399596a2c1Sopenharmony_ci if (status != napi_ok) { 15409596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetIndex Failed"); 15419596a2c1Sopenharmony_ci return nullptr; 15429596a2c1Sopenharmony_ci } 15439596a2c1Sopenharmony_ci return result; 15449596a2c1Sopenharmony_ci} 15459596a2c1Sopenharmony_ci 15469596a2c1Sopenharmony_cinapi_value I18nAddon::ObjectConstructor(napi_env env, napi_callback_info info) 15479596a2c1Sopenharmony_ci{ 15489596a2c1Sopenharmony_ci napi_value thisVar = nullptr; 15499596a2c1Sopenharmony_ci void *data = nullptr; 15509596a2c1Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); 15519596a2c1Sopenharmony_ci if (status != napi_ok) { 15529596a2c1Sopenharmony_ci return nullptr; 15539596a2c1Sopenharmony_ci } 15549596a2c1Sopenharmony_ci std::unique_ptr<I18nAddon> obj = nullptr; 15559596a2c1Sopenharmony_ci obj = std::make_unique<I18nAddon>(); 15569596a2c1Sopenharmony_ci status = 15579596a2c1Sopenharmony_ci napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nAddon::Destructor, nullptr, nullptr); 15589596a2c1Sopenharmony_ci if (status != napi_ok) { 15599596a2c1Sopenharmony_ci HILOG_ERROR_I18N("ObjectConstructor: Wrap I18nAddon failed"); 15609596a2c1Sopenharmony_ci return nullptr; 15619596a2c1Sopenharmony_ci } 15629596a2c1Sopenharmony_ci obj.release(); 15639596a2c1Sopenharmony_ci return thisVar; 15649596a2c1Sopenharmony_ci} 15659596a2c1Sopenharmony_ci 15669596a2c1Sopenharmony_cinapi_value I18nAddon::InitUtil(napi_env env, napi_value exports) 15679596a2c1Sopenharmony_ci{ 15689596a2c1Sopenharmony_ci napi_status status = napi_ok; 15699596a2c1Sopenharmony_ci napi_property_descriptor properties[] = { 15709596a2c1Sopenharmony_ci DECLARE_NAPI_FUNCTION("unitConvert", UnitConvert) 15719596a2c1Sopenharmony_ci }; 15729596a2c1Sopenharmony_ci 15739596a2c1Sopenharmony_ci napi_value constructor = nullptr; 15749596a2c1Sopenharmony_ci status = napi_define_class(env, "Util", NAPI_AUTO_LENGTH, ObjectConstructor, nullptr, 15759596a2c1Sopenharmony_ci sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor); 15769596a2c1Sopenharmony_ci if (status != napi_ok) { 15779596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Define class failed when InitUtil"); 15789596a2c1Sopenharmony_ci return nullptr; 15799596a2c1Sopenharmony_ci } 15809596a2c1Sopenharmony_ci 15819596a2c1Sopenharmony_ci status = napi_set_named_property(env, exports, "Util", constructor); 15829596a2c1Sopenharmony_ci if (status != napi_ok) { 15839596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Set property failed when InitUtil"); 15849596a2c1Sopenharmony_ci return nullptr; 15859596a2c1Sopenharmony_ci } 15869596a2c1Sopenharmony_ci return exports; 15879596a2c1Sopenharmony_ci} 15889596a2c1Sopenharmony_ci 15899596a2c1Sopenharmony_cinapi_value Init(napi_env env, napi_value exports) 15909596a2c1Sopenharmony_ci{ 15919596a2c1Sopenharmony_ci napi_value val = I18nAddon::Init(env, exports); 15929596a2c1Sopenharmony_ci val = I18nAddon::InitPhoneNumberFormat(env, val); 15939596a2c1Sopenharmony_ci val = I18nAddon::InitI18nBreakIterator(env, val); 15949596a2c1Sopenharmony_ci val = I18nCalendarAddon::InitI18nCalendar(env, val); 15959596a2c1Sopenharmony_ci val = I18nAddon::InitI18nIndexUtil(env, val); 15969596a2c1Sopenharmony_ci val = I18nAddon::InitI18nUtil(env, val); 15979596a2c1Sopenharmony_ci val = I18nTimeZoneAddon::InitI18nTimeZone(env, val); 15989596a2c1Sopenharmony_ci val = I18nAddon::InitI18nTransliterator(env, val); 15999596a2c1Sopenharmony_ci val = I18nUnicodeAddon::InitCharacter(env, val); 16009596a2c1Sopenharmony_ci val = I18nUnicodeAddon::InitI18nUnicode(env, val); 16019596a2c1Sopenharmony_ci val = I18nAddon::InitUtil(env, val); 16029596a2c1Sopenharmony_ci val = I18nNormalizerAddon::InitI18nNormalizer(env, val); 16039596a2c1Sopenharmony_ci val = SystemLocaleManagerAddon::InitSystemLocaleManager(env, val); 16049596a2c1Sopenharmony_ci val = I18nSystemAddon::InitI18nSystem(env, val); 16059596a2c1Sopenharmony_ci val = HolidayManagerAddon::InitHolidayManager(env, val); 16069596a2c1Sopenharmony_ci val = EntityRecognizerAddon::InitEntityRecognizer(env, val); 16079596a2c1Sopenharmony_ci return val; 16089596a2c1Sopenharmony_ci} 16099596a2c1Sopenharmony_ci 16109596a2c1Sopenharmony_cistatic napi_module g_i18nModule = { 16119596a2c1Sopenharmony_ci .nm_version = 1, 16129596a2c1Sopenharmony_ci .nm_flags = 0, 16139596a2c1Sopenharmony_ci .nm_filename = nullptr, 16149596a2c1Sopenharmony_ci .nm_register_func = Init, 16159596a2c1Sopenharmony_ci .nm_modname = "i18n", 16169596a2c1Sopenharmony_ci .nm_priv = nullptr, 16179596a2c1Sopenharmony_ci .reserved = { 0 } 16189596a2c1Sopenharmony_ci}; 16199596a2c1Sopenharmony_ci 16209596a2c1Sopenharmony_ciextern "C" __attribute__((constructor)) void I18nRegister() 16219596a2c1Sopenharmony_ci{ 16229596a2c1Sopenharmony_ci napi_module_register(&g_i18nModule); 16239596a2c1Sopenharmony_ci} 16249596a2c1Sopenharmony_ci} // namespace I18n 16259596a2c1Sopenharmony_ci} // namespace Global 16269596a2c1Sopenharmony_ci} // namespace OHOS 1627