1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef I18N_ADDON_H 16 #define I18N_ADDON_H 17 18 #include <string> 19 #include <unordered_map> 20 #include "napi/native_api.h" 21 #include "i18n_break_iterator.h" 22 #include "i18n_calendar.h" 23 #include "i18n_timezone.h" 24 #include "index_util.h" 25 #include "napi/native_node_api.h" 26 #include "locale_config.h" 27 #include "measure_data.h" 28 #include "number_format.h" 29 #include "phone_number_format.h" 30 #include "preferred_language.h" 31 32 namespace OHOS { 33 namespace Global { 34 namespace I18n { 35 void GetOptionMap(napi_env env, napi_value argv, std::map<std::string, std::string> &map); 36 void GetOptionValue(napi_env env, napi_value options, const std::string &optionName, 37 std::string &value); 38 39 class I18nAddon { 40 public: 41 static napi_value Init(napi_env env, napi_value exports); 42 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 43 I18nAddon(); 44 virtual ~I18nAddon(); 45 static napi_value GetSystemLanguages(napi_env env, napi_callback_info info); 46 static napi_value GetSystemCountries(napi_env env, napi_callback_info info); 47 static napi_value GetSystemCountriesWithError(napi_env env, napi_callback_info info); 48 static napi_value IsSuggested(napi_env env, napi_callback_info info); 49 static napi_value IsSuggestedWithError(napi_env env, napi_callback_info info); 50 static napi_value GetDisplayLanguage(napi_env env, napi_callback_info info); 51 static napi_value GetDisplayLanguageWithError(napi_env env, napi_callback_info info); 52 static napi_value GetDisplayCountry(napi_env env, napi_callback_info info); 53 static napi_value GetDisplayCountryWithError(napi_env env, napi_callback_info info); 54 static napi_value GetSystemLanguage(napi_env env, napi_callback_info info); 55 static napi_value GetSystemRegion(napi_env env, napi_callback_info info); 56 static napi_value GetSystemLocale(napi_env env, napi_callback_info info); 57 static napi_value SetSystemLanguage(napi_env env, napi_callback_info info); 58 static napi_value SetSystemLanguageWithError(napi_env env, napi_callback_info info); 59 static napi_value SetSystemRegion(napi_env env, napi_callback_info info); 60 static napi_value SetSystemRegionWithError(napi_env env, napi_callback_info info); 61 static napi_value SetSystemLocale(napi_env env, napi_callback_info info); 62 static napi_value SetSystemLocaleWithError(napi_env env, napi_callback_info info); 63 static napi_value IsRTL(napi_env env, napi_callback_info info); 64 static napi_value IsRTLWihtError(napi_env env, napi_callback_info info); 65 static napi_value InitPhoneNumberFormat(napi_env env, napi_value exports); 66 static napi_value InitI18nCalendar(napi_env env, napi_value exports); 67 static napi_value UnitConvert(napi_env env, napi_callback_info info); 68 static napi_value InitBreakIterator(napi_env env, napi_value exports); 69 static napi_value InitIndexUtil(napi_env env, napi_value exports); 70 static napi_value IsDigitAddon(napi_env env, napi_callback_info info); 71 static napi_value IsSpaceCharAddon(napi_env env, napi_callback_info info); 72 static napi_value IsWhiteSpaceAddon(napi_env env, napi_callback_info info); 73 static napi_value IsRTLCharacterAddon(napi_env env, napi_callback_info info); 74 static napi_value IsIdeoGraphicAddon(napi_env env, napi_callback_info info); 75 static napi_value IsLetterAddon(napi_env env, napi_callback_info info); 76 static napi_value IsLowerCaseAddon(napi_env env, napi_callback_info info); 77 static napi_value IsUpperCaseAddon(napi_env env, napi_callback_info info); 78 static napi_value GetTypeAddon(napi_env env, napi_callback_info info); 79 static napi_value Is24HourClock(napi_env env, napi_callback_info info); 80 static napi_value Set24HourClock(napi_env env, napi_callback_info info); 81 static napi_value Set24HourClockWithError(napi_env env, napi_callback_info info); 82 static napi_value AddPreferredLanguage(napi_env env, napi_callback_info info); 83 static napi_value AddPreferredLanguageWithError(napi_env env, napi_callback_info info); 84 static napi_value RemovePreferredLanguage(napi_env env, napi_callback_info info); 85 static napi_value RemovePreferredLanguageWithError(napi_env env, napi_callback_info info); 86 static napi_value GetPreferredLanguageList(napi_env env, napi_callback_info info); 87 static napi_value GetFirstPreferredLanguage(napi_env env, napi_callback_info info); 88 static napi_value InitI18nTimeZone(napi_env env, napi_value exports); 89 static napi_value GetI18nTimeZone(napi_env env, napi_callback_info info); 90 static napi_value GetDateOrder(napi_env env, napi_callback_info info); 91 static napi_value InitTransliterator(napi_env env, napi_value exports); 92 static napi_value GetTransliteratorInstance(napi_env env, napi_callback_info info); 93 static napi_value GetAvailableIDs(napi_env env, napi_callback_info info); 94 static napi_value SetUsingLocalDigitAddon(napi_env env, napi_callback_info info); 95 static napi_value SetUsingLocalDigitAddonWithError(napi_env env, napi_callback_info info); 96 static napi_value GetUsingLocalDigitAddon(napi_env env, napi_callback_info info); 97 static napi_value GetAvailableTimezoneIDs(napi_env env, napi_callback_info info); 98 static napi_value GetAvailableZoneCityIDs(napi_env env, napi_callback_info info); 99 static napi_value GetCityDisplayName(napi_env env, napi_callback_info info); 100 static napi_value GetTimezoneFromCity(napi_env env, napi_callback_info info); 101 static napi_value InitCharacter(napi_env env, napi_value exports); 102 static napi_value InitUtil(napi_env env, napi_value exports); 103 static napi_value System(napi_env env, napi_value exports); 104 105 private: 106 static void CreateInitProperties(napi_property_descriptor *properties); 107 static napi_value PhoneNumberFormatConstructor(napi_env env, napi_callback_info info); 108 static napi_value IsValidPhoneNumber(napi_env env, napi_callback_info info); 109 static napi_value FormatPhoneNumber(napi_env env, napi_callback_info info); 110 static napi_value GetLocationName(napi_env env, napi_callback_info info); 111 bool InitPhoneNumberFormatContext(napi_env env, napi_callback_info info, const std::string &country, 112 const std::map<std::string, std::string> &options); 113 114 static napi_value CalendarConstructor(napi_env env, napi_callback_info info); 115 static napi_value BreakIteratorConstructor(napi_env env, napi_callback_info info); 116 bool InitCalendarContext(napi_env env, napi_callback_info info, const std::string &localeTag, CalendarType type); 117 static napi_value GetCalendar(napi_env env, napi_callback_info info); 118 static napi_value Set(napi_env env, napi_callback_info info); 119 static napi_value GetDate(napi_env env, napi_value value); 120 void SetMilliseconds(napi_env env, napi_value value); 121 void SetField(napi_env env, napi_value value, UCalendarDateFields field); 122 static napi_value SetTime(napi_env env, napi_callback_info info); 123 static std::string GetString(napi_env &env, napi_value &value, int32_t &code); 124 static napi_value SetTimeZone(napi_env env, napi_callback_info info); 125 static napi_value GetTimeZone(napi_env env, napi_callback_info info); 126 static napi_value SetFirstDayOfWeek(napi_env env, napi_callback_info info); 127 static napi_value GetFirstDayOfWeek(napi_env env, napi_callback_info info); 128 static napi_value SetMinimalDaysInFirstWeek(napi_env env, napi_callback_info info); 129 static napi_value GetMinimalDaysInFirstWeek(napi_env env, napi_callback_info info); 130 static napi_value Get(napi_env env, napi_callback_info info); 131 static napi_value GetDisplayName(napi_env env, napi_callback_info info); 132 static napi_value IsWeekend(napi_env env, napi_callback_info info); 133 static CalendarType GetCalendarType(napi_env env, napi_value value); 134 static napi_value GetLineInstance(napi_env env, napi_callback_info info); 135 static napi_value Current(napi_env env, napi_callback_info info); 136 static napi_value First(napi_env env, napi_callback_info info); 137 static napi_value Last(napi_env env, napi_callback_info info); 138 static napi_value Previous(napi_env env, napi_callback_info info); 139 static napi_value Next(napi_env env, napi_callback_info info); 140 static napi_value SetText(napi_env env, napi_callback_info info); 141 static napi_value GetText(napi_env env, napi_callback_info info); 142 static napi_value Following(napi_env env, napi_callback_info info); 143 static napi_value IsBoundary(napi_env env, napi_callback_info info); 144 145 static napi_value GetIndexUtil(napi_env env, napi_callback_info info); 146 static napi_value IndexUtilConstructor(napi_env env, napi_callback_info info); 147 static napi_value GetIndexList(napi_env env, napi_callback_info info); 148 static napi_value AddLocale(napi_env env, napi_callback_info info); 149 static napi_value GetIndex(napi_env env, napi_callback_info info); 150 bool InitIndexUtilContext(napi_env env, napi_callback_info info, const std::string &localeTag); 151 static napi_value CreateUnicodeObject(napi_env env); 152 153 static napi_value I18nTimeZoneConstructor(napi_env env, napi_callback_info info); 154 static napi_value GetID(napi_env env, napi_callback_info info); 155 static int32_t GetParameter(napi_env env, napi_value *argv, std::string &localeStr, bool &isDST); 156 static napi_value GetTimeZoneDisplayName(napi_env env, napi_callback_info info); 157 static napi_value GetOffset(napi_env env, napi_callback_info info); 158 static napi_value GetRawOffset(napi_env env, napi_callback_info info); 159 160 static napi_value Transform(napi_env env, napi_callback_info info); 161 static napi_value CreateTransliteratorObject(napi_env env); 162 bool InitTransliteratorContext(napi_env env, napi_callback_info info, const std::string &idTag); 163 static napi_value TransliteratorConstructor(napi_env env, napi_callback_info info); 164 static std::string ModifyOrder(std::string &pattern); 165 static void ProcessNormal(char ch, int *order, size_t orderSize, int *lengths, size_t lengsSize); 166 static bool GetStringFromJS(napi_env env, napi_value argv, std::string &jsString); 167 168 static napi_value StaticGetTimeZone(napi_env, napi_value *argv, bool isZoneID); 169 static napi_value CreateTimeZoneObject(napi_env env); 170 171 static napi_value CreateSystemObject(napi_env env); 172 static napi_value GetSystemCountriesImpl(napi_env env, napi_callback_info info, bool throwError); 173 static napi_value IsSuggestedImpl(napi_env env, napi_callback_info info, bool throwError); 174 static napi_value GetDisplayLanguageImpl(napi_env env, napi_callback_info info, bool throwError); 175 static napi_value GetDisplayCountryImpl(napi_env env, napi_callback_info info, bool throwError); 176 static napi_value SetSystemLanguageImpl(napi_env env, napi_callback_info info, bool throwError); 177 static napi_value SetSystemRegionImpl(napi_env env, napi_callback_info info, bool throwError); 178 static napi_value SetSystemLocaleImpl(napi_env env, napi_callback_info info, bool throwError); 179 static napi_value Set24HourClockImpl(napi_env env, napi_callback_info info, bool throwError); 180 static napi_value AddPreferredLanguageImpl(napi_env env, napi_callback_info info, bool throwError); 181 static napi_value RemovePreferredLanguageImpl(napi_env env, napi_callback_info info, bool throwError); 182 static napi_value SetUsingLocalDigitAddonImpl(napi_env env, napi_callback_info info, bool throwError); 183 184 static napi_value ObjectConstructor(napi_env env, napi_callback_info info); 185 186 static bool ParseStringParam(napi_env env, napi_value argv, bool throwError, std::string &strParam); 187 188 napi_env env_; 189 std::unique_ptr<PhoneNumberFormat> phonenumberfmt_ = nullptr; 190 std::unique_ptr<I18nCalendar> calendar_ = nullptr; 191 std::unique_ptr<icu::Transliterator> transliterator_ = nullptr; 192 std::unique_ptr<I18nBreakIterator> brkiter_ = nullptr; 193 std::unique_ptr<IndexUtil> indexUtil_ = nullptr; 194 std::unique_ptr<I18nTimeZone> timezone_ = nullptr; 195 }; 196 } // namespace I18n 197 } // namespace Global 198 } // namespace OHOS 199 #endif 200