• 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_PREFERRED_LANGUAGE_H
16 #define OHOS_GLOBAL_I18N_PREFERRED_LANGUAGE_H
17 
18 #include <set>
19 #include <string>
20 #include <unordered_set>
21 #include <vector>
22 #include "i18n_types.h"
23 
24 namespace OHOS {
25 
26 namespace NativePreferences {
27 class Preferences;
28 struct Options;
29 }
30 
31 namespace Global {
32 namespace I18n {
33 class PreferredLanguage {
34 public:
35     /**
36      * @brief Add language to system preferred language list on index.
37      *
38      * @param language Indicates language tag to add.
39      * @param index Indicates position to add.
40      * @return I18nErrorCode Return SUCCESS indicates that the add operation was successful.
41      */
42     static I18nErrorCode AddPreferredLanguage(const std::string &language, int32_t index);
43 
44     /**
45      * @brief Remove language from system preferred language list on index.
46      *
47      * @param index Indicates position to remove.
48      * @return I18nErrorCode Return SUCCESS indicates that the add operation was successful.
49      */
50     static I18nErrorCode RemovePreferredLanguage(int32_t index);
51     static std::vector<std::string> GetPreferredLanguageList();
52     static std::string GetFirstPreferredLanguage();
53 #ifdef SUPPORT_APP_PREFERRED_LANGUAGE
54     /**
55      * @brief Get App Language.
56      * Get the app language from app preferences data which saved by SetAppPreferredLanguage method.
57      *
58      * @return std::string return current app language.
59      */
60     static std::string GetAppPreferredLanguage();
61 
62     /**
63      * @brief check App Language.
64      * Check whether the preferred language is set for the app.
65      *
66      * @return bool return whether preferred language is set.
67      */
68     static bool IsSetAppPreferredLanguage();
69 
70     /**
71      * @brief Set App Language.
72      * The application interface will be refreshed in real time after call this method. And the language will be saved
73      * to app preferences data which will be used the next time the application starts.
74      *
75      * @param language language to be set.
76      * @param errCode Indicates whether the setting was successful; errCode == I18nErrorCode::SUCCESS means successful.
77      */
78     static void SetAppPreferredLanguage(const std::string &language, I18nErrorCode &errCode);
79 #endif
80     static std::string GetPreferredLocale();
81 
82 private:
83     static bool IsValidLanguage(const std::string &language);
84     static bool IsValidTag(const std::string &tag);
85     static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest);
86 #ifdef SUPPORT_APP_PREFERRED_LANGUAGE
87     static std::shared_ptr<NativePreferences::Preferences> GetI18nAppPreferences();
88 #endif
89     /**
90      * @brief Filtering the language list.
91      *
92      * @param preferredLanguagesList Indicates list of languages to be filtered.
93      * @return vector Return list of supported languages.
94      */
95     static std::vector<std::string> FilterLanguages(std::vector<std::string>& preferredLanguagesList);
96 
97     /**
98      * @brief Match the language to the supported language.
99      *
100      * @param language Indicates language code to be matched.
101      * @return string Return matching result.
102      */
103     static std::string GetMatchedLanguage(const std::string& language);
104 
105     /**
106      * @brief Normalize index to target range [0, max].
107      *
108      * @param index Indicates value to normalize.
109      * @param max Indicates the max value of range.
110      * @return int32_t Return normalized index.
111      */
112     static int32_t NormalizeIndex(int32_t index, int32_t max);
113 
114     /**
115      * @brief Find the language index in system preferred language list.
116      *
117      * @param language Indicates language tag to find.
118      * @return int32_t Return index of language in system preferred language list.
119      */
120     static int32_t FindLanguage(const std::string &language);
121 
122     /**
123      * @brief Add language to position index of system preferred language list when language is not in list.
124      *
125      * @param language Indicates language tag to add.
126      * @param index Indicates position to add.
127      * @param preferredLanguages Indicates system preferred language list after inserting language.
128      * @param errCode Indicates whether the add operation was successful.
129      */
130     static void AddNonExistPreferredLanguage(const std::string& language, int32_t index,
131         std::vector<std::string> &preferredLanguages, I18nErrorCode &errCode);
132 
133     /**
134      * @brief Add language to position index of system preferred language list when language is in list.
135      *
136      * @param language Indicates language tag to add.
137      * @param index Indicates position to add.
138      * @param preferredLanguages Indicates system preferred language list after inserting language.
139      * @param errCode Indicates whether the add operation was successful.
140      */
141     static void AddExistPreferredLanguage(const std::string& language, int32_t index,
142         std::vector<std::string> &preferredLanguages, I18nErrorCode &errCode);
143 
144     /**
145      * @brief combin preferred language list to string.
146      *
147      * @param preferredLanguages Indicates preferred language list.
148      * @return string Return Indicates the joined preferred languages.
149      */
150     static std::string JoinPreferredLanguages(const std::vector<std::string> preferredLanguages);
151 
152     /**
153      * @brief Set the Preferred Languages to System parameter.
154      *
155      * @param preferredLanguages Indicates preferreder languages.
156      * @return I18nErrorCode Return SUCCESS indicates that the add operation was successful.
157      */
158     static I18nErrorCode SetPreferredLanguages(const std::string &preferredLanguages);
159     static const char *RESOURCE_PATH_HEAD;
160     static const char *RESOURCE_PATH_TAILOR;
161     static const char *RESOURCE_PATH_SPLITOR;
162     static const char *PREFERRED_LANGUAGES;
163     static const char *APP_LANGUAGE_KEY;
164     static const char *I18N_PREFERENCES_FILE_NAME;
165     static const char *DEFAULT_PREFERRED_LANGUAGE;
166     static std::unordered_set<std::string> supportLanguageListExt;
167     static constexpr int CONFIG_LEN = 128;
168     static constexpr uint32_t LANGUAGE_LEN = 2;
169 };
170 } // namespace I18n
171 } // namespace Global
172 } // namespace OHOS
173 #endif