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 #ifndef OHOS_GLOBAL_I18N_LOCALE_CONFIG_H 16 #define OHOS_GLOBAL_I18N_LOCALE_CONFIG_H 17 18 #include <map> 19 #include <mutex> 20 #include <vector> 21 #include <set> 22 #include <string> 23 #include <unordered_map> 24 #include <unordered_set> 25 #ifdef SUPPORT_GRAPHICS 26 #include "configuration.h" 27 #include <common_event_data.h> 28 #endif 29 #include "i18n_types.h" 30 #include "unicode/locid.h" 31 32 namespace OHOS { 33 namespace Global { 34 namespace I18n { 35 class LocaleConfig { 36 public: 37 LocaleConfig() = default; 38 virtual ~LocaleConfig() = default; 39 40 /** 41 * @brief Set the System Language to language Tag. 42 * 43 * @param languageTag language tag to set. 44 * @return I18nErrorCode Return SUCCESS indicates that the setting was successful. 45 */ 46 static I18nErrorCode SetSystemLanguage(const std::string &languageTag); 47 48 /** 49 * @brief Set the System Region to region tag. 50 * 51 * @param regionTag region tag to set. 52 * @return I18nErrorCode Return SUCCESS indicates that the setting was successful. 53 */ 54 static I18nErrorCode SetSystemRegion(const std::string ®ionTag); 55 56 /** 57 * @brief Set the System Locale to locale tag. 58 * 59 * @param localeTag locale tag to set. 60 * @return I18nErrorCode Return SUCCESS indicates that the setting was successful. 61 */ 62 static I18nErrorCode SetSystemLocale(const std::string &localeTag); 63 64 /** 65 * @brief Set system hour to 12 or 24-hour clock. 66 * 67 * @param option When flag is true, the system adopts 24-hour clock; 68 * When flag is false, the system adopts 12-hour clock; When flag is empty string, the system adopts 69 * system locale default behavior. 70 * @return I18nErrorCode Return SUCCESS indicates that the setting was successful. 71 */ 72 static I18nErrorCode Set24HourClock(const std::string &option); 73 74 /** 75 * @brief Set system digit to local digit or Arbic digit. 76 * 77 * @param flag When flag is True, the system adopts local digit; 78 * When flag is False, the system doesn't adopt local digit; 79 * @return I18nErrorCode Return SUCCESS indicates that the setting was successful. 80 */ 81 static I18nErrorCode SetUsingLocalDigit(bool flag); 82 static std::string GetSystemLanguage(); 83 static std::string GetSystemRegion(); 84 static std::string GetSystemLocale(); 85 static void GetSystemLanguages(std::vector<std::string> &ret); 86 static void GetSystemCountries(std::vector<std::string> &ret); 87 static bool IsSuggested(const std::string &language); 88 static bool IsSuggested(const std::string &language, const std::string ®ion); 89 static std::string GetDisplayLanguage(const std::string &language, const std::string &displayLocale, 90 bool sentenceCase); 91 static std::string GetDisplayRegion(const std::string ®ion, const std::string &displayLocale, 92 bool sentenceCase); 93 static bool IsRTL(const std::string &locale); 94 static std::string GetValidLocale(const std::string &localeTag); 95 #ifdef SUPPORT_GRAPHICS 96 static bool Is24HourClock(sptr<IRemoteObject> &proxy); 97 #endif 98 static bool IsEmpty24HourClock(); 99 static bool Is24HourClock(); 100 static std::string GetSystemHour(); 101 static bool GetUsingLocalDigit(); 102 static std::unordered_set<std::string> GetBlockedLanguages(); 103 static std::unordered_set<std::string> GetBlockedRegions(); 104 static std::unordered_set<std::string> GetLanguageBlockedRegions(); 105 static bool IsValidLanguage(const std::string &language); 106 static bool IsValidRegion(const std::string ®ion); 107 static bool IsValidTag(const std::string &tag); 108 static bool IsValid24HourClockValue(const std::string &tag); 109 110 private: 111 static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest); 112 static void Split(const std::string &src, const std::string &sep, std::unordered_set<std::string> &dest); 113 static I18nErrorCode UpdateSystemLocale(const std::string &language); 114 static constexpr uint32_t LANGUAGE_LEN = 2; 115 static constexpr uint32_t LOCALE_ITEM_COUNT = 3; 116 static constexpr uint32_t SCRIPT_OFFSET = 2; 117 static const char *LANGUAGE_KEY; 118 static const char *LOCALE_KEY; 119 static const char *HOUR_KEY; 120 static const char *UPGRADE_LOCALE_KEY; 121 static const char *DEFAULT_LOCALE_KEY; 122 static const char *DEFAULT_LANGUAGE_KEY; 123 static const char *DEFAULT_REGION_KEY; 124 static const char *SIM_COUNTRY_CODE_KEY; 125 static const char *SUPPORTED_LOCALES_PATH; 126 static constexpr int CONFIG_LEN = 128; 127 static const char *SUPPORTED_LOCALES_NAME; 128 static const char *SUPPORTED_REGIONS_NAME; 129 static const char *WHITE_LANGUAGES_NAME; 130 static const char *FORBIDDEN_REGIONS_NAME; 131 static const char *FORBIDDEN_LANGUAGES_NAME; 132 static const char *REGIONS_LANGUAGES_PATH; 133 static const char *REGIONS_LANGUAGES_NAME; 134 static const char *SUPPORT_LOCALES_PATH; 135 static const char *SUPPORT_LOCALES_NAME; 136 static const char *DEFAULT_LOCALE; 137 static const char *supportLocalesTag; 138 static const char *LANG_PATH; 139 static const char *rootTag; 140 static const char *secondRootTag; 141 static const char *rootRegion; 142 static const char *secondRootRegion; 143 static const uint32_t ELEMENT_NUM = 2; 144 145 static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH; 146 static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME; 147 148 static const char *OVERRIDE_SUPPORTED_REGIONS_NAME; 149 static const char *OVERRIDE_SUPPORTED_REGIONS_PATH; 150 static const char *DIALECT_LANGS_PATH; 151 static const char *DIALECT_LANGS_NAME; 152 static const char *REGION_PATH; 153 static const std::string HOUR_EVENT_DATA; 154 static std::mutex dialectLocaleMutex; 155 static std::mutex region2DisplayNameMutex; 156 static std::mutex locale2DisplayNameMutex; 157 158 static const std::unordered_set<std::string>& GetSupportedLocales(); 159 static const std::unordered_set<std::string>& GetForbiddenRegions(); 160 static const std::unordered_set<std::string>& GetSupportedRegions(); 161 static void GetCountriesFromSim(std::vector<std::string> &simCountries); 162 static void GetRelatedLocales(std::unordered_set<std::string> &relatedLocales, 163 const std::vector<std::string> countries); 164 static void GetListFromFile(const char *path, const char *resourceName, std::unordered_set<std::string> &ret); 165 static void LoadRegionsLanguages(std::unordered_set<std::string>& forbiddenRegions); 166 static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another); 167 static std::string GetMainLanguage(const std::string &language); 168 static std::string GetCountry(const std::string& parameter); 169 static std::string GetDisplayLanguageWithDialect(const std::string &language, const std::string &displayLocale); 170 static std::string GetDisplayOverrideRegion(const std::string ®ion, const std::string &displayLocale); 171 static std::string ComputeLocale(const std::string &displayLocale); 172 static void ReadLangData(const char *langDataPath); 173 static void ReadRegionData(const char *regionDataPath); 174 static void ProcessForbiddenRegions(const std::unordered_set<std::string> &forbiddenRegions); 175 static void SetSupportedDialectLocales(const char* key, const char* value); 176 static void SetRegion2DisplayName(const char* key, const char* value); 177 static void SetLocale2DisplayName(const char* key, const char* value); 178 static bool Is24HourLocale(const std::string& systemLocale); 179 static bool HasDesignator(const std::string& inFormat, const char designator); 180 static void QueryUpgradeLocale(); 181 182 #ifdef SUPPORT_GRAPHICS 183 /** 184 * @brief Provided for updating i18n configuration include language and hour24. 185 * 186 * @param key Indicats configuration key. 187 * @param value Indicates configuration value. 188 */ 189 static void UpdateConfiguration(const char *key, const std::string &value); 190 191 /** 192 * @brief Provided for public i18n event include locale-change event and time-change event. 193 * 194 * @param eventType Indicates event type. 195 * @return I18nErrorCode Return SUCCESS indicates that the setting was successful. 196 */ 197 static I18nErrorCode PublishCommonEvent(const std::string &eventType); 198 #endif 199 200 /** 201 * @brief Change language part of system locale to languageTag. 202 * 203 * @param languageTag Indicats language tag use to update locale. 204 * @return std::string Return locale tag. 205 */ 206 static std::string UpdateLanguageOfLocale(const std::string &languageTag); 207 208 /** 209 * @brief Provided for combining language, script, region, extend param to locale. 210 * 211 * @param languageTag Indicate language part in locale. 212 * @param scriptTag Indicate script part in locale. 213 * @param regionTag Indicate region part in locale. 214 * @param extendParamTag Indicate extend param part in locale. 215 * @return std::string Return locale tag. 216 */ 217 static std::string CreateLocale(const std::string &languageTag, const std::string &scriptTag, 218 const std::string ®ionTag, const std::string &extendParamTag); 219 220 /** 221 * @brief Create a locale tag from region tag. Use icu to fill in missing parts. 222 * 223 * @param regionTag Indicats region tag used to create locale. 224 * @return std::string Return locale tag. 225 */ 226 static std::string CreateLocaleFromRegion(const std::string ®ionTag); 227 228 /** 229 * @brief Change the region part of system locale to regionTag. 230 * 231 * @param regionTag Indicates region tag used to update locale. 232 * @return std::string Return locale Tag. 233 */ 234 static std::string UpdateRegionOfLocale(const std::string ®ionTag); 235 236 /** 237 * @brief Change number system extend param part of locale. 238 * 239 * @param flag When flag is true, change number system extend param part to local digit of system language, 240 * when flag is false, remove number system extend param part. 241 * @return std::string Return locale Tag. 242 */ 243 static std::string UpdateNumberSystemOfLocale(const std::string &localDigitTag, bool flag); 244 245 /** 246 * @brief Add local digit tag to number system extend param of locale. 247 * 248 * @param localDigitTag Indicats local digit tag which define in localDigitMap. 249 * @return std::string Return locale Tag. 250 */ 251 static std::string AddLocalDigitToLocale(const std::string &localDigitTag); 252 253 /** 254 * @brief Remove local digit tag from number system extend param of locale. 255 * 256 * @param localDigitTag Indicats local digit tag which define in localDigitMap. 257 * @return std::string Return locale Tag. 258 */ 259 static std::string RemoveLocalDigitFromLocale(const std::string &localDigitTag); 260 static void ExtendWhiteLanguages(); 261 static std::unordered_set<std::string> supportedLocales; 262 static std::unordered_set<std::string> supportedRegions; 263 static std::unordered_set<std::string> supportLocales; 264 static std::unordered_set<std::string> dialectLang; 265 static std::unordered_set<std::string> overrideSupportedRegions; 266 static std::unordered_set<std::string> blockedLanguages; 267 static std::unordered_set<std::string> blockedRegions; 268 static std::unordered_map<std::string, std::unordered_set<std::string>> blockedLanguageRegions; 269 static std::unordered_set<std::string> whiteLanguages; 270 static std::set<std::string> extendWhiteLanguageList; 271 static std::map<std::string, std::string> supportedDialectLocales; 272 static std::unordered_map<std::string, std::string> dialectMap; 273 static std::unordered_map<std::string, std::string> localDigitMap; 274 static std::map<std::string, std::string> locale2DisplayName; 275 static std::map<std::string, std::string> region2DisplayName; 276 static std::string currentDialectLocale; 277 static std::string currentOverrideRegion; 278 static std::set<std::string> validCaTag; 279 static std::set<std::string> validCoTag; 280 static std::set<std::string> validKnTag; 281 static std::set<std::string> validKfTag; 282 static std::set<std::string> validNuTag; 283 static std::set<std::string> validHcTag; 284 static bool listsInitialized; 285 static bool InitializeLists(); 286 static const char *NUMBER_SYSTEM_KEY; 287 }; 288 } // namespace I18n 289 } // namespace Global 290 } // namespace OHOS 291 #endif 292