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 bool IsSuggestedV15(const std::string &language); 90 static bool IsSuggestedV15(const std::string &language, const std::string ®ion); 91 static std::string GetDisplayLanguage(const std::string &language, const std::string &displayLocale, 92 bool sentenceCase); 93 static std::string GetDisplayRegion(const std::string ®ion, const std::string &displayLocale, 94 bool sentenceCase); 95 static bool IsRTL(const std::string &locale); 96 static std::string GetValidLocale(const std::string &localeTag); 97 #ifdef SUPPORT_GRAPHICS 98 static bool Is24HourClock(sptr<IRemoteObject> &proxy); 99 #endif 100 static bool IsEmpty24HourClock(); 101 static bool Is24HourClock(); 102 static std::string GetSystemHour(); 103 static bool GetUsingLocalDigit(); 104 static std::unordered_set<std::string> GetBlockedLanguages(); 105 static std::unordered_set<std::string> GetBlockedRegions(); 106 static std::unordered_set<std::string> GetLanguageBlockedRegions(); 107 static bool IsValidLanguage(const std::string &language); 108 static bool IsValidRegion(const std::string ®ion); 109 static bool IsValidTag(const std::string &tag); 110 static bool IsValid24HourClockValue(const std::string &tag); 111 static std::string GetSimplifiedLanguage(const std::string& language, int32_t &code); 112 static std::string GetSimplifiedSystemLanguage(); 113 114 private: 115 static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest); 116 static void Split(const std::string &src, const std::string &sep, std::unordered_set<std::string> &dest); 117 static I18nErrorCode UpdateSystemLocale(const std::string &language); 118 static constexpr uint32_t LANGUAGE_LEN = 2; 119 static constexpr uint32_t LOCALE_ITEM_COUNT = 3; 120 static constexpr uint32_t SCRIPT_OFFSET = 2; 121 static const char *LANGUAGE_KEY; 122 static const char *LOCALE_KEY; 123 static const char *HOUR_KEY; 124 static const char *UPGRADE_LOCALE_KEY; 125 static const char *DEFAULT_LOCALE_KEY; 126 static const char *DEFAULT_LANGUAGE_KEY; 127 static const char *DEFAULT_REGION_KEY; 128 static const char *SIM_COUNTRY_CODE_KEY; 129 static const char *SUPPORTED_LOCALES_PATH; 130 static const char *SUPPORTED_LOCALES_OLD_PATH; 131 static constexpr int CONFIG_LEN = 128; 132 static const char *SUPPORTED_LOCALES_NAME; 133 static const char *SUPPORTED_REGIONS_NAME; 134 static const char *WHITE_LANGUAGES_NAME; 135 static const char *FORBIDDEN_REGIONS_NAME; 136 static const char *FORBIDDEN_LANGUAGES_NAME; 137 static const char *REGIONS_LANGUAGES_PATH; 138 static const char *REGIONS_LANGUAGES_NAME; 139 static const char *SUPPORT_LOCALES_PATH; 140 static const char *SUPPORT_LOCALES_NAME; 141 static const char *DEFAULT_LOCALE; 142 static const char *supportLocalesTag; 143 static const char *LANG_PATH; 144 static const char *rootTag; 145 static const char *secondRootTag; 146 static const char *rootRegion; 147 static const char *secondRootRegion; 148 static const uint32_t ELEMENT_NUM = 2; 149 150 static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH; 151 static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME; 152 153 static const char *OVERRIDE_SUPPORTED_REGIONS_NAME; 154 static const char *OVERRIDE_SUPPORTED_REGIONS_PATH; 155 static const char *DIALECT_LANGS_PATH; 156 static const char *DIALECT_LANGS_NAME; 157 static const char *REGION_PATH; 158 static const std::string HOUR_EVENT_DATA; 159 static std::mutex dialectLocaleMutex; 160 static std::mutex region2DisplayNameMutex; 161 static std::mutex locale2DisplayNameMutex; 162 163 static const std::unordered_set<std::string>& GetSupportedLocales(); 164 static const std::unordered_set<std::string>& GetSupportedLocalesV15(); 165 static const std::unordered_set<std::string>& GetForbiddenRegions(); 166 static const std::unordered_set<std::string>& GetSupportedRegions(); 167 static void GetCountriesFromSim(std::vector<std::string> &simCountries); 168 static void GetRelatedLocales(std::unordered_set<std::string> &relatedLocales, 169 const std::vector<std::string> countries); 170 static void GetRelatedLocalesV15(std::unordered_set<std::string> &relatedLocales, 171 const std::vector<std::string> countries); 172 static void GetListFromFile(const char *path, const char *resourceName, std::unordered_set<std::string> &ret); 173 static void LoadRegionsLanguages(std::unordered_set<std::string>& forbiddenRegions); 174 static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another); 175 static std::string GetMainLanguage(const std::string &language, 176 std::unordered_map<std::string, std::string> selfDialectMap); 177 static std::string GetCountry(const std::string& parameter); 178 static std::string GetDisplayLanguageWithDialect(const std::string &language, const std::string &displayLocale); 179 static std::string GetDisplayOverrideRegion(const std::string ®ion, const std::string &displayLocale); 180 static std::string ComputeLocale(const std::string &displayLocale); 181 static void ReadLangData(const char *langDataPath); 182 static void ReadRegionData(const char *regionDataPath); 183 static void ProcessForbiddenRegions(const std::unordered_set<std::string> &forbiddenRegions); 184 static void SetSupportedDialectLocales(const char* key, const char* value); 185 static void SetRegion2DisplayName(const char* key, const char* value); 186 static void SetLocale2DisplayName(const char* key, const char* value); 187 static bool Is24HourLocale(const std::string& systemLocale); 188 static bool HasDesignator(const std::string& inFormat, const char designator); 189 static void QueryUpgradeLocale(); 190 191 #ifdef SUPPORT_GRAPHICS 192 /** 193 * @brief Provided for updating i18n configuration include language and hour24. 194 * 195 * @param key Indicats configuration key. 196 * @param value Indicates configuration value. 197 */ 198 static void UpdateConfiguration(const char *key, const std::string &value); 199 200 /** 201 * @brief Provided for public i18n event include locale-change event and time-change event. 202 * 203 * @param eventType Indicates event type. 204 * @return I18nErrorCode Return SUCCESS indicates that the setting was successful. 205 */ 206 static I18nErrorCode PublishCommonEvent(const std::string &eventType); 207 #endif 208 209 /** 210 * @brief Change language part of system locale to languageTag. 211 * 212 * @param languageTag Indicats language tag use to update locale. 213 * @return std::string Return locale tag. 214 */ 215 static std::string UpdateLanguageOfLocale(const std::string &languageTag); 216 217 /** 218 * @brief Provided for combining language, script, region, extend param to locale. 219 * 220 * @param languageTag Indicate language part in locale. 221 * @param scriptTag Indicate script part in locale. 222 * @param regionTag Indicate region part in locale. 223 * @param extendParamTag Indicate extend param part in locale. 224 * @return std::string Return locale tag. 225 */ 226 static std::string CreateLocale(const std::string &languageTag, const std::string &scriptTag, 227 const std::string ®ionTag, const std::string &extendParamTag); 228 229 /** 230 * @brief Create a locale tag from region tag. Use icu to fill in missing parts. 231 * 232 * @param regionTag Indicats region tag used to create locale. 233 * @return std::string Return locale tag. 234 */ 235 static std::string CreateLocaleFromRegion(const std::string ®ionTag); 236 237 /** 238 * @brief Change the region part of system locale to regionTag. 239 * 240 * @param regionTag Indicates region tag used to update locale. 241 * @return std::string Return locale Tag. 242 */ 243 static std::string UpdateRegionOfLocale(const std::string ®ionTag); 244 245 /** 246 * @brief Change number system extend param part of locale. 247 * 248 * @param flag When flag is true, change number system extend param part to local digit of system language, 249 * when flag is false, remove number system extend param part. 250 * @return std::string Return locale Tag. 251 */ 252 static std::string UpdateNumberSystemOfLocale(const std::string &localDigitTag, bool flag); 253 254 /** 255 * @brief Add local digit tag to number system extend param of locale. 256 * 257 * @param localDigitTag Indicats local digit tag which define in localDigitMap. 258 * @return std::string Return locale Tag. 259 */ 260 static std::string AddLocalDigitToLocale(const std::string &localDigitTag); 261 262 /** 263 * @brief Remove local digit tag from number system extend param of locale. 264 * 265 * @param localDigitTag Indicats local digit tag which define in localDigitMap. 266 * @return std::string Return locale Tag. 267 */ 268 static std::string RemoveLocalDigitFromLocale(const std::string &localDigitTag); 269 static void ExtendWhiteLanguages(); 270 static void ExtendLanguageWithScript(const std::string &languageStr); 271 static std::unordered_set<std::string> supportedLocales; 272 static std::unordered_set<std::string> supportedLocalesV15; 273 static std::unordered_set<std::string> supportedRegions; 274 static std::unordered_set<std::string> supportLocales; 275 static std::unordered_set<std::string> dialectLang; 276 static std::unordered_set<std::string> overrideSupportedRegions; 277 static std::unordered_set<std::string> blockedLanguages; 278 static std::unordered_set<std::string> blockedRegions; 279 static std::unordered_map<std::string, std::unordered_set<std::string>> blockedLanguageRegions; 280 static std::unordered_set<std::string> whiteLanguages; 281 static std::set<std::string> extendWhiteLanguageList; 282 static std::set<std::string> extendWhiteLanguageListV15; 283 static std::map<std::string, std::string> supportedDialectLocales; 284 static std::unordered_map<std::string, std::string> dialectMap; 285 static std::unordered_map<std::string, std::string> dialectMapV15; 286 static std::unordered_map<std::string, std::string> localDigitMap; 287 static std::map<std::string, std::string> locale2DisplayName; 288 static std::map<std::string, std::string> region2DisplayName; 289 static std::unordered_map<std::string, std::vector<std::string>> dialectLanguages; 290 static std::unordered_map<std::string, std::string> resourceIdMap; 291 static std::string currentDialectLocale; 292 static std::string currentOverrideRegion; 293 static std::set<std::string> validCaTag; 294 static std::set<std::string> validCoTag; 295 static std::set<std::string> validKnTag; 296 static std::set<std::string> validKfTag; 297 static std::set<std::string> validNuTag; 298 static std::set<std::string> validHcTag; 299 static bool listsInitialized; 300 static bool InitializeLists(); 301 static const char *NUMBER_SYSTEM_KEY; 302 }; 303 } // namespace I18n 304 } // namespace Global 305 } // namespace OHOS 306 #endif 307