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