1 /* 2 * Copyright (c) 2025 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 OHOS_GLOBAL_I18N_INTL_LOCALE_ADDON_H 16 #define OHOS_GLOBAL_I18N_INTL_LOCALE_ADDON_H 17 18 #include <unordered_map> 19 #include "intl_locale.h" 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 23 namespace OHOS { 24 namespace Global { 25 namespace I18n { 26 typedef std::string (*GetIntlLocaleProperty)(std::shared_ptr<IntlLocale>); 27 28 class IntlLocaleAddon { 29 public: 30 IntlLocaleAddon(); 31 virtual ~IntlLocaleAddon(); 32 33 std::shared_ptr<IntlLocale> GetIntlLocale(); 34 static napi_value InitIntlLocale(napi_env env, napi_value exports); 35 static bool IsIntlLocale(napi_env env, napi_value argv); 36 static napi_value CreateIntlLocaleObject(napi_env env, napi_value argv); 37 38 private: 39 static void Destructor(napi_env env, void *nativeObject, void *hint); 40 static napi_value SetProperty(napi_env env, napi_callback_info info); 41 static napi_value IntlLocaleConstructor(napi_env env, napi_callback_info info); 42 static std::string LocaleToString(napi_env env, napi_value argv); 43 bool InitLocaleContext(napi_env env, const std::string localeTag, 44 std::unordered_map<std::string, std::string>& map); 45 46 static napi_value GetLanguage(napi_env env, napi_callback_info info); 47 static napi_value GetBaseName(napi_env env, napi_callback_info info); 48 static napi_value GetRegion(napi_env env, napi_callback_info info); 49 static napi_value GetScript(napi_env env, napi_callback_info info); 50 static napi_value GetCalendar(napi_env env, napi_callback_info info); 51 static napi_value GetCollation(napi_env env, napi_callback_info info); 52 static napi_value GetHourCycle(napi_env env, napi_callback_info info); 53 static napi_value GetNumberingSystem(napi_env env, napi_callback_info info); 54 static napi_value GetNumeric(napi_env env, napi_callback_info info); 55 static napi_value GetCaseFirst(napi_env env, napi_callback_info info); 56 static napi_value ToString(napi_env env, napi_callback_info info); 57 static napi_value Minimize(napi_env env, napi_callback_info info); 58 static napi_value Maximize(napi_env env, napi_callback_info info); 59 60 static std::string GetLanguageInner(std::shared_ptr<IntlLocale> intlLocale); 61 static std::string GetBaseNameInner(std::shared_ptr<IntlLocale> intlLocale); 62 static std::string GetRegionInner(std::shared_ptr<IntlLocale> intlLocale); 63 static std::string GetScriptInner(std::shared_ptr<IntlLocale> intlLocale); 64 static std::string GetCalendarInner(std::shared_ptr<IntlLocale> intlLocale); 65 static std::string GetCollationInner(std::shared_ptr<IntlLocale> intlLocale); 66 static std::string GetHourCycleInner(std::shared_ptr<IntlLocale> intlLocale); 67 static std::string GetNumberingSystemInner(std::shared_ptr<IntlLocale> intlLocale); 68 static std::string GetNumericInner(std::shared_ptr<IntlLocale> intlLocale); 69 static std::string GetCaseFirstInner(std::shared_ptr<IntlLocale> intlLocale); 70 static std::string MaximizeInner(std::shared_ptr<IntlLocale> intlLocale); 71 static std::string MinimizeInner(std::shared_ptr<IntlLocale> intlLocale); 72 static napi_value GetIntlLocaleAttribute(napi_env env, napi_callback_info info, GetIntlLocaleProperty func, 73 napi_valuetype type); 74 static std::string ParseLocaleTag(napi_env env, napi_value value, napi_valuetype type); 75 static void ParseConfigs(napi_env env, napi_value options, std::unordered_map<std::string, std::string>& configs); 76 std::shared_ptr<IntlLocale> intlLocale = nullptr; 77 }; 78 } // namespace I18n 79 } // namespace Global 80 } // namespace OHOS 81 #endif