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 Set App Language. 60 * The application interface will be refreshed in real time after call this method. And the language will be saved 61 * to app preferences data which will be used the next time the application starts. 62 * 63 * @param language language to be set. 64 * @param errCode Indicates whether the setting was successful; errCode == I18nErrorCode::SUCCESS means successful. 65 */ 66 static void SetAppPreferredLanguage(const std::string &language, I18nErrorCode &errCode); 67 #endif 68 static std::string GetPreferredLocale(); 69 70 private: 71 static bool IsValidLanguage(const std::string &language); 72 static bool IsValidTag(const std::string &tag); 73 static void Split(const std::string &src, const std::string &sep, std::vector<std::string> &dest); 74 #ifdef SUPPORT_APP_PREFERRED_LANGUAGE 75 static std::shared_ptr<NativePreferences::Preferences> GetI18nAppPreferences(); 76 #endif 77 /** 78 * @brief Normalize index to target range [0, max]. 79 * 80 * @param index Indicates value to normalize. 81 * @param max Indicates the max value of range. 82 * @return int32_t Return normalized index. 83 */ 84 static int32_t NormalizeIndex(int32_t index, int32_t max); 85 86 /** 87 * @brief Find the language index in system preferred language list. 88 * 89 * @param language Indicates language tag to find. 90 * @return int32_t Return index of language in system preferred language list. 91 */ 92 static int32_t FindLanguage(const std::string &language); 93 94 /** 95 * @brief Add language to position index of system preferred language list when language is not in list. 96 * 97 * @param language Indicates language tag to add. 98 * @param index Indicates position to add. 99 * @param preferredLanguages Indicates system preferred language list after inserting language. 100 * @param errCode Indicates whether the add operation was successful. 101 */ 102 static void AddNonExistPreferredLanguage(const std::string& language, int32_t index, 103 std::vector<std::string> &preferredLanguages, I18nErrorCode &errCode); 104 105 /** 106 * @brief Add language to position index of system preferred language list when language is in list. 107 * 108 * @param language Indicates language tag to add. 109 * @param index Indicates position to add. 110 * @param preferredLanguages Indicates system preferred language list after inserting language. 111 * @param errCode Indicates whether the add operation was successful. 112 */ 113 static void AddExistPreferredLanguage(const std::string& language, int32_t index, 114 std::vector<std::string> &preferredLanguages, I18nErrorCode &errCode); 115 116 /** 117 * @brief combin preferred language list to string. 118 * 119 * @param preferredLanguages Indicates preferred language list. 120 * @return string Return Indicates the joined preferred languages. 121 */ 122 static std::string JoinPreferredLanguages(const std::vector<std::string> preferredLanguages); 123 124 /** 125 * @brief Set the Preferred Languages to System parameter. 126 * 127 * @param preferredLanguages Indicates preferreder languages. 128 * @return I18nErrorCode Return SUCCESS indicates that the add operation was successful. 129 */ 130 static I18nErrorCode SetPreferredLanguages(const std::string &preferredLanguages); 131 static const char *RESOURCE_PATH_HEAD; 132 static const char *RESOURCE_PATH_TAILOR; 133 static const char *RESOURCE_PATH_SPLITOR; 134 static const char *PREFERRED_LANGUAGES; 135 static const char *APP_LANGUAGE_KEY; 136 static const char *I18N_PREFERENCES_FILE_NAME; 137 static constexpr int CONFIG_LEN = 128; 138 static constexpr uint32_t LANGUAGE_LEN = 2; 139 }; 140 } // namespace I18n 141 } // namespace Global 142 } // namespace OHOS 143 #endif