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