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