• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef OHOS_GLOBAL_INTL_PLURAL_RULES_H
17 #define OHOS_GLOBAL_INTL_PLURAL_RULES_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <set>
23 #include <string>
24 #include <vector>
25 #include "unicode/locid.h"
26 #include "unicode/numberformatter.h"
27 #include "unicode/plurrule.h"
28 #include "i18n_types.h"
29 
30 namespace OHOS {
31 namespace Global {
32 namespace I18n {
33 enum class RoundingType : uint8_t { FRACTIONDIGITS = 0x01, SIGNIFICANTDIGITS, COMPACTROUNDING, EXCEPTION };
34 
35 class IntlPluralRules {
36 public:
37     struct ResolvedValue {
38         RoundingType roundingType = RoundingType::EXCEPTION;
39         std::string locale = "";
40         std::string type = "";
41         int32_t minimumIntegerDigits = 0;
42         int32_t minimumDigits = 0;
43         int32_t maximumDigits = 0;
44         std::vector<std::string> pluralCategories {};
45     };
46 
47     static std::vector<std::string> SupportedLocalesOf(const std::vector<std::string>& requestLocales,
48         const std::unordered_map<std::string, std::string>& options, ErrorMessage& errorMessage);
49 
50     IntlPluralRules(const std::vector<std::string>& localeTags,
51         const std::unordered_map<std::string, std::string>& options, ErrorMessage& errorMessage);
52     std::string Select(double number, ErrorMessage& errorMessage) const;
53     ResolvedValue ResolvedOptions() const;
54 
55 private:
56     static std::set<std::string> GetAvailableLocales();
57     static bool GetNextLocale(icu::StringEnumeration* locales, std::string& validLocale, int32_t* len);
58     static int32_t GetDefaultNumberOption(const std::unordered_map<std::string, std::string>& options,
59         const std::string& key, int32_t minimum, int32_t maximum, int32_t fallback, ErrorMessage& errorMessage);
60     static bool IsValidOptionName(const std::unordered_set<std::string>& optionValues, const std::string& value);
61     static std::string ParseLocaleMatcher(const std::unordered_map<std::string, std::string>& options,
62         ErrorMessage& errorMessage);
63     static std::string ParseType(const std::unordered_map<std::string, std::string>& options,
64         ErrorMessage& errorMessage);
65 
66     bool ParseOptions(const std::unordered_map<std::string, std::string>& options, ErrorMessage& errorMessage);
67     bool ResolveLocale(const std::set<std::string>& availableLocales,
68         const std::vector<std::string>& requestedLocales, ErrorMessage& errorMessage);
69     bool InitIntlPluralRules(const std::unordered_map<std::string, std::string>& options, ErrorMessage& errorMessage);
70     bool SetNumberFormatDigitOptions(const std::unordered_map<std::string, std::string>& options,
71         int32_t mnfdDefault, int32_t mxfdDefault, ErrorMessage& errorMessage);
72     bool SetSignificantDigitsOptions(const std::unordered_map<std::string, std::string>& options,
73         ErrorMessage& errorMessage);
74     bool SetFractionDigitsOptions(const std::unordered_map<std::string, std::string>& options, int32_t mnfdDefault,
75         int32_t mxfdDefault, ErrorMessage& errorMessage);
76     bool GetPluralCategories(ResolvedValue& options) const;
77 
78     static const int32_t STRING_SEPARATOR_LENGTH;
79     static const int32_t MAX_DIGITS_VALUE;
80     static const int32_t MAX_FRACTION_DIGITS_VALUE;
81     static const int32_t MNFD_DEFAULT;
82     static const int32_t MXFD_DEFAULT;
83     static bool initAvailableLocales;
84     static std::mutex getLocalesMutex;
85     static std::set<std::string> availableLocales;
86     static const std::string LOCALE_MATCHER_TAG;
87     static const std::string TYPE_TAG;
88     static const std::string MIN_INTEGER_DIGITS;
89     static const std::string MIN_SIGNALE_DIGITS;
90     static const std::string MAX_SIGNALE_DIGITS;
91     static const std::string MIN_FRACTION_DIGITS;
92     static const std::string MAX_FRACTION_DIGITS;
93     static const std::unordered_set<std::string> LOCALE_MATCHER_VALUE;
94     static const std::unordered_set<std::string> TYPE_VALUE;
95     static const std::unordered_map<std::string, UPluralType> TYPE_TO_ICU_TYPE;
96 
97     bool initSuccess = false;
98     std::string localeMatcher;
99     std::string type;
100     int32_t minimumIntegerDigits = -1;
101     int32_t minimumFractionDigits = -1;
102     int32_t maximumFractionDigits = -1;
103     int32_t minimumSignificantDigits = -1;
104     int32_t maximumSignificantDigits = -1;
105     std::string roundingPriority;
106     std::string roundingIncrement;
107     std::string roundingMode;
108     RoundingType roundingType = RoundingType::EXCEPTION;
109     std::string localeStr;
110     icu::Locale icuLocale;
111     icu::number::LocalizedNumberFormatter icuNumberFormatter;
112     std::unique_ptr<icu::PluralRules> icuPluralRules = nullptr;
113 };
114 } // namespace I18n
115 } // namespace Global
116 } // namespace OHOS
117 #endif
118