• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "i18n_hilog.h"
17 #include "locale_config.h"
18 #include "ohos/init_data.h"
19 #include "unicode/locid.h"
20 #include "utils.h"
21 #include "simple_number_format.h"
22 
23 namespace OHOS {
24 namespace Global {
25 namespace I18n {
26 bool SimpleNumberFormat::icuInitialized = SimpleNumberFormat::Init();
27 
SimpleNumberFormat(const std::string & skeleton,std::shared_ptr<LocaleInfo> localeInfo,I18nErrorCode & errCode)28 SimpleNumberFormat::SimpleNumberFormat(const std::string& skeleton, std::shared_ptr<LocaleInfo> localeInfo,
29     I18nErrorCode& errCode)
30 {
31     std::string locale;
32     if (localeInfo == nullptr) {
33         locale = LocaleConfig::GetEffectiveLocale();
34     } else {
35         locale = localeInfo->ToString();
36     }
37     SimpleNumberFormat::InitSimpleNumberFormat(skeleton, locale, errCode);
38 }
39 
SimpleNumberFormat(const std::string & skeleton,const std::string & localeTag,I18nErrorCode & errCode)40 SimpleNumberFormat::SimpleNumberFormat(const std::string& skeleton, const std::string& localeTag,
41     I18nErrorCode& errCode)
42 {
43     std::string locale = localeTag;
44     if (locale.empty()) {
45         locale = LocaleConfig::GetEffectiveLocale();
46     }
47     SimpleNumberFormat::InitSimpleNumberFormat(skeleton, locale, errCode);
48 }
49 
InitSimpleNumberFormat(const std::string & skeleton,const std::string & locale,I18nErrorCode & errCode)50 void SimpleNumberFormat::InitSimpleNumberFormat(const std::string& skeleton, const std::string& locale,
51     I18nErrorCode& errCode)
52 {
53     if (skeleton.empty()) {
54         HILOG_ERROR_I18N("SimpleNumberFormat::InitSimpleNumberFormat: Skeleton is empty.");
55         errCode = I18nErrorCode::INVALID_NUMBER_FORMAT_SKELETON;
56         return;
57     }
58 
59     if (locale.empty() || !LocaleConfig::IsValidTag(locale)) {
60         HILOG_ERROR_I18N("SimpleNumberFormat::InitSimpleNumberFormat: Locale %{public}s is invalid.", locale.c_str());
61         errCode = I18nErrorCode::INVALID_LOCALE_TAG;
62         return;
63     }
64     UErrorCode status = U_ZERO_ERROR;
65     icu::Locale icuLocale = icu::Locale::forLanguageTag(icu::StringPiece(locale), status);
66     if (U_FAILURE(status)) {
67         HILOG_ERROR_I18N("SimpleNumberFormat::InitSimpleNumberFormat: Create icu::Locale failed.");
68         errCode = I18nErrorCode::FAILED;
69         return;
70     }
71 
72     icu::number::UnlocalizedNumberFormatter unlocalizedNumberFormat =
73         icu::number::NumberFormatter::forSkeleton(skeleton.c_str(), status);
74     if (U_FAILURE(status)) {
75         HILOG_ERROR_I18N("SimpleNumberFormat::InitSimpleNumberFormat: Create UnlocalizedNumberFormatter failed.");
76         if (status == U_NUMBER_SKELETON_SYNTAX_ERROR) {
77             HILOG_ERROR_I18N("SimpleNumberFormat::InitSimpleNumberFormat: Invalid skeleton %{public}s.",
78                 skeleton.c_str());
79             errCode = I18nErrorCode::INVALID_NUMBER_FORMAT_SKELETON;
80             return;
81         }
82         errCode = I18nErrorCode::FAILED;
83         return;
84     }
85     simpleNumberFormat = unlocalizedNumberFormat.locale(icuLocale);
86     initSuccess = true;
87 }
88 
~SimpleNumberFormat()89 SimpleNumberFormat::~SimpleNumberFormat() {}
90 
Format(double number)91 std::string SimpleNumberFormat::Format(double number)
92 {
93     icu::number::FormattedNumber formattedNumber = FormatToFormattedNumber(number);
94     UErrorCode status = U_ZERO_ERROR;
95     icu::UnicodeString formatResult = formattedNumber.toString(status);
96     if (U_FAILURE(status)) {
97         HILOG_ERROR_I18N("SimpleNumberFormat::Format: FormattedNumber convert to string failed.");
98         return PseudoLocalizationProcessor("");
99     }
100     std::string result;
101     formatResult.toUTF8String(result);
102     return PseudoLocalizationProcessor(result);
103 }
104 
Init()105 bool SimpleNumberFormat::Init()
106 {
107     SetHwIcuDirectory();
108     return true;
109 }
110 
FormatToFormattedNumber(double number)111 icu::number::FormattedNumber SimpleNumberFormat::FormatToFormattedNumber(double number)
112 {
113     if (!initSuccess) {
114         HILOG_ERROR_I18N("SimpleNumberFormat::FormatToFormattedNumber: Init failed.");
115         return {};
116     }
117     UErrorCode status = U_ZERO_ERROR;
118     icu::number::FormattedNumber formattedNumber = simpleNumberFormat.formatDouble(number, status);
119     if (U_FAILURE(status)) {
120         HILOG_ERROR_I18N("SimpleNumberFormat::FormatToFormattedNumber: Format double failed.");
121         return {};
122     }
123     return formattedNumber;
124 }
125 } // namespace I18n
126 } // namespace Global
127 } // namespace OHOS
128