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