• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 <shared_mutex>
23 #include <string>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include "i18n_types.h"
27 #include "taboo_utils.h"
28 
29 namespace OHOS {
30 namespace Global {
31 namespace I18n {
32 class LocaleInfo;
33 class IntlLocale;
34 
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, int32_t userId = -1);
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, int32_t userId = -1);
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, int32_t userId = -1);
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, int32_t userId = -1);
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, int32_t userId = -1);
82 
83     /**
84      * @brief Set user's preferrence for temperature type.
85      *
86      * @param type temperature type to set.
87      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
88      */
89     static I18nErrorCode SetTemperatureType(const TemperatureType& type, int32_t userId = -1);
90 
91     /**
92      * @brief Set user's preferrence for first day of week.
93      *
94      * @param type one of week days.
95      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
96      */
97     static I18nErrorCode SetFirstDayOfWeek(const WeekDay& type, int32_t userId = -1);
98 
99     /**
100      * @brief Gets collations supported by system locale.
101      *
102      * @param systemCollations The map will containing the collation's identifier and name.
103      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
104      */
105     static I18nErrorCode GetSystemCollations(std::unordered_map<std::string, std::string>& systemCollations);
106 
107     /**
108      * @brief Gets collation currently used by system locale.
109      *
110      * @param usingCollation The identifier of the collation model used by system locale will be return.
111      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
112      */
113     static I18nErrorCode GetUsingCollation(std::string& usingCollation);
114 
115     /**
116      * @brief Sets the system collation mode.
117      *
118      * @param identifier Identifier of the collation mode.
119      * @param userId Indicates update user id.
120      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
121      */
122     static I18nErrorCode SetSystemCollation(const std::string& identifier, int32_t userId = -1);
123 
124     /**
125      * @brief Gets numbering systems supported by system locale.
126      *
127      * @param systemNumberingSystems a map will containing the numbering system's identifier and sample.
128      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
129      */
130     static I18nErrorCode GetSystemNumberingSystems(
131         std::unordered_map<std::string, std::string>& systemNumberingSystems);
132 
133     /**
134      * @brief Gets numbering system currently used by system locale.
135      *
136      * @param usingNumberingSystem the numbering systems's identifier
137      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
138      */
139     static I18nErrorCode GetUsingNumberingSystem(std::string& usingNumberingSystem);
140 
141     /**
142      * @brief Sets the numbering system used by the system locale.
143      *
144      * @param identifier Identifier of the numbering system.
145      * @param userId Indicates update user id.
146      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
147      */
148     static I18nErrorCode SetSystemNumberingSystem(const std::string& identifier, int32_t userId = -1);
149 
150     /**
151      * @brief Gets commonly used number patterns for system locale.
152      *
153      * @param systemNumberPatterns a map containing the used number patterns and example of system locale.
154      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
155      */
156     static I18nErrorCode GetSystemNumberPatterns(std::unordered_map<std::string, std::string>& systemNumberPatterns);
157 
158     /**
159      * @brief Gets number pattern used by system locale.
160      *
161      * @param usingNumberPattern The number pattern identifier used by system locale.
162      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
163      */
164     static I18nErrorCode GetUsingNumberPattern(std::string& usingNumberPattern);
165 
166     /**
167      * @brief Sets the number pattern used by the system locale.
168      *
169      * @param pattern Identifier of the number pattern.
170      * @param userId Indicates update user id.
171      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
172      */
173     static I18nErrorCode SetSystemNumberPattern(const std::string& pattern, int32_t userId = -1);
174 
175     /**
176      * @brief Gets measurements supported by system locale.
177      *
178      * @param systemMeasurements a map will containing identifier and name of measurements supported by system locale.
179      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
180      */
181     static I18nErrorCode GetSystemMeasurements(std::unordered_map<std::string, std::string>& systemMeasurements);
182 
183     /**
184      * @brief Gets measurement currently used by system locale.
185      *
186      * @param usingMeasurement The identifier of measurement system using by system locale.
187      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
188      */
189     static I18nErrorCode GetUsingMeasurement(std::string& identifier);
190 
191     /**
192      * @brief Sets the measurement system used by the system locale.
193      *
194      * @param identifier Identifier of the measurement system.
195      * @param userId Indicates update user id.
196      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
197      */
198     static I18nErrorCode SetSystemMeasurement(const std::string& identifier, int32_t userId = -1);
199 
200     /**
201      * @brief Gets numerical date patterns and examples supported by system locale.
202      *
203      * @param numericalDatePatterns a map containing the date patterns and examples.
204      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
205      */
206     static I18nErrorCode GetSystemNumericalDatePatterns(
207         std::unordered_map<std::string, std::string>& numericalDatePatterns);
208 
209     /**
210      * @brief Gets numerical date pattern currently used by system locale.
211      *
212      * @param identifier Identifier of numerical date pattern used by system locale
213      * @return I18nErrorCode Return SUCCESS indicates that the getting was successful.
214      */
215     static I18nErrorCode GetUsingNumericalDatePattern(std::string& identifier);
216 
217     /**
218      * @brief Sets the numerical date pattern used by the system locale.
219      *
220      * @param identifier Identifier of the numerical date pattern.
221      * @param userId Indicates update user id.
222      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
223      */
224     static I18nErrorCode SetSystemNumericalDatePattern(const std::string& identifier, int32_t userId = -1);
225 
226     static TemperatureType GetTemperatureType();
227     static std::string GetTemperatureName(const TemperatureType& type);
228     static WeekDay GetFirstDayOfWeek();
229     static std::string GetEffectiveLanguage();
230     static std::string GetEffectiveLocale();
231     static std::string GetSystemLanguage();
232     static std::string GetSystemRegion();
233     static std::string GetSystemLocale();
234     static std::string GetSystemTimezone();
235     static std::unordered_set<std::string> GetSystemLanguages();
236     static std::unordered_set<std::string> GetSystemCountries(const std::string& language = "");
237     static bool IsSuggested(const std::string &language);
238     static bool IsSuggested(const std::string &language, const std::string &region);
239     static bool IsSuggestedV15(const std::string &language);
240     static bool IsSuggestedV15(const std::string &language, const std::string &region);
241     static std::string GetDisplayLanguage(const std::string &language, const std::string &displayLocale,
242         bool sentenceCase);
243     static std::string GetDisplayRegion(const std::string &region, const std::string &displayLocale,
244         bool sentenceCase);
245     static bool IsRTL(const std::string &locale);
246     static std::string GetValidLocale(const std::string &localeTag);
247     static bool IsEmpty24HourClock();
248     static bool Is24HourClock();
249     static std::string GetSystemHour();
250     static bool GetUsingLocalDigit();
251     static std::unordered_set<std::string> GetBlockedLanguages();
252     static std::unordered_set<std::string> GetBlockedRegions(const std::string& language = "");
253     static bool IsValidLanguage(const std::string &language);
254     static bool IsValidRegion(const std::string &region);
255     static bool IsValidTag(const std::string &tag);
256     static bool IsValid24HourClockValue(const std::string &tag);
257     static std::string GetSimplifiedLanguage(const std::string& language, int32_t &code);
258     static std::string GetSimplifiedSystemLanguage();
259     static std::string GetUnicodeWrappedFilePath(const std::string &path, const char delimiter,
260         std::shared_ptr<LocaleInfo> localeInfo, std::string &invalidField);
261     static std::string GetUnicodeWrappedFilePath(const std::string &path, const char delimiter,
262         const std::string& localeTag, std::string &invalidField);
263     static std::string ComputeEffectiveLanguage(const std::string& localeTag, const std::string& language);
264     static std::string GetSystemLocaleInstanceTag();
265     static std::pair<std::string, std::string> GetNumberPatternFromLocale(const std::string& localeTag);
266     static std::string RemoveCustExtParam(const std::string& locale);
267     static std::string RemoveAllExtParam(const std::string& locale);
268     static std::string ModifyExtParam(const std::string& locale, const std::string& key, const std::string& value,
269         const std::string& extParamKey);
270     static std::string QueryExtParam(const std::string& locale, const std::string& key,
271         const std::string& extParamKey);
272     static std::string GetNumberingSystemDigit(const std::string& NumberingSystem);
273     static std::string GetMeasurementFromLocale(const std::string& localeTag);
274     static std::string GetPatternNumberFromLocale(const std::string& locale);
275     static TemperatureType GetTemperatureTypeFromLocale(const std::string& localeTag);
276     static const std::string HOUR_EVENT_DATA;
277     static const std::string LANGUAGE_KEY;
278     static const std::string LOCALE_KEY;
279     static const std::string HOUR_KEY;
280     static const char *DEFAULT_REGION_KEY;
281     static constexpr int CONFIG_LEN = 128;
282 
283 private:
284     static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest);
285     static void Split(const std::string &src, const std::string &sep, std::unordered_set<std::string> &dest);
286     static I18nErrorCode UpdateSystemLocale(const std::string &language);
287     static constexpr uint32_t LANGUAGE_LEN = 2;
288     static constexpr uint32_t LOCALE_ITEM_COUNT = 3;
289     static constexpr uint32_t SCRIPT_OFFSET = 2;
290     static const char *UPGRADE_LOCALE_KEY;
291     static const char *DEFAULT_LOCALE_KEY;
292     static const char *DEFAULT_LANGUAGE_KEY;
293     static const char *SIM_COUNTRY_CODE_KEY;
294     static const char *SUPPORTED_LOCALES_PATH;
295     static const char *SUPPORTED_LOCALES_OLD_PATH;
296     static const char *SUPPORTED_LOCALES_NAME;
297     static const char *SUPPORTED_REGIONS_NAME;
298     static const char *WHITE_LANGUAGES_NAME;
299     static const char *REGIONS_LANGUAGES_PATH;
300     static const char *REGIONS_LANGUAGES_NAME;
301     static const char *SUPPORT_LOCALES_PATH;
302     static const char *SUPPORT_LOCALES_NAME;
303     static const char *DEFAULT_LOCALE;
304     static const char *DEVELOPER_MODE_KEY;
305     static const char *supportLocalesTag;
306     static const char *LANG_PATH;
307     static const char *rootTag;
308     static const char *secondRootTag;
309     static const char *rootRegion;
310     static const char *secondRootRegion;
311     static const uint32_t ELEMENT_NUM = 2;
312     static const size_t SYMBOL_HEX_LEN = 4;
313 
314     static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH;
315     static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME;
316 
317     static const char *OVERRIDE_SUPPORTED_REGIONS_NAME;
318     static const char *OVERRIDE_SUPPORTED_REGIONS_PATH;
319     static const char *DIALECT_LANGS_PATH;
320     static const char *DIALECT_LANGS_NAME;
321     static const char *REGION_PATH;
322     static const std::string TIMEZONE_KEY;
323     static const std::string DEFAULT_TIMEZONE;
324     static std::mutex dialectLocaleMutex;
325     static std::mutex region2DisplayNameMutex;
326     static std::mutex locale2DisplayNameMutex;
327     static std::shared_mutex is24HourLocaleMapMutex;
328     static const std::string STANDARD_EXT_PARAM_KEY;
329     static const std::string CUST_EXT_PARAM_KEY;
330     static const std::string NUMBER_SYSTEM_KEY;
331     static const std::string TEMPERATURE_UNIT_KEY;
332     static const std::string WEEK_DAY_KEY;
333     static const std::string CASE_FIRST_KEY;
334     static const std::string COLLATION_KEY;
335     static const std::string NUNBER_PATTERN_KEY;
336     static const std::string MEASUREMENT_KEY;
337     static const std::string NUMERIC_DATE_KEY;
338     static const std::string DEFAULT_NUMBERING_SYSTEM_NAME;
339 
340     static const std::unordered_set<std::string>& GetSupportedLocales();
341     static const std::unordered_set<std::string>& GetSupportedLocalesV15();
342     static const std::unordered_set<std::string>& GetSupportedRegions();
343     static void GetCountriesFromSim(std::vector<std::string> &simCountries);
344     static void GetRelatedLocales(std::unordered_set<std::string> &relatedLocales,
345         const std::vector<std::string> countries);
346     static void GetRelatedLocalesV15(std::unordered_set<std::string> &relatedLocales,
347         const std::vector<std::string> countries);
348     static void GetListFromFile(const char *path, const char *resourceName, std::unordered_set<std::string> &ret);
349     static void LoadRegionsLanguages();
350     static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another);
351     static std::string GetMainLanguage(const std::string &language,
352         std::unordered_map<std::string, std::string> selfDialectMap);
353     static std::string GetCountry(const std::string& parameter);
354     static std::string GetDisplayLanguageWithDialect(const std::string &language, const std::string &displayLocale);
355     static std::string GetDisplayOverrideRegion(const std::string &region, const std::string &displayLocale);
356     static std::string ComputeLocale(const std::string &displayLocale);
357     static void ReadLangData(const char *langDataPath);
358     static void ReadRegionData(const char *regionDataPath);
359     static void SetSupportedDialectLocales(const char* key, const char* value);
360     static void SetRegion2DisplayName(const char* key, const char* value);
361     static void SetLocale2DisplayName(const char* key, const char* value);
362     static bool Is24HourLocale(const std::string& systemLocale);
363     static bool HasDesignator(const std::string& inFormat, const char designator);
364     static void QueryUpgradeLocale();
365     static std::string GetUnicodeWrappedFilePathInner(const std::string &path, const char delimiter,
366         const std::string& localeTag, std::string &invalidField);
367     static I18nErrorCode GetSystemNumberingSystemsFromLanguage(const std::string& language,
368         std::unordered_map<std::string, std::string>& systemNumberingSystems);
369 
370 #ifdef SUPPORT_GRAPHICS
371     /**
372      * @brief Provided for updating i18n configuration include locale and language.
373      *
374      * @param locale Indicats update locale.
375      * @param language Indicates update language.
376      * @param userId Indicats update user id.
377      */
378     static void UpdateConfigurationLocaleAndLanguage(const std::string& locale, const std::string& language,
379         int32_t userId);
380 
381     /**
382      * @brief Provided for updating i18n configuration include is24Hour.
383      *
384      * @param locale Indicats update is24Hour.
385      * @param userId Indicats update user id.
386      */
387     static void UpdateConfiguration24Hour(const std::string& is24Hour, int32_t userId);
388 
389     /**
390      * @brief Provided for public i18n event include locale-change event and time-change event.
391      *
392      * @param eventType Indicates event type.
393      * @return I18nErrorCode Return SUCCESS indicates that the setting was successful.
394      */
395     static I18nErrorCode PublishCommonEvent(const std::string &eventType, int32_t userId);
396 #endif
397 
398     /**
399      * @brief Change language part of system locale to languageTag.
400      *
401      * @param languageTag Indicats language tag use to update locale.
402      * @return std::string Return locale tag.
403      */
404     static std::string UpdateLanguageOfLocale(const std::string &languageTag);
405 
406     /**
407      * @brief Provided for combining language, script, region, extend param to locale.
408      *
409      * @param languageTag Indicate language part in locale.
410      * @param scriptTag Indicate script part in locale.
411      * @param regionTag Indicate region part in locale.
412      * @param extendParamTag Indicate extend param part in locale.
413      * @return std::string Return locale tag.
414      */
415     static std::string CreateLocale(const std::string &languageTag, const std::string &scriptTag,
416         const std::string &regionTag, const std::string &extendParamTag);
417 
418     /**
419      * @brief Create a locale tag from region tag. Use icu to fill in missing parts.
420      *
421      * @param regionTag Indicats region tag used to create locale.
422      * @return std::string Return locale tag.
423      */
424     static std::string CreateLocaleFromRegion(const std::string &regionTag);
425 
426     /**
427      * @brief Change the region part of system locale to regionTag.
428      *
429      * @param regionTag Indicates region tag used to update locale.
430      * @return std::string Return locale Tag.
431      */
432     static std::string UpdateRegionOfLocale(const std::string &regionTag);
433     static I18nErrorCode GetSystemNumberPatternsFromLocale(
434         std::unordered_map<std::string, std::string>& systemNumberPatterns, const std::string& locale);
435     static std::string NumberPatternToHex(const std::string& groupingSymbol, const std::string& decimalSymbol);
436     static std::pair<std::string, std::string> HexToNumberPattern(const std::string& hex);
437     static bool IsValidMeasurement(const std::string& measurement);
438     static std::unordered_map<std::string, std::unordered_map<std::string, std::string>> ParseAllExtParam(
439         std::string locale);
440     static std::unordered_map<std::string, std::string> ParseExtParam(const std::string& extParam,
441         const std::string& extParamKey);
442     static std::string GenerateAllExtParam(
443         const std::unordered_map<std::string, std::unordered_map<std::string, std::string>>& extParamMap);
444     static std::string GenerateExtParam(const std::unordered_map<std::string, std::string>& extParamMap,
445         const std::string& extParamKey);
446     static std::vector<std::pair<size_t, std::string>> GetExtParamPositions(const std::string& locale);
447     static void ExtendWhiteLanguages();
448     static void ExtendLanguageWithScript(const std::string &languageStr);
449     static __attribute__((destructor)) void CleanupXmlResources();
450     static std::string GetPseudoDisplayLanguage(const std::string &language);
451     static std::unordered_set<std::string> supportedLocales;
452     static std::unordered_set<std::string> supportedLocalesV15;
453     static std::unordered_set<std::string> supportedRegions;
454     static std::unordered_set<std::string> supportLocales;
455     static std::unordered_set<std::string> dialectLang;
456     static std::unordered_set<std::string> overrideSupportedRegions;
457     static std::unordered_set<std::string> whiteLanguages;
458     static std::set<std::string> extendWhiteLanguageList;
459     static std::set<std::string> extendWhiteLanguageListV15;
460     static std::map<std::string, std::string> supportedDialectLocales;
461     static std::unordered_map<std::string, std::string> dialectMap;
462     static std::unordered_map<std::string, std::string> dialectMapV15;
463     static std::unordered_map<std::string, std::string> localDigitMap;
464     static std::map<std::string, std::string> locale2DisplayName;
465     static std::map<std::string, std::string> region2DisplayName;
466     static std::unordered_map<std::string, bool> is24HourLocaleMap;
467     static std::unordered_map<std::string, std::vector<std::string>> dialectLanguages;
468     static std::unordered_map<std::string, std::string> resourceIdMap;
469     static std::string currentDialectLocale;
470     static std::string currentOverrideRegion;
471     static std::set<std::string> validCaTag;
472     static std::set<std::string> validCoTag;
473     static std::set<std::string> validKnTag;
474     static std::set<std::string> validKfTag;
475     static std::set<std::string> validNuTag;
476     static std::set<std::string> validHcTag;
477     static bool listsInitialized;
478     static bool InitializeLists();
479 };
480 } // namespace I18n
481 } // namespace Global
482 } // namespace OHOS
483 #endif
484