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 IsAmPmHour()115 bool IsAmPmHour() 116 { 117 bool isAmPm = false; 118 bool hasZero = true; 119 GetHourFormat(isAmPm, hasZero); 120 return isAmPm; 121 } 122 HasZeroHour()123 bool HasZeroHour() 124 { 125 bool isAmPm = false; 126 bool hasZero = true; 127 GetHourFormat(isAmPm, hasZero); 128 return hasZero; 129 } 130 131 std::string GetLanguage(); 132 std::string GetLanguageTag(); 133 std::string GetFontLocale(); 134 135 /** 136 * For formatting local format duration. Cannot be formatted correctly when duration greater than 24 137 * hours. For example: 10:00. 138 * @param duration The value used to set the duration, the range is 0 to 90,000. 139 * @return local format duration. 140 */ 141 const std::string FormatDuration(uint32_t duration, bool needShowHour = false); 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 * @param format the pattern for the format.For example: HH-mm-ss-SS. 148 * @return local format duration. 149 */ 150 std::string FormatDuration(uint32_t duration, const std::string& format); 151 152 /** 153 * For formatting date time. For example: 2020/03/30 08:00:00. 154 * @param dateTime The value of date time. 155 * @param format the pattern for the format. 156 * @return local format date time. 157 */ 158 const std::string FormatDateTime(DateTime dateTime, const std::string& format); 159 160 /** 161 * For formatting date time. 162 * @param dateTime The value of date time. 163 * @param dateStyle The value of date style. 164 * @param timeStyle The value of time style. 165 * @return local format date time. 166 */ 167 const std::string FormatDateTime(DateTime dateTime, DateTimeStyle dateStyle, DateTimeStyle timeStyle); 168 169 /** 170 * Gets month strings. For example: "January", "February", etc. 171 * @param isShortType The month style. 172 * @param calendarType The calendar style. 173 * @return the month string vector. 174 */ 175 std::vector<std::string> GetMonths(bool isShortType = false, const std::string& calendarType = ""); 176 177 /** 178 * Gets weekdays strings. For example: "Monday", "Tuesday", etc. 179 * @param isShortType The weekday style. 180 * @return the weekday string vector. 181 */ 182 std::vector<std::string> GetWeekdays(bool isShortType = false); 183 184 /** 185 * Gets AM/PM strings. For example: "AM", "PM". 186 * @return the AM/PM string vector. 187 */ 188 std::vector<std::string> GetAmPmStrings(); 189 190 /** 191 * Gets relative date. For example: "yesterday", "today", "tomorrow", "in 2 days", etc. 192 * @param offset The relative date time offset. 193 * @return the relative date string. 194 */ 195 std::string GetRelativeDateTime(double offset); 196 197 /** 198 * Gets lunar date. 199 * @param date the western calendar date. 200 * @return the lunar calendar date. 201 */ 202 LunarDate GetLunarDate(Date date); 203 204 /** 205 * Gets lunar month. 206 * @param month the western calendar month. 207 * @param isLeapMonth is a leap month. 208 * @return the lunar calendar month. 209 */ 210 std::string GetLunarMonth(uint32_t month, bool isLeapMonth); 211 212 /** 213 * Gets lunar day. 214 * @param dayOfMonth the western calendar day. 215 * @return the lunar calendar day. 216 */ 217 std::string GetLunarDay(uint32_t dayOfMonth); 218 219 /** 220 * For formatting time unit. For example: "8hours", "8min", etc . 221 * @param timeValue the format time value. 222 * @param timeStyle the time unit style. 223 * @param formatStyle the measure format style. 224 * @return local format time unit. 225 */ 226 std::string TimeUnitFormat(double timeValue, TimeUnitStyle timeStyle, MeasureFormatStyle formatStyle); 227 228 /** 229 * For formatting plural rules. 230 * @param number the number to be formatted. 231 * @param isCardinal the plural is cardinal type. 232 * @return local keyword of the plural rule. 233 */ 234 std::string PluralRulesFormat(double number, bool isCardinal = true); 235 236 /** 237 * Gets Letter strings for indexer. For example: "A", "B", etc. 238 * @return the letter string vector. 239 */ 240 std::vector<std::u16string> GetIndexLetter(); 241 242 /** 243 * For formatting number. 244 * @param number the number to be formatted. 245 * @return local format number. 246 */ 247 std::string NumberFormat(double number); 248 249 /** 250 * Gets alphabet strings. For example: "A", "B", etc. 251 * @return the alphabet string vector. 252 */ 253 std::vector<std::u16string> GetIndexAlphabet(); 254 255 /** 256 * Gets entry letters, read from resource/binary/entry.json. 257 * @param lettersIndex letters index, like "common.ok" 258 * @return letters 259 */ 260 std::string GetEntryLetters(const std::string& lettersIndex); 261 262 /** 263 * Gets error description, read from resource/binary/errorcode.json. 264 * @param errorIndex error index, like "error_video_000001" 265 * @return error description 266 */ 267 std::string GetErrorDescription(const std::string& errorIndex); 268 269 private: 270 void SetLocaleImpl(const std::string& language, const std::string& countryOrRegion, const std::string& script, 271 const std::string& selectLanguage, const std::string& keywordsAndValues); 272 std::vector<std::u16string> GetLetters(bool alphabet); 273 bool GetHourFormat(bool& isAmPm, bool& hasZero); 274 bool Contain(const std::string& str, const std::string& tag); 275 276 std::unique_ptr<LocaleProxy> locale_; 277 std::string languageTag_; 278 std::string selectLanguage_; 279 std::string fontLocale_; 280 281 std::promise<bool> promise_; 282 std::shared_future<bool> future_ = promise_.get_future(); 283 std::function<void()> onChange_; 284 std::function<void(bool)> onMymrChange_; 285 bool isPromiseUsed_ = false; 286 bool isInit_ = false; 287 WaitingForInit()288 void WaitingForInit() 289 { 290 if (!isInit_) { 291 isInit_ = future_.get(); 292 } 293 } 294 295 static std::mutex mutex_; 296 static std::shared_ptr<Localization> instance_; 297 static bool firstInstance_; 298 }; 299 300 } // namespace OHOS::Ace 301 302 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_LOCALIZATION_H 303