1 /*
2 * Copyright (c) 2021-2022 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 #include "plural_rules.h"
16
17 #include <stringpiece.h>
18
19 #include "algorithm"
20 #include "hilog/log_c.h"
21 #include "hilog/log_cpp.h"
22 #include "locale_config.h"
23 #include "locid.h"
24 #include "plural_rules.h"
25 #include "map"
26 #include "set"
27 #include "string"
28 #include "unicode/unistr.h"
29 #include "unicode/upluralrules.h"
30 #include "utility"
31 #include "utils.h"
32 #include "utypes.h"
33 #include "vector"
34
35 namespace OHOS {
36 namespace Global {
37 namespace I18n {
38 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "IntlJs" };
39 using namespace OHOS::HiviewDFX;
40
ParseOption(std::map<std::string,std::string> & options,const std::string & key)41 std::string PluralRules::ParseOption(std::map<std::string, std::string> &options, const std::string &key)
42 {
43 std::map<std::string, std::string>::iterator it = options.find(key);
44 if (it != options.end()) {
45 return it->second;
46 } else {
47 return "";
48 }
49 }
50
GetValidInteger(std::string & integerStr,int minValue,int maxValue,int defaultValue)51 int PluralRules::GetValidInteger(std::string &integerStr, int minValue, int maxValue, int defaultValue)
52 {
53 int status = 0;
54 int validInteger = ConvertString2Int(integerStr, status);
55 if (status < 0) {
56 validInteger = defaultValue;
57 }
58 if (validInteger < minValue) {
59 validInteger = minValue;
60 }
61 if (validInteger > maxValue) {
62 validInteger = maxValue;
63 }
64 return validInteger;
65 }
66
ParseAllOptions(std::map<std::string,std::string> & options)67 void PluralRules::ParseAllOptions(std::map<std::string, std::string> &options)
68 {
69 localeMatcher = ParseOption(options, "localeMatcher");
70 localeMatcher = (localeMatcher == "") ? "best fit" : localeMatcher;
71 type = ParseOption(options, "type");
72 type = (type == "") ? "cardinal" : type;
73 std::string minIntegerStr = ParseOption(options, "minimumIntegerDigits");
74 // 1 is minValue and defaultValue, 21 is maxValue
75 minInteger = GetValidInteger(minIntegerStr, 1, 21, 1);
76
77 minFraction = 0;
78 maxFraction = 0;
79 std::string minFractionStr = ParseOption(options, "minimumFractionDigits");
80 std::string maxFractionStr = ParseOption(options, "maximumFractionDigits");
81 std::string minSignificantStr = ParseOption(options, "minimumSignificantDigits");
82 std::string maxSignificantStr = ParseOption(options, "maximumSignificantDigits");
83 if (minSignificantStr != "" || maxSignificantStr != "") {
84 // 1 is minValue and defaultValue, 21 is maxValue
85 minSignificant = GetValidInteger(minSignificantStr, 1, 21, 1);
86 // 1 is minValue, 21 is maxValue and defaultValue
87 maxSignificant = GetValidInteger(maxSignificantStr, 1, 21, 21);
88 } else {
89 minSignificant = 0;
90 maxSignificant = 0;
91
92 if (minFractionStr != "" || maxFractionStr != "") {
93 // 0 is minValue and defaultValue, 20 is maxValue
94 minFraction = GetValidInteger(minFractionStr, 0, 20, 0);
95 int maxFractionDefault = std::max(3, minFraction); // 3 is the default value of minFraction
96 int maxFractionMin = std::max(1, minFraction); // 1 is the min value of minFraction
97 // 21 is max value
98 maxFraction = GetValidInteger(maxFractionStr, maxFractionMin, 21, maxFractionDefault);
99 } else {
100 minFraction = 0; // 0 is the default value of minFraction.
101 maxFraction = 3; // 3 is the default value of maxFraction
102 }
103 }
104 }
105
InitPluralRules(std::vector<std::string> & localeTags,std::map<std::string,std::string> & options)106 void PluralRules::InitPluralRules(std::vector<std::string> &localeTags,
107 std::map<std::string, std::string> &options)
108 {
109 UPluralType uPluralType = (type == "cardinal") ? UPLURAL_TYPE_CARDINAL : UPLURAL_TYPE_ORDINAL;
110 UErrorCode status = UErrorCode::U_ZERO_ERROR;
111 localeTags.push_back(LocaleConfig::GetSystemLocale());
112 for (size_t i = 0; i < localeTags.size(); i++) {
113 std::string curLocale = localeTags[i];
114 locale = icu::Locale::forLanguageTag(icu::StringPiece(curLocale), status);
115 if (status != U_ZERO_ERROR) {
116 status = U_ZERO_ERROR;
117 continue;
118 }
119 if (LocaleInfo::allValidLocales.count(locale.getLanguage()) > 0) {
120 localeInfo = std::make_unique<LocaleInfo>(curLocale, options);
121 if (!localeInfo->InitSuccess()) {
122 continue;
123 }
124 locale = localeInfo->GetLocale();
125 localeStr = localeInfo->GetBaseName();
126 pluralRules = icu::PluralRules::forLocale(locale, uPluralType, status);
127 if (status != UErrorCode::U_ZERO_ERROR || !pluralRules) {
128 continue;
129 }
130 createSuccess = true;
131 break;
132 }
133 }
134 if (status != UErrorCode::U_ZERO_ERROR || !pluralRules) {
135 HiLog::Error(LABEL, "PluralRules object created failed");
136 return;
137 }
138 }
139
InitNumberFormatter()140 void PluralRules::InitNumberFormatter()
141 {
142 numberFormatter = icu::number::NumberFormatter::withLocale(locale).roundingMode(UNUM_ROUND_HALFUP);
143 if (minInteger > 1) {
144 numberFormatter = numberFormatter.integerWidth(icu::number::IntegerWidth::zeroFillTo(minInteger));
145 }
146
147 if (minSignificant >= 0) {
148 if (minSignificant > 0) {
149 icu::number::Precision precision = icu::number::Precision::minMaxSignificantDigits(minSignificant,
150 maxSignificant);
151 numberFormatter = numberFormatter.precision(precision);
152 } else {
153 icu::number::Precision precision = icu::number::Precision::minMaxFraction(minFraction, maxFraction);
154 numberFormatter = numberFormatter.precision(precision);
155 }
156 }
157 }
158
PluralRules(std::vector<std::string> & localeTags,std::map<std::string,std::string> & options)159 PluralRules::PluralRules(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options)
160 {
161 ParseAllOptions(options);
162 InitPluralRules(localeTags, options);
163 InitNumberFormatter();
164 }
165
~PluralRules()166 PluralRules::~PluralRules()
167 {
168 if (!pluralRules) {
169 delete pluralRules;
170 pluralRules = nullptr;
171 }
172 }
173
Select(double number)174 std::string PluralRules::Select(double number)
175 {
176 if (!createSuccess || pluralRules == nullptr) {
177 return "other";
178 }
179 UErrorCode status = UErrorCode::U_ZERO_ERROR;
180 icu::number::FormattedNumber formattedNumber = numberFormatter.formatDouble(number, status);
181 if (status != UErrorCode::U_ZERO_ERROR) {
182 status = UErrorCode::U_ZERO_ERROR;
183 formattedNumber = numberFormatter.formatDouble(number, status);
184 }
185 icu::UnicodeString unicodeString = pluralRules->select(formattedNumber, status);
186 std::string result;
187 unicodeString.toUTF8String(result);
188 return result;
189 }
190 } // namespace I18n
191 } // namespace Global
192 } // namespace OHOS