123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_LOCALIZATION_H 1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_LOCALIZATION_H 1823b3eb3cSopenharmony_ci 1923b3eb3cSopenharmony_ci#include <cstdint> 2023b3eb3cSopenharmony_ci#include <functional> 2123b3eb3cSopenharmony_ci#include <future> 2223b3eb3cSopenharmony_ci#include <memory> 2323b3eb3cSopenharmony_ci#include <mutex> 2423b3eb3cSopenharmony_ci#include <new> 2523b3eb3cSopenharmony_ci#include <string> 2623b3eb3cSopenharmony_ci#include <vector> 2723b3eb3cSopenharmony_ci 2823b3eb3cSopenharmony_ci#include "base/utils/date_util.h" 2923b3eb3cSopenharmony_ci#include "base/utils/macros.h" 3023b3eb3cSopenharmony_ci#include "base/utils/noncopyable.h" 3123b3eb3cSopenharmony_ci 3223b3eb3cSopenharmony_cinamespace OHOS::Ace { 3323b3eb3cSopenharmony_ci 3423b3eb3cSopenharmony_cistruct LocaleProxy; 3523b3eb3cSopenharmony_ci 3623b3eb3cSopenharmony_cistruct LunarDate : Date { 3723b3eb3cSopenharmony_ci bool isLeapMonth = false; 3823b3eb3cSopenharmony_ci 3923b3eb3cSopenharmony_ci bool operator==(const LunarDate& lunarDate) const 4023b3eb3cSopenharmony_ci { 4123b3eb3cSopenharmony_ci if (lunarDate.isLeapMonth != isLeapMonth) { 4223b3eb3cSopenharmony_ci return false; 4323b3eb3cSopenharmony_ci } 4423b3eb3cSopenharmony_ci return (lunarDate.year == year && lunarDate.month == month && lunarDate.day == day); 4523b3eb3cSopenharmony_ci } 4623b3eb3cSopenharmony_ci 4723b3eb3cSopenharmony_ci bool operator!=(const LunarDate& lunarDate) const 4823b3eb3cSopenharmony_ci { 4923b3eb3cSopenharmony_ci return !operator==(lunarDate); 5023b3eb3cSopenharmony_ci } 5123b3eb3cSopenharmony_ci}; 5223b3eb3cSopenharmony_ci 5323b3eb3cSopenharmony_cistruct DateTime final : Date { 5423b3eb3cSopenharmony_ci uint32_t hour = 0; 5523b3eb3cSopenharmony_ci uint32_t minute = 0; 5623b3eb3cSopenharmony_ci uint32_t second = 0; 5723b3eb3cSopenharmony_ci}; 5823b3eb3cSopenharmony_ci 5923b3eb3cSopenharmony_cienum DateTimeStyle { NONE, FULL, LONG, MEDIUM, SHORT }; 6023b3eb3cSopenharmony_ci 6123b3eb3cSopenharmony_cienum MeasureFormatStyle { WIDTH_WIDE, WIDTH_SHORT, WIDTH_NARROW, WIDTH_NUMERIC, WIDTH_COUNT }; 6223b3eb3cSopenharmony_ci 6323b3eb3cSopenharmony_cienum TimeUnitStyle { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, MILLISECOND }; 6423b3eb3cSopenharmony_ci 6523b3eb3cSopenharmony_ciclass ACE_FORCE_EXPORT Localization : public NonCopyable { 6623b3eb3cSopenharmony_cipublic: 6723b3eb3cSopenharmony_ci /** 6823b3eb3cSopenharmony_ci * Get language list to select the best language. 6923b3eb3cSopenharmony_ci * @return language list which is supported 7023b3eb3cSopenharmony_ci */ 7123b3eb3cSopenharmony_ci virtual ~Localization(); 7223b3eb3cSopenharmony_ci static const std::vector<std::string>& GetLanguageList(const std::string& language); 7323b3eb3cSopenharmony_ci 7423b3eb3cSopenharmony_ci static std::shared_ptr<Localization> GetInstance(); 7523b3eb3cSopenharmony_ci 7623b3eb3cSopenharmony_ci static void SetLocale(const std::string& language, const std::string& countryOrRegion, const std::string& script, 7723b3eb3cSopenharmony_ci const std::string& selectLanguage, const std::string& keywordsAndValues); 7823b3eb3cSopenharmony_ci 7923b3eb3cSopenharmony_ci static std::string ComputeScript(const std::string& language, const std::string& region); 8023b3eb3cSopenharmony_ci 8123b3eb3cSopenharmony_ci static void ParseLocaleTag(const std::string& languageTag, std::string& language, std::string& script, 8223b3eb3cSopenharmony_ci std::string& region, bool needAddSubtags); 8323b3eb3cSopenharmony_ci 8423b3eb3cSopenharmony_ci void SetOnChange(const std::function<void()>& value) 8523b3eb3cSopenharmony_ci { 8623b3eb3cSopenharmony_ci onChange_ = value; 8723b3eb3cSopenharmony_ci } 8823b3eb3cSopenharmony_ci 8923b3eb3cSopenharmony_ci void HandleOnChange() 9023b3eb3cSopenharmony_ci { 9123b3eb3cSopenharmony_ci if (onChange_) { 9223b3eb3cSopenharmony_ci onChange_(); 9323b3eb3cSopenharmony_ci } 9423b3eb3cSopenharmony_ci } 9523b3eb3cSopenharmony_ci 9623b3eb3cSopenharmony_ci void SetOnMymrChange(const std::function<void(bool)>& value) 9723b3eb3cSopenharmony_ci { 9823b3eb3cSopenharmony_ci onMymrChange_ = value; 9923b3eb3cSopenharmony_ci } 10023b3eb3cSopenharmony_ci 10123b3eb3cSopenharmony_ci const std::function<void(bool)>& GetOnMymrChange() const 10223b3eb3cSopenharmony_ci { 10323b3eb3cSopenharmony_ci return onMymrChange_; 10423b3eb3cSopenharmony_ci } 10523b3eb3cSopenharmony_ci 10623b3eb3cSopenharmony_ci void HandleOnMymrChange(bool isZawgyiMyanmar) 10723b3eb3cSopenharmony_ci { 10823b3eb3cSopenharmony_ci if (onMymrChange_) { 10923b3eb3cSopenharmony_ci onMymrChange_(isZawgyiMyanmar); 11023b3eb3cSopenharmony_ci } 11123b3eb3cSopenharmony_ci } 11223b3eb3cSopenharmony_ci 11323b3eb3cSopenharmony_ci bool GetDateColumnFormatOrder(std::vector<std::string>& outOrder); 11423b3eb3cSopenharmony_ci 11523b3eb3cSopenharmony_ci bool GetDateOrder(std::vector<std::string>& outOrder); 11623b3eb3cSopenharmony_ci 11723b3eb3cSopenharmony_ci bool IsAmPmHour() 11823b3eb3cSopenharmony_ci { 11923b3eb3cSopenharmony_ci bool isAmPm = false; 12023b3eb3cSopenharmony_ci bool hasZero = true; 12123b3eb3cSopenharmony_ci GetHourFormat(isAmPm, hasZero); 12223b3eb3cSopenharmony_ci return isAmPm; 12323b3eb3cSopenharmony_ci } 12423b3eb3cSopenharmony_ci 12523b3eb3cSopenharmony_ci bool HasZeroHour() 12623b3eb3cSopenharmony_ci { 12723b3eb3cSopenharmony_ci bool isAmPm = false; 12823b3eb3cSopenharmony_ci bool hasZero = true; 12923b3eb3cSopenharmony_ci GetHourFormat(isAmPm, hasZero); 13023b3eb3cSopenharmony_ci return hasZero; 13123b3eb3cSopenharmony_ci } 13223b3eb3cSopenharmony_ci 13323b3eb3cSopenharmony_ci std::string GetLanguage(); 13423b3eb3cSopenharmony_ci std::string GetLanguageTag(); 13523b3eb3cSopenharmony_ci std::string GetFontLocale(); 13623b3eb3cSopenharmony_ci 13723b3eb3cSopenharmony_ci /** 13823b3eb3cSopenharmony_ci * For formatting local format duration. Cannot be formatted correctly when duration greater than 24 13923b3eb3cSopenharmony_ci * hours. For example: 10:00. 14023b3eb3cSopenharmony_ci * @param duration The value used to set the duration, the range is 0 to 90,000. 14123b3eb3cSopenharmony_ci * @return local format duration. 14223b3eb3cSopenharmony_ci */ 14323b3eb3cSopenharmony_ci const std::string FormatDuration(uint32_t duration, bool needShowHour = false); 14423b3eb3cSopenharmony_ci 14523b3eb3cSopenharmony_ci /** 14623b3eb3cSopenharmony_ci * For formatting local format duration. Cannot be formatted correctly when duration greater than 24 14723b3eb3cSopenharmony_ci * hours. For example: 10:00. 14823b3eb3cSopenharmony_ci * @param duration The value used to set the duration, the range is 0 to 90,000. 14923b3eb3cSopenharmony_ci * @param format the pattern for the format.For example: HH-mm-ss-SS. 15023b3eb3cSopenharmony_ci * @return local format duration. 15123b3eb3cSopenharmony_ci */ 15223b3eb3cSopenharmony_ci std::string FormatDuration(uint32_t duration, const std::string& format); 15323b3eb3cSopenharmony_ci 15423b3eb3cSopenharmony_ci /** 15523b3eb3cSopenharmony_ci * For formatting date time. For example: 2020/03/30 08:00:00. 15623b3eb3cSopenharmony_ci * @param dateTime The value of date time. 15723b3eb3cSopenharmony_ci * @param format the pattern for the format. 15823b3eb3cSopenharmony_ci * @return local format date time. 15923b3eb3cSopenharmony_ci */ 16023b3eb3cSopenharmony_ci const std::string FormatDateTime(DateTime dateTime, const std::string& format); 16123b3eb3cSopenharmony_ci 16223b3eb3cSopenharmony_ci /** 16323b3eb3cSopenharmony_ci * For formatting date time. 16423b3eb3cSopenharmony_ci * @param dateTime The value of date time. 16523b3eb3cSopenharmony_ci * @param dateStyle The value of date style. 16623b3eb3cSopenharmony_ci * @param timeStyle The value of time style. 16723b3eb3cSopenharmony_ci * @return local format date time. 16823b3eb3cSopenharmony_ci */ 16923b3eb3cSopenharmony_ci const std::string FormatDateTime(DateTime dateTime, DateTimeStyle dateStyle, DateTimeStyle timeStyle); 17023b3eb3cSopenharmony_ci 17123b3eb3cSopenharmony_ci /** 17223b3eb3cSopenharmony_ci * Gets month strings. For example: "January", "February", etc. 17323b3eb3cSopenharmony_ci * @param isShortType The month style. 17423b3eb3cSopenharmony_ci * @param calendarType The calendar style. 17523b3eb3cSopenharmony_ci * @return the month string vector. 17623b3eb3cSopenharmony_ci */ 17723b3eb3cSopenharmony_ci std::vector<std::string> GetMonths(bool isShortType = false, const std::string& calendarType = ""); 17823b3eb3cSopenharmony_ci 17923b3eb3cSopenharmony_ci /** 18023b3eb3cSopenharmony_ci * Gets weekdays strings. For example: "Monday", "Tuesday", etc. 18123b3eb3cSopenharmony_ci * @param isShortType The weekday style. 18223b3eb3cSopenharmony_ci * @return the weekday string vector. 18323b3eb3cSopenharmony_ci */ 18423b3eb3cSopenharmony_ci std::vector<std::string> GetWeekdays(bool isShortType = false); 18523b3eb3cSopenharmony_ci 18623b3eb3cSopenharmony_ci /** 18723b3eb3cSopenharmony_ci * Gets AM/PM strings. For example: "AM", "PM". 18823b3eb3cSopenharmony_ci * @return the AM/PM string vector. 18923b3eb3cSopenharmony_ci */ 19023b3eb3cSopenharmony_ci std::vector<std::string> GetAmPmStrings(); 19123b3eb3cSopenharmony_ci 19223b3eb3cSopenharmony_ci /** 19323b3eb3cSopenharmony_ci * Gets relative date. For example: "yesterday", "today", "tomorrow", "in 2 days", etc. 19423b3eb3cSopenharmony_ci * @param offset The relative date time offset. 19523b3eb3cSopenharmony_ci * @return the relative date string. 19623b3eb3cSopenharmony_ci */ 19723b3eb3cSopenharmony_ci std::string GetRelativeDateTime(double offset); 19823b3eb3cSopenharmony_ci 19923b3eb3cSopenharmony_ci /** 20023b3eb3cSopenharmony_ci * Gets lunar date. 20123b3eb3cSopenharmony_ci * @param date the western calendar date. 20223b3eb3cSopenharmony_ci * @return the lunar calendar date. 20323b3eb3cSopenharmony_ci */ 20423b3eb3cSopenharmony_ci LunarDate GetLunarDate(Date date); 20523b3eb3cSopenharmony_ci 20623b3eb3cSopenharmony_ci /** 20723b3eb3cSopenharmony_ci * Gets lunar month. 20823b3eb3cSopenharmony_ci * @param month the western calendar month. 20923b3eb3cSopenharmony_ci * @param isLeapMonth is a leap month. 21023b3eb3cSopenharmony_ci * @return the lunar calendar month. 21123b3eb3cSopenharmony_ci */ 21223b3eb3cSopenharmony_ci std::string GetLunarMonth(uint32_t month, bool isLeapMonth); 21323b3eb3cSopenharmony_ci 21423b3eb3cSopenharmony_ci /** 21523b3eb3cSopenharmony_ci * Gets lunar day. 21623b3eb3cSopenharmony_ci * @param dayOfMonth the western calendar day. 21723b3eb3cSopenharmony_ci * @return the lunar calendar day. 21823b3eb3cSopenharmony_ci */ 21923b3eb3cSopenharmony_ci std::string GetLunarDay(uint32_t dayOfMonth); 22023b3eb3cSopenharmony_ci 22123b3eb3cSopenharmony_ci /** 22223b3eb3cSopenharmony_ci * For formatting time unit. For example: "8hours", "8min", etc . 22323b3eb3cSopenharmony_ci * @param timeValue the format time value. 22423b3eb3cSopenharmony_ci * @param timeStyle the time unit style. 22523b3eb3cSopenharmony_ci * @param formatStyle the measure format style. 22623b3eb3cSopenharmony_ci * @return local format time unit. 22723b3eb3cSopenharmony_ci */ 22823b3eb3cSopenharmony_ci std::string TimeUnitFormat(double timeValue, TimeUnitStyle timeStyle, MeasureFormatStyle formatStyle); 22923b3eb3cSopenharmony_ci 23023b3eb3cSopenharmony_ci /** 23123b3eb3cSopenharmony_ci * For formatting plural rules. 23223b3eb3cSopenharmony_ci * @param number the number to be formatted. 23323b3eb3cSopenharmony_ci * @param isCardinal the plural is cardinal type. 23423b3eb3cSopenharmony_ci * @return local keyword of the plural rule. 23523b3eb3cSopenharmony_ci */ 23623b3eb3cSopenharmony_ci std::string PluralRulesFormat(double number, bool isCardinal = true); 23723b3eb3cSopenharmony_ci 23823b3eb3cSopenharmony_ci /** 23923b3eb3cSopenharmony_ci * Gets Letter strings for indexer. For example: "A", "B", etc. 24023b3eb3cSopenharmony_ci * @return the letter string vector. 24123b3eb3cSopenharmony_ci */ 24223b3eb3cSopenharmony_ci std::vector<std::u16string> GetIndexLetter(); 24323b3eb3cSopenharmony_ci 24423b3eb3cSopenharmony_ci /** 24523b3eb3cSopenharmony_ci * For formatting number. 24623b3eb3cSopenharmony_ci * @param number the number to be formatted. 24723b3eb3cSopenharmony_ci * @return local format number. 24823b3eb3cSopenharmony_ci */ 24923b3eb3cSopenharmony_ci std::string NumberFormat(double number); 25023b3eb3cSopenharmony_ci 25123b3eb3cSopenharmony_ci /** 25223b3eb3cSopenharmony_ci * Gets alphabet strings. For example: "A", "B", etc. 25323b3eb3cSopenharmony_ci * @return the alphabet string vector. 25423b3eb3cSopenharmony_ci */ 25523b3eb3cSopenharmony_ci std::vector<std::u16string> GetIndexAlphabet(); 25623b3eb3cSopenharmony_ci 25723b3eb3cSopenharmony_ci /** 25823b3eb3cSopenharmony_ci * Gets entry letters, read from resource/binary/entry.json. 25923b3eb3cSopenharmony_ci * @param lettersIndex letters index, like "common.ok" 26023b3eb3cSopenharmony_ci * @return letters 26123b3eb3cSopenharmony_ci */ 26223b3eb3cSopenharmony_ci std::string GetEntryLetters(const std::string& lettersIndex); 26323b3eb3cSopenharmony_ci 26423b3eb3cSopenharmony_ci /** 26523b3eb3cSopenharmony_ci * Gets error description, read from resource/binary/errorcode.json. 26623b3eb3cSopenharmony_ci * @param errorIndex error index, like "error_video_000001" 26723b3eb3cSopenharmony_ci * @return error description 26823b3eb3cSopenharmony_ci */ 26923b3eb3cSopenharmony_ci std::string GetErrorDescription(const std::string& errorIndex); 27023b3eb3cSopenharmony_ci 27123b3eb3cSopenharmony_ciprivate: 27223b3eb3cSopenharmony_ci void SetLocaleImpl(const std::string& language, const std::string& countryOrRegion, const std::string& script, 27323b3eb3cSopenharmony_ci const std::string& selectLanguage, const std::string& keywordsAndValues); 27423b3eb3cSopenharmony_ci std::vector<std::u16string> GetLetters(bool alphabet); 27523b3eb3cSopenharmony_ci bool GetHourFormat(bool& isAmPm, bool& hasZero); 27623b3eb3cSopenharmony_ci bool Contain(const std::string& str, const std::string& tag); 27723b3eb3cSopenharmony_ci LunarDate GetIcuLunarDate(Date date); 27823b3eb3cSopenharmony_ci 27923b3eb3cSopenharmony_ci std::unique_ptr<LocaleProxy> locale_; 28023b3eb3cSopenharmony_ci std::string languageTag_; 28123b3eb3cSopenharmony_ci std::string selectLanguage_; 28223b3eb3cSopenharmony_ci std::string fontLocale_; 28323b3eb3cSopenharmony_ci 28423b3eb3cSopenharmony_ci std::promise<bool> promise_; 28523b3eb3cSopenharmony_ci std::shared_future<bool> future_ = promise_.get_future(); 28623b3eb3cSopenharmony_ci std::function<void()> onChange_; 28723b3eb3cSopenharmony_ci std::function<void(bool)> onMymrChange_; 28823b3eb3cSopenharmony_ci bool isPromiseUsed_ = false; 28923b3eb3cSopenharmony_ci bool isInit_ = false; 29023b3eb3cSopenharmony_ci 29123b3eb3cSopenharmony_ci void WaitingForInit() 29223b3eb3cSopenharmony_ci { 29323b3eb3cSopenharmony_ci if (!isInit_) { 29423b3eb3cSopenharmony_ci isInit_ = future_.get(); 29523b3eb3cSopenharmony_ci } 29623b3eb3cSopenharmony_ci } 29723b3eb3cSopenharmony_ci 29823b3eb3cSopenharmony_ci static std::mutex mutex_; 29923b3eb3cSopenharmony_ci static std::shared_ptr<Localization> instance_; 30023b3eb3cSopenharmony_ci static bool firstInstance_; 30123b3eb3cSopenharmony_ci}; 30223b3eb3cSopenharmony_ci 30323b3eb3cSopenharmony_ci} // namespace OHOS::Ace 30423b3eb3cSopenharmony_ci 30523b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_LOCALIZATION_H 306