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