• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 #include "i18n_types.h"
26 #include "taboo_utils.h"
27 #include "unicode/calendar.h"
28 #include "locale_info.h"
29 #include "unicode/locid.h"
30 #include "unicode/utypes.h"
31 
32 namespace OHOS {
33 
34 #ifdef SUPPORT_GRAPHICS
35 template <typename T>
36 class sptr;
37 class IRemoteObject;
38 #endif
39 
40 namespace Global {
41 namespace I18n {
42 class LocaleConfig {
43 public:
44     LocaleConfig() = default;
45     virtual ~LocaleConfig() = default;
46 
47     /**
48      * @brief Set the System Language to language Tag.
49      *
50      * @param languageTag language tag to set.
51      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
52      */
53     static I18nErrorCode SetSystemLanguage(const std::string &languageTag, int32_t userId = -1);
54 
55     /**
56      * @brief Set the System Region to region tag.
57      *
58      * @param regionTag region tag to set.
59      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
60      */
61     static I18nErrorCode SetSystemRegion(const std::string &regionTag, int32_t userId = -1);
62 
63     /**
64      * @brief Set the System Locale to locale tag.
65      *
66      * @param localeTag locale tag to set.
67      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
68      */
69     static I18nErrorCode SetSystemLocale(const std::string &localeTag, int32_t userId = -1);
70 
71     /**
72      * @brief Set system hour to 12 or 24-hour clock.
73      *
74      * @param option When flag is true, the system adopts 24-hour clock;
75      * When flag is false, the system adopts 12-hour clock; When flag is empty string, the system adopts
76      * system locale default behavior.
77      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
78      */
79     static I18nErrorCode Set24HourClock(const std::string &option, int32_t userId = -1);
80 
81     /**
82      * @brief Set system digit to local digit or Arbic digit.
83      *
84      * @param flag When flag is True, the system adopts local digit;
85      * When flag is False, the system doesn't adopt local digit;
86      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
87      */
88     static I18nErrorCode SetUsingLocalDigit(bool flag, int32_t userId = -1);
89 
90     /**
91      * @brief Set user's preferrence for temperature type.
92      *
93      * @param type temperature type to set.
94      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
95      */
96     static I18nErrorCode SetTemperatureType(const TemperatureType& type, int32_t userId = -1);
97 
98     /**
99      * @brief Set user's preferrence for first day of week.
100      *
101      * @param type one of week days.
102      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
103      */
104     static I18nErrorCode SetFirstDayOfWeek(const WeekDay& type, int32_t userId = -1);
105 
106     static TemperatureType GetTemperatureType();
107     static std::string GetTemperatureName(const TemperatureType& type);
108     static WeekDay GetFirstDayOfWeek();
109     static std::string GetEffectiveLanguage();
110     static std::string GetSystemLanguage();
111     static std::string GetSystemRegion();
112     static std::string GetSystemLocale();
113     static std::string GetSystemTimezone();
114     static std::unordered_set<std::string> GetSystemLanguages();
115     static std::unordered_set<std::string> GetSystemCountries(const std::string& language = "");
116     static bool IsSuggested(const std::string &language);
117     static bool IsSuggested(const std::string &language, const std::string &region);
118     static bool IsSuggestedV15(const std::string &language);
119     static bool IsSuggestedV15(const std::string &language, const std::string &region);
120     static std::string GetDisplayLanguage(const std::string &language, const std::string &displayLocale,
121         bool sentenceCase);
122     static std::string GetDisplayRegion(const std::string &region, const std::string &displayLocale,
123         bool sentenceCase);
124     static bool IsRTL(const std::string &locale);
125     static std::string GetValidLocale(const std::string &localeTag);
126 #ifdef SUPPORT_GRAPHICS
127     static bool Is24HourClock(sptr<IRemoteObject> &proxy);
128 #endif
129     static bool IsEmpty24HourClock();
130     static bool Is24HourClock();
131     static std::string GetSystemHour();
132     static bool GetUsingLocalDigit();
133     static std::unordered_set<std::string> GetBlockedLanguages();
134     static std::unordered_set<std::string> GetBlockedRegions(const std::string& language = "");
135     static bool IsValidLanguage(const std::string &language);
136     static bool IsValidRegion(const std::string &region);
137     static bool IsValidTag(const std::string &tag);
138     static bool IsValid24HourClockValue(const std::string &tag);
139     static std::string GetSimplifiedLanguage(const std::string& language, int32_t &code);
140     static std::string GetSimplifiedSystemLanguage();
141     static std::string GetUnicodeWrappedFilePath(const std::string &path, const char delimiter,
142         std::shared_ptr<LocaleInfo> localeInfo, std::string &invalidField);
143     static const std::string HOUR_EVENT_DATA;
144     static const std::string LANGUAGE_KEY;
145     static const std::string LOCALE_KEY;
146     static const std::string HOUR_KEY;
147     static const char *DEFAULT_REGION_KEY;
148     static constexpr int CONFIG_LEN = 128;
149 
150 private:
151     static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest);
152     static void Split(const std::string &src, const std::string &sep, std::unordered_set<std::string> &dest);
153     static I18nErrorCode UpdateSystemLocale(const std::string &language);
154     static std::string ComputeEffectiveLanguage(const std::string& locale, const std::string& language);
155     static constexpr uint32_t LANGUAGE_LEN = 2;
156     static constexpr uint32_t LOCALE_ITEM_COUNT = 3;
157     static constexpr uint32_t SCRIPT_OFFSET = 2;
158     static const char *UPGRADE_LOCALE_KEY;
159     static const char *DEFAULT_LOCALE_KEY;
160     static const char *DEFAULT_LANGUAGE_KEY;
161     static const char *SIM_COUNTRY_CODE_KEY;
162     static const char *SUPPORTED_LOCALES_PATH;
163     static const char *SUPPORTED_LOCALES_OLD_PATH;
164     static const char *SUPPORTED_LOCALES_NAME;
165     static const char *SUPPORTED_REGIONS_NAME;
166     static const char *WHITE_LANGUAGES_NAME;
167     static const char *REGIONS_LANGUAGES_PATH;
168     static const char *REGIONS_LANGUAGES_NAME;
169     static const char *SUPPORT_LOCALES_PATH;
170     static const char *SUPPORT_LOCALES_NAME;
171     static const char *DEFAULT_LOCALE;
172     static const char *supportLocalesTag;
173     static const char *LANG_PATH;
174     static const char *rootTag;
175     static const char *secondRootTag;
176     static const char *rootRegion;
177     static const char *secondRootRegion;
178     static const uint32_t ELEMENT_NUM = 2;
179 
180     static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH;
181     static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME;
182 
183     static const char *OVERRIDE_SUPPORTED_REGIONS_NAME;
184     static const char *OVERRIDE_SUPPORTED_REGIONS_PATH;
185     static const char *DIALECT_LANGS_PATH;
186     static const char *DIALECT_LANGS_NAME;
187     static const char *REGION_PATH;
188     static const std::string TIMEZONE_KEY;
189     static const std::string DEFAULT_TIMEZONE;
190     static std::mutex dialectLocaleMutex;
191     static std::mutex region2DisplayNameMutex;
192     static std::mutex locale2DisplayNameMutex;
193     static const std::string EXT_PARAM_KEY;
194     static const std::string NUMBER_SYSTEM_KEY;
195     static const std::string TEMPERATURE_UNIT_KEY;
196     static const std::string WEEK_DAY_KEY;
197 
198     static const std::unordered_set<std::string>& GetSupportedLocales();
199     static const std::unordered_set<std::string>& GetSupportedLocalesV15();
200     static const std::unordered_set<std::string>& GetSupportedRegions();
201     static void GetCountriesFromSim(std::vector<std::string> &simCountries);
202     static void GetRelatedLocales(std::unordered_set<std::string> &relatedLocales,
203         const std::vector<std::string> countries);
204     static void GetRelatedLocalesV15(std::unordered_set<std::string> &relatedLocales,
205         const std::vector<std::string> countries);
206     static void GetListFromFile(const char *path, const char *resourceName, std::unordered_set<std::string> &ret);
207     static void LoadRegionsLanguages();
208     static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another);
209     static std::string GetMainLanguage(const std::string &language,
210         std::unordered_map<std::string, std::string> selfDialectMap);
211     static std::string GetCountry(const std::string& parameter);
212     static std::string GetDisplayLanguageWithDialect(const std::string &language, const std::string &displayLocale);
213     static std::string GetDisplayOverrideRegion(const std::string &region, const std::string &displayLocale);
214     static std::string ComputeLocale(const std::string &displayLocale);
215     static void ReadLangData(const char *langDataPath);
216     static void ReadRegionData(const char *regionDataPath);
217     static void SetSupportedDialectLocales(const char* key, const char* value);
218     static void SetRegion2DisplayName(const char* key, const char* value);
219     static void SetLocale2DisplayName(const char* key, const char* value);
220     static bool Is24HourLocale(const std::string& systemLocale);
221     static bool HasDesignator(const std::string& inFormat, const char designator);
222     static void QueryUpgradeLocale();
223 
224 #ifdef SUPPORT_GRAPHICS
225     /**
226      * @brief Provided for updating i18n configuration include language and hour24.
227      *
228      * @param key Indicats configuration key.
229      * @param value Indicates configuration value.
230      * @param userId Indicats update user id.
231      * @param isSwitchUser Indicates whether user switch.
232      */
233     static void UpdateConfiguration(const char *key, const std::string &value, int32_t userId);
234 
235     /**
236      * @brief Provided for public i18n event include locale-change event and time-change event.
237      *
238      * @param eventType Indicates event type.
239      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
240      */
241     static I18nErrorCode PublishCommonEvent(const std::string &eventType, int32_t userId);
242 #endif
243 
244     /**
245      * @brief Change language part of system locale to languageTag.
246      *
247      * @param languageTag Indicats language tag use to update locale.
248      * @return std::string Return locale tag.
249      */
250     static std::string UpdateLanguageOfLocale(const std::string &languageTag);
251 
252     /**
253      * @brief Provided for combining language, script, region, extend param to locale.
254      *
255      * @param languageTag Indicate language part in locale.
256      * @param scriptTag Indicate script part in locale.
257      * @param regionTag Indicate region part in locale.
258      * @param extendParamTag Indicate extend param part in locale.
259      * @return std::string Return locale tag.
260      */
261     static std::string CreateLocale(const std::string &languageTag, const std::string &scriptTag,
262         const std::string &regionTag, const std::string &extendParamTag);
263 
264     /**
265      * @brief Create a locale tag from region tag. Use icu to fill in missing parts.
266      *
267      * @param regionTag Indicats region tag used to create locale.
268      * @return std::string Return locale tag.
269      */
270     static std::string CreateLocaleFromRegion(const std::string &regionTag);
271 
272     /**
273      * @brief Change the region part of system locale to regionTag.
274      *
275      * @param regionTag Indicates region tag used to update locale.
276      * @return std::string Return locale Tag.
277      */
278     static std::string UpdateRegionOfLocale(const std::string &regionTag);
279     static std::string ModifyExtParam(const std::string& locale, const std::string& key, const std::string& value);;
280     static std::unordered_map<std::string, std::string> ParseExtParam(const std::string& param);
281     static std::string GenerateExtParam(const std::unordered_map<std::string, std::string>& paramMap);
282     static void ExtendWhiteLanguages();
283     static void ExtendLanguageWithScript(const std::string &languageStr);
284     static __attribute__((destructor)) void CleanupXmlResources();
285     static std::unordered_set<std::string> supportedLocales;
286     static std::unordered_set<std::string> supportedLocalesV15;
287     static std::unordered_set<std::string> supportedRegions;
288     static std::unordered_set<std::string> supportLocales;
289     static std::unordered_set<std::string> dialectLang;
290     static std::unordered_set<std::string> overrideSupportedRegions;
291     static std::unordered_set<std::string> whiteLanguages;
292     static std::set<std::string> extendWhiteLanguageList;
293     static std::set<std::string> extendWhiteLanguageListV15;
294     static std::map<std::string, std::string> supportedDialectLocales;
295     static std::unordered_map<std::string, std::string> dialectMap;
296     static std::unordered_map<std::string, std::string> dialectMapV15;
297     static std::unordered_map<std::string, std::string> localDigitMap;
298     static std::unordered_map<TemperatureType, std::string> temperatureTypeToName;
299     static std::unordered_map<std::string, TemperatureType> nameToTemperatureType;
300     static std::unordered_set<std::string> fahrenheitUsingRegions;
301     static std::unordered_map<WeekDay, std::string> weekDayToName;
302     static std::unordered_map<std::string, WeekDay> nameToWeekDay;
303     static std::unordered_map<icu::Calendar::EDaysOfWeek, WeekDay> eDaysOfWeekToWeekDay;
304     static std::map<std::string, std::string> locale2DisplayName;
305     static std::map<std::string, std::string> region2DisplayName;
306     static std::unordered_map<std::string, std::vector<std::string>> dialectLanguages;
307     static std::unordered_map<std::string, std::string> resourceIdMap;
308     static std::string currentDialectLocale;
309     static std::string currentOverrideRegion;
310     static std::set<std::string> validCaTag;
311     static std::set<std::string> validCoTag;
312     static std::set<std::string> validKnTag;
313     static std::set<std::string> validKfTag;
314     static std::set<std::string> validNuTag;
315     static std::set<std::string> validHcTag;
316     static bool listsInitialized;
317     static bool InitializeLists();
318 };
319 } // namespace I18n
320 } // namespace Global
321 } // namespace OHOS
322 #endif
323