• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 &regionTag);
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 &region);
89     static std::string GetDisplayLanguage(const std::string &language, const std::string &displayLocale,
90         bool sentenceCase);
91     static std::string GetDisplayRegion(const std::string &region, 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 &region);
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 *DEFAULT_LOCALE_KEY;
121     static const char *DEFAULT_LANGUAGE_KEY;
122     static const char *DEFAULT_REGION_KEY;
123     static const char *SIM_COUNTRY_CODE_KEY;
124     static const char *SUPPORTED_LOCALES_PATH;
125     static constexpr int CONFIG_LEN = 128;
126     static const char *SUPPORTED_LOCALES_NAME;
127     static const char *SUPPORTED_REGIONS_NAME;
128     static const char *WHITE_LANGUAGES_NAME;
129     static const char *FORBIDDEN_REGIONS_NAME;
130     static const char *FORBIDDEN_LANGUAGES_NAME;
131     static const char *REGIONS_LANGUAGES_PATH;
132     static const char *REGIONS_LANGUAGES_NAME;
133     static const char *SUPPORT_LOCALES_PATH;
134     static const char *SUPPORT_LOCALES_NAME;
135     static const char *DEFAULT_LOCALE;
136     static const char *supportLocalesTag;
137     static const char *LANG_PATH;
138     static const char *rootTag;
139     static const char *secondRootTag;
140     static const char *rootRegion;
141     static const char *secondRootRegion;
142     static const uint32_t ELEMENT_NUM = 2;
143 
144     static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH;
145     static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME;
146 
147     static const char *OVERRIDE_SUPPORTED_REGIONS_NAME;
148     static const char *OVERRIDE_SUPPORTED_REGIONS_PATH;
149     static const char *DIALECT_LANGS_PATH;
150     static const char *DIALECT_LANGS_NAME;
151     static const char *REGION_PATH;
152     static const std::string HOUR_EVENT_DATA;
153     static std::mutex dialectLocaleMutex;
154     static std::mutex region2DisplayNameMutex;
155     static std::mutex locale2DisplayNameMutex;
156 
157     static const std::unordered_set<std::string>& GetSupportedLocales();
158     static const std::unordered_set<std::string>& GetForbiddenRegions();
159     static const std::unordered_set<std::string>& GetSupportedRegions();
160     static void GetCountriesFromSim(std::vector<std::string> &simCountries);
161     static void GetRelatedLocales(std::unordered_set<std::string> &relatedLocales,
162         const std::vector<std::string> countries);
163     static void GetListFromFile(const char *path, const char *resourceName, std::unordered_set<std::string> &ret);
164     static void LoadRegionsLanguages(std::unordered_set<std::string>& forbiddenRegions);
165     static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another);
166     static std::string GetMainLanguage(const std::string &language);
167     static std::string GetCountry(const std::string& parameter);
168     static std::string GetDisplayLanguageWithDialect(const std::string &language, const std::string &displayLocale);
169     static std::string GetDisplayOverrideRegion(const std::string &region, const std::string &displayLocale);
170     static std::string ComputeLocale(const std::string &displayLocale);
171     static void ReadLangData(const char *langDataPath);
172     static void ReadRegionData(const char *regionDataPath);
173     static void ProcessForbiddenRegions(const std::unordered_set<std::string> &forbiddenRegions);
174     static void SetSupportedDialectLocales(const char* key, const char* value);
175     static void SetRegion2DisplayName(const char* key, const char* value);
176     static void SetLocale2DisplayName(const char* key, const char* value);
177     static bool Is24HourLocale(const std::string& systemLocale);
178     static bool HasDesignator(const std::string& inFormat, const char designator);
179 
180 #ifdef SUPPORT_GRAPHICS
181     /**
182      * @brief Provided for updating i18n configuration include language and hour24.
183      *
184      * @param key Indicats configuration key.
185      * @param value Indicates configuration value.
186      */
187     static void UpdateConfiguration(const char *key, const std::string &value);
188 
189     /**
190      * @brief Provided for public i18n event include locale-change event and time-change event.
191      *
192      * @param eventType Indicates event type.
193      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
194      */
195     static I18nErrorCode PublishCommonEvent(const std::string &eventType);
196 #endif
197 
198     /**
199      * @brief Change language part of system locale to languageTag.
200      *
201      * @param languageTag Indicats language tag use to update locale.
202      * @return std::string Return locale tag.
203      */
204     static std::string UpdateLanguageOfLocale(const std::string &languageTag);
205 
206     /**
207      * @brief Provided for combining language, script, region, extend param to locale.
208      *
209      * @param languageTag Indicate language part in locale.
210      * @param scriptTag Indicate script part in locale.
211      * @param regionTag Indicate region part in locale.
212      * @param extendParamTag Indicate extend param part in locale.
213      * @return std::string Return locale tag.
214      */
215     static std::string CreateLocale(const std::string &languageTag, const std::string &scriptTag,
216         const std::string &regionTag, const std::string &extendParamTag);
217 
218     /**
219      * @brief Create a locale tag from region tag. Use icu to fill in missing parts.
220      *
221      * @param regionTag Indicats region tag used to create locale.
222      * @return std::string Return locale tag.
223      */
224     static std::string CreateLocaleFromRegion(const std::string &regionTag);
225 
226     /**
227      * @brief Change the region part of system locale to regionTag.
228      *
229      * @param regionTag Indicates region tag used to update locale.
230      * @return std::string Return locale Tag.
231      */
232     static std::string UpdateRegionOfLocale(const std::string &regionTag);
233 
234     /**
235      * @brief Change number system extend param part of locale.
236      *
237      * @param flag When flag is true, change number system extend param part to local digit of system language,
238      * when flag is false, remove number system extend param part.
239      * @return std::string Return locale Tag.
240      */
241     static std::string UpdateNumberSystemOfLocale(const std::string &localDigitTag, bool flag);
242 
243     /**
244      * @brief Add local digit tag to number system extend param of locale.
245      *
246      * @param localDigitTag Indicats local digit tag which define in localDigitMap.
247      * @return std::string Return locale Tag.
248      */
249     static std::string AddLocalDigitToLocale(const std::string &localDigitTag);
250 
251     /**
252      * @brief Remove local digit tag from number system extend param of locale.
253      *
254      * @param localDigitTag Indicats local digit tag which define in localDigitMap.
255      * @return std::string Return locale Tag.
256      */
257     static std::string RemoveLocalDigitFromLocale(const std::string &localDigitTag);
258     static void ExtendWhiteLanguages();
259     static std::unordered_set<std::string> supportedLocales;
260     static std::unordered_set<std::string> supportedRegions;
261     static std::unordered_set<std::string> supportLocales;
262     static std::unordered_set<std::string> dialectLang;
263     static std::unordered_set<std::string> overrideSupportedRegions;
264     static std::unordered_set<std::string> blockedLanguages;
265     static std::unordered_set<std::string> blockedRegions;
266     static std::unordered_map<std::string, std::unordered_set<std::string>> blockedLanguageRegions;
267     static std::unordered_set<std::string> whiteLanguages;
268     static std::set<std::string> extendWhiteLanguageList;
269     static std::map<std::string, std::string> supportedDialectLocales;
270     static std::unordered_map<std::string, std::string> dialectMap;
271     static std::unordered_map<std::string, std::string> localDigitMap;
272     static std::map<std::string, std::string> locale2DisplayName;
273     static std::map<std::string, std::string> region2DisplayName;
274     static std::string currentDialectLocale;
275     static std::string currentOverrideRegion;
276     static std::set<std::string> validCaTag;
277     static std::set<std::string> validCoTag;
278     static std::set<std::string> validKnTag;
279     static std::set<std::string> validKfTag;
280     static std::set<std::string> validNuTag;
281     static std::set<std::string> validHcTag;
282     static bool listsInitialized;
283     static bool InitializeLists();
284     static const char *NUMBER_SYSTEM_KEY;
285 };
286 } // namespace I18n
287 } // namespace Global
288 } // namespace OHOS
289 #endif
290