1 /* 2 * Copyright (c) 2023 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 OHOS_GLOBAL_SYSTEM_LOCALE_MANAGER_H 17 #define OHOS_GLOBAL_SYSTEM_LOCALE_MANAGER_H 18 19 #include <string> 20 #include <unordered_set> 21 #include <vector> 22 #include "collator.h" 23 #include "i18n_types.h" 24 #include "taboo_utils.h" 25 26 namespace OHOS { 27 namespace Global { 28 namespace I18n { 29 enum SuggestionType { 30 // language or region is not suggested with system region or system language. 31 SUGGESTION_TYPE_NONE = 0, 32 33 // language is suggested with sim card region. 34 SUGGESTION_TYPE_SIM = 1, 35 36 // language is suggested with system region or region is suggested with system language. 37 SUGGESTION_TYPE_RELATED = 2, 38 }; 39 40 struct LocaleItem { 41 // language or region code. 42 std::string id; 43 44 // the suggestion type of language or region. 45 SuggestionType suggestionType; 46 47 // the name of language or region in specified language. 48 std::string displayName; 49 50 // the name of language or region in local language. 51 std::string localName; 52 }; 53 54 struct SortOptions { 55 // locale use to sort language or region. 56 std::string localeTag; 57 58 // whether to use local name to sort language or region. 59 bool isUseLocalName; 60 61 // whether to put the suggested item at the top 62 bool isSuggestedFirst; 63 }; 64 65 struct TimeZoneCityItem { 66 // time zone code 67 std::string zoneId; 68 69 //city code 70 std::string cityId; 71 72 //the display name of city 73 std::string cityDisplayName; 74 75 //the timezone offset for the city 76 int32_t offset; 77 78 //the display name of the timezone of the city 79 std::string zoneDisplayName; 80 81 //the timezone raw offset for the city 82 int32_t rawOffset; 83 }; 84 85 class SystemLocaleManager { 86 public: 87 SystemLocaleManager(); 88 ~SystemLocaleManager(); 89 90 /** 91 * @brief sort input languages according to SortOptions and obtain sorted result. 92 * 93 * @param languages input language array to be sorted. 94 * @param options sort options. 95 * @return std::vector<LocaleItem> sort result. 96 */ 97 std::vector<LocaleItem> GetLanguageInfoArray(const std::vector<std::string> &languages, 98 const SortOptions &options, I18nErrorCode &status); 99 100 /** 101 * @brief sort input countries according to SortOptions and obtain sorted result. 102 * 103 * @param regions input country array to be sorted. 104 * @param options sort options. 105 * @return std::vector<LocaleItem> sort result. 106 */ 107 std::vector<LocaleItem> GetCountryInfoArray(const std::vector<std::string> &countries, const SortOptions &options, 108 I18nErrorCode &status); 109 110 /** 111 * @brief obtains sorted time zone city array. 112 * 113 * @return std::vector<TimeZoneCityItem> sort result. 114 */ 115 static std::vector<TimeZoneCityItem> GetTimezoneCityInfoArray(I18nErrorCode &status); 116 117 private: 118 void SortLocaleItemList(std::vector<LocaleItem> &localeItemList, const SortOptions &options); 119 static void SortTimezoneCityItemList(const std::string &locale, 120 std::vector<TimeZoneCityItem> &timezoneCityItemList); 121 static std::vector<TimeZoneCityItem> GetTimezoneCityInfoArray(); 122 std::unordered_set<std::string> GetCountryCodeFromSimCard(); 123 bool IsLanguageSimRegionSuggest(const std::unordered_set<std::string> &simRegions, 124 const std::string &language); 125 void GetCountryCodeFromSimCardInner(std::unordered_set<std::string> &resultSet, size_t soltId); 126 bool IsSuggestRegion(const std::string &language, const std::string ®ion); 127 LocaleItem GetLocaleItem(const std::string ®ion, SuggestionType &suggestionType, 128 const std::string &localeTag); 129 static std::string GetCityDisplayNameWithTaboo(const std::string &cityId, const std::string &localeBaseName, 130 const std::string &cityDisplayName); 131 static const std::unordered_set<std::string> supportLocales; 132 static std::unordered_map<std::string, std::string> custLanguageMap; 133 static constexpr int CONFIG_LEN = 128; 134 }; 135 } // namespace I18n 136 } // namespace Global 137 } // namespace OHOS 138 #endif