19596a2c1Sopenharmony_ci/*
29596a2c1Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
39596a2c1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49596a2c1Sopenharmony_ci * you may not use this file except in compliance with the License.
59596a2c1Sopenharmony_ci * You may obtain a copy of the License at
69596a2c1Sopenharmony_ci *
79596a2c1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89596a2c1Sopenharmony_ci *
99596a2c1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109596a2c1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119596a2c1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129596a2c1Sopenharmony_ci * See the License for the specific language governing permissions and
139596a2c1Sopenharmony_ci * limitations under the License.
149596a2c1Sopenharmony_ci */
159596a2c1Sopenharmony_ci#ifndef OHOS_GLOBAL_I18N_LOCALE_CONFIG_H
169596a2c1Sopenharmony_ci#define OHOS_GLOBAL_I18N_LOCALE_CONFIG_H
179596a2c1Sopenharmony_ci
189596a2c1Sopenharmony_ci#include <map>
199596a2c1Sopenharmony_ci#include <mutex>
209596a2c1Sopenharmony_ci#include <vector>
219596a2c1Sopenharmony_ci#include <set>
229596a2c1Sopenharmony_ci#include <string>
239596a2c1Sopenharmony_ci#include <unordered_map>
249596a2c1Sopenharmony_ci#include <unordered_set>
259596a2c1Sopenharmony_ci#ifdef SUPPORT_GRAPHICS
269596a2c1Sopenharmony_ci#include "configuration.h"
279596a2c1Sopenharmony_ci#include <common_event_data.h>
289596a2c1Sopenharmony_ci#endif
299596a2c1Sopenharmony_ci#include "i18n_types.h"
309596a2c1Sopenharmony_ci#include "unicode/locid.h"
319596a2c1Sopenharmony_ci
329596a2c1Sopenharmony_cinamespace OHOS {
339596a2c1Sopenharmony_cinamespace Global {
349596a2c1Sopenharmony_cinamespace I18n {
359596a2c1Sopenharmony_ciclass LocaleConfig {
369596a2c1Sopenharmony_cipublic:
379596a2c1Sopenharmony_ci    LocaleConfig() = default;
389596a2c1Sopenharmony_ci    virtual ~LocaleConfig() = default;
399596a2c1Sopenharmony_ci
409596a2c1Sopenharmony_ci    /**
419596a2c1Sopenharmony_ci     * @brief Set the System Language to language Tag.
429596a2c1Sopenharmony_ci     *
439596a2c1Sopenharmony_ci     * @param languageTag language tag to set.
449596a2c1Sopenharmony_ci     * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
459596a2c1Sopenharmony_ci     */
469596a2c1Sopenharmony_ci    static I18nErrorCode SetSystemLanguage(const std::string &languageTag);
479596a2c1Sopenharmony_ci
489596a2c1Sopenharmony_ci    /**
499596a2c1Sopenharmony_ci     * @brief Set the System Region to region tag.
509596a2c1Sopenharmony_ci     *
519596a2c1Sopenharmony_ci     * @param regionTag region tag to set.
529596a2c1Sopenharmony_ci     * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
539596a2c1Sopenharmony_ci     */
549596a2c1Sopenharmony_ci    static I18nErrorCode SetSystemRegion(const std::string &regionTag);
559596a2c1Sopenharmony_ci
569596a2c1Sopenharmony_ci    /**
579596a2c1Sopenharmony_ci     * @brief Set the System Locale to locale tag.
589596a2c1Sopenharmony_ci     *
599596a2c1Sopenharmony_ci     * @param localeTag locale tag to set.
609596a2c1Sopenharmony_ci     * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
619596a2c1Sopenharmony_ci     */
629596a2c1Sopenharmony_ci    static I18nErrorCode SetSystemLocale(const std::string &localeTag);
639596a2c1Sopenharmony_ci
649596a2c1Sopenharmony_ci    /**
659596a2c1Sopenharmony_ci     * @brief Set system hour to 12 or 24-hour clock.
669596a2c1Sopenharmony_ci     *
679596a2c1Sopenharmony_ci     * @param option When flag is true, the system adopts 24-hour clock;
689596a2c1Sopenharmony_ci     * When flag is false, the system adopts 12-hour clock; When flag is empty string, the system adopts
699596a2c1Sopenharmony_ci     * system locale default behavior.
709596a2c1Sopenharmony_ci     * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
719596a2c1Sopenharmony_ci     */
729596a2c1Sopenharmony_ci    static I18nErrorCode Set24HourClock(const std::string &option);
739596a2c1Sopenharmony_ci
749596a2c1Sopenharmony_ci    /**
759596a2c1Sopenharmony_ci     * @brief Set system digit to local digit or Arbic digit.
769596a2c1Sopenharmony_ci     *
779596a2c1Sopenharmony_ci     * @param flag When flag is True, the system adopts local digit;
789596a2c1Sopenharmony_ci     * When flag is False, the system doesn't adopt local digit;
799596a2c1Sopenharmony_ci     * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
809596a2c1Sopenharmony_ci     */
819596a2c1Sopenharmony_ci    static I18nErrorCode SetUsingLocalDigit(bool flag);
829596a2c1Sopenharmony_ci    static std::string GetSystemLanguage();
839596a2c1Sopenharmony_ci    static std::string GetSystemRegion();
849596a2c1Sopenharmony_ci    static std::string GetSystemLocale();
859596a2c1Sopenharmony_ci    static void GetSystemLanguages(std::vector<std::string> &ret);
869596a2c1Sopenharmony_ci    static void GetSystemCountries(std::vector<std::string> &ret);
879596a2c1Sopenharmony_ci    static bool IsSuggested(const std::string &language);
889596a2c1Sopenharmony_ci    static bool IsSuggested(const std::string &language, const std::string &region);
899596a2c1Sopenharmony_ci    static std::string GetDisplayLanguage(const std::string &language, const std::string &displayLocale,
909596a2c1Sopenharmony_ci        bool sentenceCase);
919596a2c1Sopenharmony_ci    static std::string GetDisplayRegion(const std::string &region, const std::string &displayLocale,
929596a2c1Sopenharmony_ci        bool sentenceCase);
939596a2c1Sopenharmony_ci    static bool IsRTL(const std::string &locale);
949596a2c1Sopenharmony_ci    static std::string GetValidLocale(const std::string &localeTag);
959596a2c1Sopenharmony_ci#ifdef SUPPORT_GRAPHICS
969596a2c1Sopenharmony_ci    static bool Is24HourClock(sptr<IRemoteObject> &proxy);
979596a2c1Sopenharmony_ci#endif
989596a2c1Sopenharmony_ci    static bool IsEmpty24HourClock();
999596a2c1Sopenharmony_ci    static bool Is24HourClock();
1009596a2c1Sopenharmony_ci    static std::string GetSystemHour();
1019596a2c1Sopenharmony_ci    static bool GetUsingLocalDigit();
1029596a2c1Sopenharmony_ci    static std::unordered_set<std::string> GetBlockedLanguages();
1039596a2c1Sopenharmony_ci    static std::unordered_set<std::string> GetBlockedRegions();
1049596a2c1Sopenharmony_ci    static std::unordered_set<std::string> GetLanguageBlockedRegions();
1059596a2c1Sopenharmony_ci    static bool IsValidLanguage(const std::string &language);
1069596a2c1Sopenharmony_ci    static bool IsValidRegion(const std::string &region);
1079596a2c1Sopenharmony_ci    static bool IsValidTag(const std::string &tag);
1089596a2c1Sopenharmony_ci    static bool IsValid24HourClockValue(const std::string &tag);
1099596a2c1Sopenharmony_ci    static std::string GetLanguageKey();
1109596a2c1Sopenharmony_ci    static std::string GetLocaleKey();
1119596a2c1Sopenharmony_ci    static std::string GetHourKey();
1129596a2c1Sopenharmony_ci
1139596a2c1Sopenharmony_ciprivate:
1149596a2c1Sopenharmony_ci    static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest);
1159596a2c1Sopenharmony_ci    static void Split(const std::string &src, const std::string &sep, std::unordered_set<std::string> &dest);
1169596a2c1Sopenharmony_ci    static I18nErrorCode UpdateSystemLocale(const std::string &language);
1179596a2c1Sopenharmony_ci    static constexpr uint32_t LANGUAGE_LEN = 2;
1189596a2c1Sopenharmony_ci    static constexpr uint32_t LOCALE_ITEM_COUNT = 3;
1199596a2c1Sopenharmony_ci    static constexpr uint32_t SCRIPT_OFFSET = 2;
1209596a2c1Sopenharmony_ci    static const char *LANGUAGE_KEY;
1219596a2c1Sopenharmony_ci    static const char *LOCALE_KEY;
1229596a2c1Sopenharmony_ci    static const char *HOUR_KEY;
1239596a2c1Sopenharmony_ci    static const char *DEFAULT_LOCALE_KEY;
1249596a2c1Sopenharmony_ci    static const char *DEFAULT_LANGUAGE_KEY;
1259596a2c1Sopenharmony_ci    static const char *DEFAULT_REGION_KEY;
1269596a2c1Sopenharmony_ci    static const char *SIM_COUNTRY_CODE_KEY;
1279596a2c1Sopenharmony_ci    static const char *SUPPORTED_LOCALES_PATH;
1289596a2c1Sopenharmony_ci    static constexpr int CONFIG_LEN = 128;
1299596a2c1Sopenharmony_ci    static const char *SUPPORTED_LOCALES_NAME;
1309596a2c1Sopenharmony_ci    static const char *SUPPORTED_REGIONS_NAME;
1319596a2c1Sopenharmony_ci    static const char *WHITE_LANGUAGES_NAME;
1329596a2c1Sopenharmony_ci    static const char *FORBIDDEN_REGIONS_NAME;
1339596a2c1Sopenharmony_ci    static const char *FORBIDDEN_LANGUAGES_NAME;
1349596a2c1Sopenharmony_ci    static const char *REGIONS_LANGUAGES_PATH;
1359596a2c1Sopenharmony_ci    static const char *REGIONS_LANGUAGES_NAME;
1369596a2c1Sopenharmony_ci    static const char *SUPPORT_LOCALES_PATH;
1379596a2c1Sopenharmony_ci    static const char *SUPPORT_LOCALES_NAME;
1389596a2c1Sopenharmony_ci    static const char *DEFAULT_LOCALE;
1399596a2c1Sopenharmony_ci    static const char *supportLocalesTag;
1409596a2c1Sopenharmony_ci    static const char *LANG_PATH;
1419596a2c1Sopenharmony_ci    static const char *rootTag;
1429596a2c1Sopenharmony_ci    static const char *secondRootTag;
1439596a2c1Sopenharmony_ci    static const char *rootRegion;
1449596a2c1Sopenharmony_ci    static const char *secondRootRegion;
1459596a2c1Sopenharmony_ci    static const uint32_t ELEMENT_NUM = 2;
1469596a2c1Sopenharmony_ci
1479596a2c1Sopenharmony_ci    static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH;
1489596a2c1Sopenharmony_ci    static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME;
1499596a2c1Sopenharmony_ci
1509596a2c1Sopenharmony_ci    static const char *OVERRIDE_SUPPORTED_REGIONS_NAME;
1519596a2c1Sopenharmony_ci    static const char *OVERRIDE_SUPPORTED_REGIONS_PATH;
1529596a2c1Sopenharmony_ci    static const char *DIALECT_LANGS_PATH;
1539596a2c1Sopenharmony_ci    static const char *DIALECT_LANGS_NAME;
1549596a2c1Sopenharmony_ci    static const char *REGION_PATH;
1559596a2c1Sopenharmony_ci    static std::mutex dialectLocaleMutex;
1569596a2c1Sopenharmony_ci    static std::mutex region2DisplayNameMutex;
1579596a2c1Sopenharmony_ci    static std::mutex locale2DisplayNameMutex;
1589596a2c1Sopenharmony_ci
1599596a2c1Sopenharmony_ci    static const std::unordered_set<std::string>& GetSupportedLocales();
1609596a2c1Sopenharmony_ci    static const std::unordered_set<std::string>& GetForbiddenRegions();
1619596a2c1Sopenharmony_ci    static const std::unordered_set<std::string>& GetSupportedRegions();
1629596a2c1Sopenharmony_ci    static void GetCountriesFromSim(std::vector<std::string> &simCountries);
1639596a2c1Sopenharmony_ci    static void GetRelatedLocales(std::unordered_set<std::string> &relatedLocales,
1649596a2c1Sopenharmony_ci        const std::vector<std::string> countries);
1659596a2c1Sopenharmony_ci    static void GetListFromFile(const char *path, const char *resourceName, std::unordered_set<std::string> &ret);
1669596a2c1Sopenharmony_ci    static void LoadRegionsLanguages(std::unordered_set<std::string>& forbiddenRegions);
1679596a2c1Sopenharmony_ci    static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another);
1689596a2c1Sopenharmony_ci    static std::string GetMainLanguage(const std::string &language);
1699596a2c1Sopenharmony_ci    static std::string GetCountry(const std::string& parameter);
1709596a2c1Sopenharmony_ci    static std::string GetDisplayLanguageWithDialect(const std::string &language, const std::string &displayLocale);
1719596a2c1Sopenharmony_ci    static std::string GetDisplayOverrideRegion(const std::string &region, const std::string &displayLocale);
1729596a2c1Sopenharmony_ci    static std::string ComputeLocale(const std::string &displayLocale);
1739596a2c1Sopenharmony_ci    static void ReadLangData(const char *langDataPath);
1749596a2c1Sopenharmony_ci    static void ReadRegionData(const char *regionDataPath);
1759596a2c1Sopenharmony_ci    static void ProcessForbiddenRegions(const std::unordered_set<std::string> &forbiddenRegions);
1769596a2c1Sopenharmony_ci    static void SetSupportedDialectLocales(const char* key, const char* value);
1779596a2c1Sopenharmony_ci    static void SetRegion2DisplayName(const char* key, const char* value);
1789596a2c1Sopenharmony_ci    static void SetLocale2DisplayName(const char* key, const char* value);
1799596a2c1Sopenharmony_ci    static bool Is24HourLocale(const std::string& systemLocale);
1809596a2c1Sopenharmony_ci    static bool HasDesignator(const std::string& inFormat, const char designator);
1819596a2c1Sopenharmony_ci
1829596a2c1Sopenharmony_ci#ifdef SUPPORT_GRAPHICS
1839596a2c1Sopenharmony_ci    /**
1849596a2c1Sopenharmony_ci     * @brief Provided for updating i18n configuration include language and hour24.
1859596a2c1Sopenharmony_ci     *
1869596a2c1Sopenharmony_ci     * @param key Indicats configuration key.
1879596a2c1Sopenharmony_ci     * @param value Indicates configuration value.
1889596a2c1Sopenharmony_ci     */
1899596a2c1Sopenharmony_ci    static void UpdateConfiguration(const char *key, const std::string &value);
1909596a2c1Sopenharmony_ci
1919596a2c1Sopenharmony_ci    /**
1929596a2c1Sopenharmony_ci     * @brief Provided for public i18n event include locale-change event and time-change event.
1939596a2c1Sopenharmony_ci     *
1949596a2c1Sopenharmony_ci     * @param eventType Indicates event type.
1959596a2c1Sopenharmony_ci     * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
1969596a2c1Sopenharmony_ci     */
1979596a2c1Sopenharmony_ci    static I18nErrorCode PublishCommonEvent(const std::string &eventType);
1989596a2c1Sopenharmony_ci#endif
1999596a2c1Sopenharmony_ci
2009596a2c1Sopenharmony_ci    /**
2019596a2c1Sopenharmony_ci     * @brief Change language part of system locale to languageTag.
2029596a2c1Sopenharmony_ci     *
2039596a2c1Sopenharmony_ci     * @param languageTag Indicats language tag use to update locale.
2049596a2c1Sopenharmony_ci     * @return std::string Return locale tag.
2059596a2c1Sopenharmony_ci     */
2069596a2c1Sopenharmony_ci    static std::string UpdateLanguageOfLocale(const std::string &languageTag);
2079596a2c1Sopenharmony_ci
2089596a2c1Sopenharmony_ci    /**
2099596a2c1Sopenharmony_ci     * @brief Provided for combining language, script, region, extend param to locale.
2109596a2c1Sopenharmony_ci     *
2119596a2c1Sopenharmony_ci     * @param languageTag Indicate language part in locale.
2129596a2c1Sopenharmony_ci     * @param scriptTag Indicate script part in locale.
2139596a2c1Sopenharmony_ci     * @param regionTag Indicate region part in locale.
2149596a2c1Sopenharmony_ci     * @param extendParamTag Indicate extend param part in locale.
2159596a2c1Sopenharmony_ci     * @return std::string Return locale tag.
2169596a2c1Sopenharmony_ci     */
2179596a2c1Sopenharmony_ci    static std::string CreateLocale(const std::string &languageTag, const std::string &scriptTag,
2189596a2c1Sopenharmony_ci        const std::string &regionTag, const std::string &extendParamTag);
2199596a2c1Sopenharmony_ci
2209596a2c1Sopenharmony_ci    /**
2219596a2c1Sopenharmony_ci     * @brief Create a locale tag from region tag. Use icu to fill in missing parts.
2229596a2c1Sopenharmony_ci     *
2239596a2c1Sopenharmony_ci     * @param regionTag Indicats region tag used to create locale.
2249596a2c1Sopenharmony_ci     * @return std::string Return locale tag.
2259596a2c1Sopenharmony_ci     */
2269596a2c1Sopenharmony_ci    static std::string CreateLocaleFromRegion(const std::string &regionTag);
2279596a2c1Sopenharmony_ci
2289596a2c1Sopenharmony_ci    /**
2299596a2c1Sopenharmony_ci     * @brief Change the region part of system locale to regionTag.
2309596a2c1Sopenharmony_ci     *
2319596a2c1Sopenharmony_ci     * @param regionTag Indicates region tag used to update locale.
2329596a2c1Sopenharmony_ci     * @return std::string Return locale Tag.
2339596a2c1Sopenharmony_ci     */
2349596a2c1Sopenharmony_ci    static std::string UpdateRegionOfLocale(const std::string &regionTag);
2359596a2c1Sopenharmony_ci
2369596a2c1Sopenharmony_ci    /**
2379596a2c1Sopenharmony_ci     * @brief Change number system extend param part of locale.
2389596a2c1Sopenharmony_ci     *
2399596a2c1Sopenharmony_ci     * @param flag When flag is true, change number system extend param part to local digit of system language,
2409596a2c1Sopenharmony_ci     * when flag is false, remove number system extend param part.
2419596a2c1Sopenharmony_ci     * @return std::string Return locale Tag.
2429596a2c1Sopenharmony_ci     */
2439596a2c1Sopenharmony_ci    static std::string UpdateNumberSystemOfLocale(const std::string &localDigitTag, bool flag);
2449596a2c1Sopenharmony_ci
2459596a2c1Sopenharmony_ci    /**
2469596a2c1Sopenharmony_ci     * @brief Add local digit tag to number system extend param of locale.
2479596a2c1Sopenharmony_ci     *
2489596a2c1Sopenharmony_ci     * @param localDigitTag Indicats local digit tag which define in localDigitMap.
2499596a2c1Sopenharmony_ci     * @return std::string Return locale Tag.
2509596a2c1Sopenharmony_ci     */
2519596a2c1Sopenharmony_ci    static std::string AddLocalDigitToLocale(const std::string &localDigitTag);
2529596a2c1Sopenharmony_ci
2539596a2c1Sopenharmony_ci    /**
2549596a2c1Sopenharmony_ci     * @brief Remove local digit tag from number system extend param of locale.
2559596a2c1Sopenharmony_ci     *
2569596a2c1Sopenharmony_ci     * @param localDigitTag Indicats local digit tag which define in localDigitMap.
2579596a2c1Sopenharmony_ci     * @return std::string Return locale Tag.
2589596a2c1Sopenharmony_ci     */
2599596a2c1Sopenharmony_ci    static std::string RemoveLocalDigitFromLocale(const std::string &localDigitTag);
2609596a2c1Sopenharmony_ci    static void ExtendWhiteLanguages();
2619596a2c1Sopenharmony_ci    static std::unordered_set<std::string> supportedLocales;
2629596a2c1Sopenharmony_ci    static std::unordered_set<std::string> supportedRegions;
2639596a2c1Sopenharmony_ci    static std::unordered_set<std::string> supportLocales;
2649596a2c1Sopenharmony_ci    static std::unordered_set<std::string> dialectLang;
2659596a2c1Sopenharmony_ci    static std::unordered_set<std::string> overrideSupportedRegions;
2669596a2c1Sopenharmony_ci    static std::unordered_set<std::string> blockedLanguages;
2679596a2c1Sopenharmony_ci    static std::unordered_set<std::string> blockedRegions;
2689596a2c1Sopenharmony_ci    static std::unordered_map<std::string, std::unordered_set<std::string>> blockedLanguageRegions;
2699596a2c1Sopenharmony_ci    static std::unordered_set<std::string> whiteLanguages;
2709596a2c1Sopenharmony_ci    static std::set<std::string> extendWhiteLanguageList;
2719596a2c1Sopenharmony_ci    static std::map<std::string, std::string> supportedDialectLocales;
2729596a2c1Sopenharmony_ci    static std::unordered_map<std::string, std::string> dialectMap;
2739596a2c1Sopenharmony_ci    static std::unordered_map<std::string, std::string> localDigitMap;
2749596a2c1Sopenharmony_ci    static std::map<std::string, std::string> locale2DisplayName;
2759596a2c1Sopenharmony_ci    static std::map<std::string, std::string> region2DisplayName;
2769596a2c1Sopenharmony_ci    static std::string currentDialectLocale;
2779596a2c1Sopenharmony_ci    static std::string currentOverrideRegion;
2789596a2c1Sopenharmony_ci    static std::set<std::string> validCaTag;
2799596a2c1Sopenharmony_ci    static std::set<std::string> validCoTag;
2809596a2c1Sopenharmony_ci    static std::set<std::string> validKnTag;
2819596a2c1Sopenharmony_ci    static std::set<std::string> validKfTag;
2829596a2c1Sopenharmony_ci    static std::set<std::string> validNuTag;
2839596a2c1Sopenharmony_ci    static std::set<std::string> validHcTag;
2849596a2c1Sopenharmony_ci    static bool listsInitialized;
2859596a2c1Sopenharmony_ci    static bool InitializeLists();
2869596a2c1Sopenharmony_ci    static const char *NUMBER_SYSTEM_KEY;
2879596a2c1Sopenharmony_ci};
2889596a2c1Sopenharmony_ci} // namespace I18n
2899596a2c1Sopenharmony_ci} // namespace Global
2909596a2c1Sopenharmony_ci} // namespace OHOS
2919596a2c1Sopenharmony_ci#endif
292