• 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 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 class LocaleConfig {
29 public:
30     LocaleConfig() = default;
31     virtual ~LocaleConfig() = default;
32     static bool SetSystemLanguage(const std::string &language);
33     static bool SetSystemRegion(const std::string &region);
34     static bool SetSystemLocale(const std::string &locale);
35     static std::string GetSystemLanguage();
36     static std::string GetSystemRegion();
37     static std::string GetSystemLocale();
38     static void GetSystemLanguages(std::vector<std::string> &ret);
39     static void GetSystemCountries(std::vector<std::string> &ret);
40     static bool IsSuggested(const std::string &language);
41     static bool IsSuggested(const std::string &language, const std::string &region);
42     static std::string GetDisplayLanguage(const std::string &language, const std::string &displayLocale,
43         bool sentenceCase);
44     static std::string GetDisplayRegion(const std::string &region, const std::string &displayLocale,
45         bool sentenceCase);
46     static bool IsRTL(const std::string &locale);
47     static std::string GetValidLocale(const std::string &localeTag);
48     static bool Is24HourClock();
49     static bool Set24HourClock(bool option);
50     static bool CheckPermission();
51     static bool SetUsingLocalDigit(bool flag);
52     static bool GetUsingLocalDigit();
53     static std::unordered_set<std::string> GetBlockedLanguages();
54     static std::unordered_set<std::string> GetBlockedRegions();
55     static std::unordered_set<std::string> GetLanguageBlockedRegions();
56     static bool IsValidTag(const std::string &tag);
57 
58 private:
59     static bool IsValidLanguage(const std::string &language);
60     static bool IsValidRegion(const std::string &region);
61     static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest);
62     static bool UpdateSystemLocale(const std::string &language);
63     static constexpr uint32_t LANGUAGE_LEN = 2;
64     static constexpr uint32_t LOCALE_ITEM_COUNT = 3;
65     static constexpr uint32_t SCRIPT_OFFSET = 2;
66     static const char *LANGUAGE_KEY;
67     static const char *LOCALE_KEY;
68     static const char *HOUR_KEY;
69     static const char *DEFAULT_LOCALE_KEY;
70     static const char *DEFAULT_LANGUAGE_KEY;
71     static const char *DEFAULT_REGION_KEY;
72     static const char *SIM_COUNTRY_CODE_KEY;
73     static const char *SUPPORTED_LOCALES_PATH;
74     static const char *SUPPORTED_REGIONS_PATH;
75     static constexpr int CONFIG_LEN = 128;
76     static const char *SUPPORTED_LOCALES_NAME;
77     static const char *SUPPORTED_REGIONS_NAME;
78     static const char *WHITE_LANGUAGES_NAME;
79     static const char *WHITE_LANGUAGES_PATH;
80     static const char *FORBIDDEN_REGIONS_PATH;
81     static const char *FORBIDDEN_REGIONS_NAME;
82     static const char *FORBIDDEN_LANGUAGES_PATH;
83     static const char *FORBIDDEN_LANGUAGES_NAME;
84     static const char *SUPPORT_LOCALES_PATH;
85     static const char *SUPPORT_LOCALES_NAME;
86     static const char *DEFAULT_LOCALE;
87     static const char *supportLocalesTag;
88     static const char *LANG_PATH;
89     static const char *rootTag;
90     static const char *secondRootTag;
91     static const char *rootRegion;
92     static const char *secondRootRegion;
93     static const uint32_t ELEMENT_NUM = 2;
94 
95     static const char *SUPPORTED_LANGUAGE_EN_LATN_PATH;
96     static const char *SUPPORTED_LANGUAGE_EN_LATN_NAME;
97 
98     static const char *OVERRIDE_SUPPORTED_REGIONS_NAME;
99     static const char *OVERRIDE_SUPPORTED_REGIONS_PATH;
100     static const char *DIALECT_LANGS_PATH;
101     static const char *DIALECT_LANGS_NAME;
102     static const char *REGION_PATH;
103 
104     static const std::unordered_set<std::string>& GetSupportedLocales();
105     static const std::unordered_set<std::string>& GetForbiddenRegions();
106     static const std::unordered_set<std::string>& GetSupportedRegions();
107     static void GetCountriesFromSim(std::vector<std::string> &simCountries);
108     static void GetRelatedLocales(std::unordered_set<std::string> &relatedLocales,
109         const std::vector<std::string> countries);
110     static std::string GetRegionChangeLocale(const std::string &languageTag, const std::string &region);
111     static void GetListFromFile(const char *path, const char *resourceName, std::unordered_set<std::string> &ret);
112     static void Expunge(std::unordered_set<std::string> &src, const std::unordered_set<std::string> &another);
113     static std::string GetMainLanguage(const std::string &language);
114     static std::string GetDsiplayLanguageWithDialect(const std::string &language, const std::string &displayLocale);
115     static std::string GetDisplayOverrideRegion(const std::string &region, const std::string &displayLocale);
116     static std::string ComputeLocale(const std::string &displayLocale);
117     static void ReadLangData(const char *langDataPath);
118     static void ReadRegionData(const char *regionDataPath);
119     static void ProcessForbiddenRegions(const std::unordered_set<std::string> &forbiddenRegions);
120     static std::unordered_set<std::string> supportedLocales;
121     static std::unordered_set<std::string> supportedRegions;
122     static std::unordered_set<std::string> supportLocales;
123     static std::unordered_set<std::string> dialectLang;
124     static std::unordered_set<std::string> overrideSupportedRegions;
125     static std::unordered_set<std::string> blockedLanguages;
126     static std::unordered_set<std::string> blockedRegions;
127     static std::unordered_map<std::string, std::unordered_set<std::string>> blockedLanguageRegions;
128     static std::unordered_set<std::string> whiteLanguages;
129     static std::map<std::string, std::string> supportedDialectLocales;
130     static std::unordered_map<std::string, std::string> dialectMap;
131     static std::unordered_map<std::string, std::string> localDigitMap;
132     static std::map<std::string, std::string> locale2DisplayName;
133     static std::map<std::string, std::string> region2DisplayName;
134     static std::string currentDialectLocale;
135     static std::string currentOverrideRegion;
136     static std::set<std::string> validCaTag;
137     static std::set<std::string> validCoTag;
138     static std::set<std::string> validKnTag;
139     static std::set<std::string> validKfTag;
140     static std::set<std::string> validNuTag;
141     static std::set<std::string> validHcTag;
142     static bool listsInitialized;
143     static bool InitializeLists();
144 };
145 } // namespace I18n
146 } // namespace Global
147 } // namespace OHOS
148 #endif
149