1 /* 2 * Copyright (c) 2021 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 16 #ifndef INTL_ADDON_H 17 #define INTL_ADDON_H 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "locale_info.h" 24 #include "date_time_format.h" 25 #include "number_format.h" 26 #include "relative_time_format.h" 27 #include "collator.h" 28 #include "plural_rules.h" 29 30 namespace OHOS { 31 namespace Global { 32 namespace I18n { 33 void GetDateOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map); 34 void GetCollatorOptionValue(napi_env env, napi_value options, std::map<std::string, std::string> &map); 35 void GetPluralRulesOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map); 36 void GetRelativeTimeOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map); 37 38 class IntlAddon { 39 public: 40 static napi_value InitDateTimeFormat(napi_env env, napi_value exports); 41 static napi_value InitCollator(napi_env env, napi_value exports); 42 static napi_value InitRelativeTimeFormat(napi_env env, napi_value exports); 43 static napi_value InitPluralRules(napi_env env, napi_value exports); 44 static void Destructor(napi_env env, void *nativeObject, void *finalize_hint); 45 46 IntlAddon(); 47 virtual ~IntlAddon(); 48 49 private: 50 static napi_value DateTimeFormatConstructor(napi_env env, napi_callback_info info); 51 static napi_value RelativeTimeFormatConstructor(napi_env env, napi_callback_info info); 52 53 static napi_value FormatDateTime(napi_env env, napi_callback_info info); 54 static napi_value FormatDateTimeRange(napi_env env, napi_callback_info info); 55 static napi_value GetDateTimeResolvedOptions(napi_env env, napi_callback_info info); 56 57 static napi_value FormatRelativeTime(napi_env env, napi_callback_info info); 58 static napi_value FormatToParts(napi_env env, napi_callback_info info); 59 static void FillInArrayElement(napi_env env, napi_value &result, napi_status &status, 60 const std::vector<std::vector<std::string>> &timeVector); 61 static napi_value GetRelativeTimeResolvedOptions(napi_env env, napi_callback_info info); 62 63 static napi_value CollatorConstructor(napi_env env, napi_callback_info info); 64 static napi_value CompareString(napi_env env, napi_callback_info info); 65 static napi_value GetCollatorResolvedOptions(napi_env env, napi_callback_info info); 66 67 static napi_value PluralRulesConstructor(napi_env env, napi_callback_info info); 68 static napi_value Select(napi_env env, napi_callback_info info); 69 70 static int64_t GetMilliseconds(napi_env env, napi_value *argv, int index); 71 bool InitDateTimeFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags, 72 std::map<std::string, std::string> &map); 73 bool InitCollatorContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags, 74 std::map<std::string, std::string> &map); 75 bool InitRelativeTimeFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags, 76 std::map<std::string, std::string> &map); 77 bool InitPluralRulesContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags, 78 std::map<std::string, std::string> &map); 79 80 napi_env env_; 81 std::unique_ptr<DateTimeFormat> datefmt_ = nullptr; 82 std::unique_ptr<RelativeTimeFormat> relativetimefmt_ = nullptr; 83 std::unique_ptr<Collator> collator_ = nullptr; 84 std::unique_ptr<PluralRules> pluralrules_ = nullptr; 85 }; 86 } // namespace I18n 87 } // namespace Global 88 } // namespace OHOS 89 #endif