• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GetLocaleTags(napi_env env, napi_value rawLocaleTag, std::vector<std::string> &localeTags);
34 void GetOptionValue(napi_env env, napi_value options, const std::string &optionName,
35     std::map<std::string, std::string> &map);
36 void GetBoolOptionValue(napi_env env, napi_value options, const std::string &optionName,
37     std::map<std::string, std::string> &map);
38 void GetIntegerOptionValue(napi_env env, napi_value options, const std::string &optionName,
39     std::map<std::string, std::string> &map);
40 void GetDateOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
41 void GetNumberOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
42 void GetCollatorOptionValue(napi_env env, napi_value options, std::map<std::string, std::string> &map);
43 void GetPluralRulesOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
44 void SetOptionProperties(napi_env env, napi_value &result, std::map<std::string, std::string> &options,
45     const std::string &option);
46 void SetIntegerOptionProperties(napi_env env, napi_value &result,
47     std::map<std::string, std::string> &options, const std::string &option);
48 void SetBooleanOptionProperties(napi_env env, napi_value &result,
49     std::map<std::string, std::string> &options, const std::string &option);
50 void GetRelativeTimeOptionValues(napi_env env, napi_value options, std::map<std::string, std::string> &map);
51 
52 class IntlAddon {
53 public:
54     static napi_value InitLocale(napi_env env, napi_value exports);
55     static napi_value InitDateTimeFormat(napi_env env, napi_value exports);
56     static napi_value InitNumberFormat(napi_env env, napi_value exports);
57     static napi_value InitCollator(napi_env env, napi_value exports);
58     static napi_value InitRelativeTimeFormat(napi_env env, napi_value exports);
59     static napi_value InitPluralRules(napi_env env, napi_value exports);
60     static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
61 
62     IntlAddon();
63     virtual ~IntlAddon();
64 
65 private:
66     static napi_value DateTimeFormatConstructor(napi_env env, napi_callback_info info);
67     static napi_value NumberFormatConstructor(napi_env env, napi_callback_info info);
68     static napi_value LocaleConstructor(napi_env env, napi_callback_info info);
69     static napi_value RelativeTimeFormatConstructor(napi_env env, napi_callback_info info);
70     static napi_value GetLanguage(napi_env env, napi_callback_info info);
71     static napi_value GetScript(napi_env env, napi_callback_info info);
72     static napi_value GetRegion(napi_env env, napi_callback_info info);
73     static napi_value GetBaseName(napi_env env, napi_callback_info info);
74     static napi_value GetCalendar(napi_env env, napi_callback_info info);
75     static napi_value GetCollation(napi_env env, napi_callback_info info);
76     static napi_value GetHourCycle(napi_env env, napi_callback_info info);
77     static napi_value GetNumberingSystem(napi_env env, napi_callback_info info);
78     static napi_value GetNumeric(napi_env env, napi_callback_info info);
79     static napi_value GetCaseFirst(napi_env env, napi_callback_info info);
80     static napi_value ToString(napi_env env, napi_callback_info info);
81     static napi_value Maximize(napi_env env, napi_callback_info info);
82     static napi_value Minimize(napi_env env, napi_callback_info info);
83 
84     static napi_value FormatDateTime(napi_env env, napi_callback_info info);
85     static napi_value FormatDateTimeRange(napi_env env, napi_callback_info info);
86     static napi_value GetDateTimeResolvedOptions(napi_env env, napi_callback_info info);
87 
88     static napi_value FormatRelativeTime(napi_env env, napi_callback_info info);
89     static napi_value FormatToParts(napi_env env, napi_callback_info info);
90     static void FillInArrayElement(napi_env env, napi_value &result, napi_status &status,
91         const std::vector<std::vector<std::string>> &timeVector);
92     static napi_value GetRelativeTimeResolvedOptions(napi_env env, napi_callback_info info);
93 
94     static napi_value GetNumberResolvedOptions(napi_env env, napi_callback_info info);
95     static napi_value FormatNumber(napi_env env, napi_callback_info info);
96 
97     static napi_value CollatorConstructor(napi_env env, napi_callback_info info);
98     static napi_value CompareString(napi_env env, napi_callback_info info);
99     static napi_value GetCollatorResolvedOptions(napi_env env, napi_callback_info info);
100 
101     static napi_value PluralRulesConstructor(napi_env env, napi_callback_info info);
102     static napi_value Select(napi_env env, napi_callback_info info);
103 
104     static int64_t GetMilliseconds(napi_env env, napi_value *argv, int index);
105     bool InitLocaleContext(napi_env env, napi_callback_info info, const std::string localeTag,
106         std::map<std::string, std::string> &map);
107     bool InitDateTimeFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
108         std::map<std::string, std::string> &map);
109     bool InitNumberFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
110         std::map<std::string, std::string> &map);
111     bool InitCollatorContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
112         std::map<std::string, std::string> &map);
113     bool InitRelativeTimeFormatContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
114         std::map<std::string, std::string> &map);
115     bool InitPluralRulesContext(napi_env env, napi_callback_info info, std::vector<std::string> localeTags,
116         std::map<std::string, std::string> &map);
117 
118     napi_env env_;
119     std::unique_ptr<LocaleInfo> locale_ = nullptr;
120     std::unique_ptr<DateTimeFormat> datefmt_ = nullptr;
121     std::unique_ptr<NumberFormat> numberfmt_ = nullptr;
122     std::unique_ptr<RelativeTimeFormat> relativetimefmt_ = nullptr;
123     std::unique_ptr<Collator> collator_ = nullptr;
124     std::unique_ptr<PluralRules> pluralrules_ = nullptr;
125 };
126 } // namespace I18n
127 } // namespace Global
128 } // namespace OHOS
129 #endif