• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_LOCALIZATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_LOCALIZATION_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <future>
22 #include <memory>
23 #include <mutex>
24 #include <new>
25 #include <string>
26 #include <vector>
27 
28 #include "base/utils/date_util.h"
29 #include "base/utils/macros.h"
30 #include "base/utils/noncopyable.h"
31 
32 namespace OHOS::Ace {
33 
34 struct LocaleProxy;
35 
36 struct LunarDate : Date {
37     bool isLeapMonth = false;
38 
39     bool operator==(const LunarDate& lunarDate) const
40     {
41         if (lunarDate.isLeapMonth != isLeapMonth) {
42             return false;
43         }
44         return (lunarDate.year == year && lunarDate.month == month && lunarDate.day == day);
45     }
46 
47     bool operator!=(const LunarDate& lunarDate) const
48     {
49         return !operator==(lunarDate);
50     }
51 };
52 
53 struct DateTime final : Date {
54     uint32_t hour = 0;
55     uint32_t minute = 0;
56     uint32_t second = 0;
57 };
58 
59 enum DateTimeStyle { NONE, FULL, LONG, MEDIUM, SHORT };
60 
61 enum MeasureFormatStyle { WIDTH_WIDE, WIDTH_SHORT, WIDTH_NARROW, WIDTH_NUMERIC, WIDTH_COUNT };
62 
63 enum TimeUnitStyle { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND };
64 
65 class ACE_FORCE_EXPORT Localization : public NonCopyable {
66 public:
67     /**
68      * Get language list to select the best language.
69      * @return language list which is supported
70      */
71     virtual ~Localization();
72     static const std::vector<std::string>& GetLanguageList(const std::string& language);
73 
74     static std::shared_ptr<Localization> GetInstance();
75 
76     static void SetLocale(const std::string& language, const std::string& countryOrRegion, const std::string& script,
77         const std::string& selectLanguage, const std::string& keywordsAndValues);
78 
79     static std::string ComputeScript(const std::string& language, const std::string& region);
80 
81     static void ParseLocaleTag(const std::string& languageTag, std::string& language, std::string& script,
82         std::string& region, bool needAddSubtags);
83 
SetOnChange(const std::function<void ()> & value)84     void SetOnChange(const std::function<void()>& value)
85     {
86         onChange_ = value;
87     }
88 
HandleOnChange()89     void HandleOnChange()
90     {
91         if (onChange_) {
92             onChange_();
93         }
94     }
95 
SetOnMymrChange(const std::function<void (bool)> & value)96     void SetOnMymrChange(const std::function<void(bool)>& value)
97     {
98         onMymrChange_ = value;
99     }
100 
GetOnMymrChange()101     const std::function<void(bool)>& GetOnMymrChange() const
102     {
103         return onMymrChange_;
104     }
105 
HandleOnMymrChange(bool isZawgyiMyanmar)106     void HandleOnMymrChange(bool isZawgyiMyanmar)
107     {
108         if (onMymrChange_) {
109             onMymrChange_(isZawgyiMyanmar);
110         }
111     }
112 
113     bool GetDateColumnFormatOrder(std::vector<std::string>& outOrder);
114 
115     bool GetDateOrder(std::vector<std::string>& outOrder);
116 
IsAmPmHour()117     bool IsAmPmHour()
118     {
119         bool isAmPm = false;
120         bool hasZero = true;
121         GetHourFormat(isAmPm, hasZero);
122         return isAmPm;
123     }
124 
HasZeroHour()125     bool HasZeroHour()
126     {
127         bool isAmPm = false;
128         bool hasZero = true;
129         GetHourFormat(isAmPm, hasZero);
130         return hasZero;
131     }
132 
IsInit()133     bool IsInit() const
134     {
135         std::lock_guard<std::mutex> lock(mutex_);
136         return isInit_;
137     }
138 
139     std::string GetLanguage();
140     std::string GetLanguageTag();
141     std::string GetFontLocale();
142 
143     /**
144      * For formatting local format duration. Cannot be formatted correctly when duration greater than 24
145      * hours. For example: 10:00.
146      * @param duration    The value used to set the duration, the range is 0 to 90,000.
147      * @return local format duration.
148      */
149     const std::string FormatDuration(uint32_t duration, bool needShowHour = false);
150 
151     /**
152      * For formatting local format duration. Cannot be formatted correctly when duration greater than 24
153      * hours. For example: 10:00.
154      * @param duration    The value used to set the duration, the range is 0 to 90,000.
155      * @param format      the pattern for the format.For example: HH-mm-ss-SS.
156      * @return local format duration.
157      */
158     std::string FormatDuration(uint32_t duration, const std::string& format);
159 
160     /**
161      * For formatting date time. For example: 2020/03/30 08:00:00.
162      * @param dateTime    The value of date time.
163      * @param format      the pattern for the format.
164      * @return local format date time.
165      */
166     const std::string FormatDateTime(DateTime dateTime, const std::string& format);
167 
168     /**
169      * For formatting date time.
170      * @param dateTime     The value of date time.
171      * @param dateStyle    The value of date style.
172      * @param timeStyle    The value of time style.
173      * @return local format date time.
174      */
175     const std::string FormatDateTime(DateTime dateTime, DateTimeStyle dateStyle, DateTimeStyle timeStyle);
176 
177     /**
178      * Gets month strings. For example: "January", "February", etc.
179      * @param isShortType    The month style.
180      * @param calendarType   The calendar style.
181      * @return the month string vector.
182      */
183     std::vector<std::string> GetMonths(bool isShortType = false, const std::string& calendarType = "");
184 
185     /**
186      * Gets weekdays strings. For example: "Monday", "Tuesday", etc.
187      * @param isShortType    The weekday style.
188      * @return the weekday string vector.
189      */
190     std::vector<std::string> GetWeekdays(bool isShortType = false);
191 
192     /**
193      * Gets AM/PM strings. For example: "AM", "PM".
194      * @return the AM/PM string vector.
195      */
196     std::vector<std::string> GetAmPmStrings();
197 
198     /**
199      * Gets relative date. For example: "yesterday", "today", "tomorrow", "in 2 days", etc.
200      * @param offset    The relative date time offset.
201      * @return the relative date string.
202      */
203     std::string GetRelativeDateTime(double offset);
204 
205     /**
206      * Gets lunar date.
207      * @param date the western calendar date.
208      * @return the lunar calendar date.
209      */
210     LunarDate GetLunarDate(Date date);
211 
212     /**
213      * Gets lunar month.
214      * @param month the western calendar month.
215      * @param isLeapMonth is a leap month.
216      * @return the lunar calendar month.
217      */
218     std::string GetLunarMonth(uint32_t month, bool isLeapMonth);
219 
220     /**
221      * Gets lunar day.
222      * @param dayOfMonth the western calendar day.
223      * @return the lunar calendar day.
224      */
225     std::string GetLunarDay(uint32_t dayOfMonth);
226 
227     /**
228      * For formatting time unit. For example: "8hours", "8min", etc .
229      * @param timeValue the format time value.
230      * @param timeStyle the time unit style.
231      * @param formatStyle the measure format style.
232      * @return local format time unit.
233      */
234     std::string TimeUnitFormat(double timeValue, TimeUnitStyle timeStyle, MeasureFormatStyle formatStyle);
235 
236     /**
237      * For formatting plural rules.
238      * @param number the number to be formatted.
239      * @param isCardinal the plural is cardinal type.
240      * @return local keyword of the plural rule.
241      */
242     std::string PluralRulesFormat(double number, bool isCardinal = true);
243 
244     /**
245      * Gets Letter strings for indexer. For example: "A", "B", etc.
246      * @return the letter string vector.
247      */
248     std::vector<std::u16string> GetIndexLetter();
249 
250     /**
251      * For formatting number.
252      * @param number the number to be formatted.
253      * @return local format number.
254      */
255     std::string NumberFormat(double number);
256 
257     /**
258      * Gets alphabet strings. For example: "A", "B", etc.
259      * @return the alphabet string vector.
260      */
261     std::vector<std::u16string> GetIndexAlphabet();
262 
263     /**
264      * Gets entry letters, read from resource/binary/entry.json.
265      * @param lettersIndex letters index, like "common.ok"
266      * @return letters
267      */
268     std::string GetEntryLetters(const std::string& lettersIndex);
269 
270     /**
271      * Gets error description, read from resource/binary/errorcode.json.
272      * @param errorIndex error index, like "error_video_000001"
273      * @return error description
274      */
275     std::string GetErrorDescription(const std::string& errorIndex);
276 
277 private:
278     void SetLocaleImpl(const std::string& language, const std::string& countryOrRegion, const std::string& script,
279         const std::string& selectLanguage, const std::string& keywordsAndValues);
280     std::vector<std::u16string> GetLetters(bool alphabet);
281     bool GetHourFormat(bool& isAmPm, bool& hasZero);
282     bool Contain(const std::string& str, const std::string& tag);
283     LunarDate GetIcuLunarDate(Date date);
284 
285     std::unique_ptr<LocaleProxy> locale_;
286     std::string languageTag_;
287     std::string selectLanguage_;
288     std::string fontLocale_;
289 
290     std::promise<bool> promise_;
291     std::shared_future<bool> future_ = promise_.get_future();
292     std::function<void()> onChange_;
293     std::function<void(bool)> onMymrChange_;
294     bool isPromiseUsed_ = false;
295     bool isInit_ = false;
296 
WaitingForInit()297     void WaitingForInit()
298     {
299         if (!isInit_) {
300             isInit_ = future_.get();
301         }
302     }
303 
304     static std::mutex mutex_;
305     static std::shared_ptr<Localization> instance_;
306     static bool firstInstance_;
307 };
308 
309 } // namespace OHOS::Ace
310 
311 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_LOCALIZATION_H
312