1 /* 2 * Copyright (c) 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 UPDATE_UI_LANGUAGE_UI_H 16 #define UPDATE_UI_LANGUAGE_UI_H 17 18 #include <string> 19 #include <unordered_map> 20 #include <vector> 21 #include "json_visitor.h" 22 #include "language_ui_msg_id.h" 23 #include "updater_ui_traits.h" 24 25 namespace Updater { 26 namespace Lang { 27 enum class Language { 28 CHINESE = 0, 29 ENGLISH = 1, 30 SPANISH = 2 31 }; 32 33 class LanguageUI final { 34 DISALLOW_COPY_MOVE(LanguageUI); 35 public: 36 static LanguageUI &GetInstance(); 37 [[nodiscard]] bool Init(Language language); 38 // msgId used here to specify validation of msgId 39 const std::string &Translate(const std::string &key, 40 [[maybe_unused]] UpdaterUiMsgID msgId = UpdaterUiMsgID::DEFAULT_STRING) const; 41 [[nodiscard]] bool LoadLangRes(const JsonNode &node); 42 Language ParseLanguage() const; 43 private: 44 ~LanguageUI() = default; 45 LanguageUI(); 46 bool Parse(); 47 bool SetRes(const Res &res); 48 bool CheckLevel(int level); 49 bool ParseJson(const std::string &file); 50 std::unordered_map<std::string, std::string> strMap_; 51 std::vector<std::string> res_; 52 LangResource langRes_; 53 Language language_; 54 static constexpr auto LANG_RES_KEY = "locale"; 55 const static std::unordered_map<std::string, Language> LOCALES; 56 }; 57 } 58 59 #ifdef UPDATER_UI_SUPPORT 60 #define TR(tag) Lang::LanguageUI::GetInstance().Translate(STRINGFY(tag), UpdaterUiMsgID::tag) 61 #else 62 #define TR(tag) "" 63 #endif 64 } // namespace Updater 65 #endif /* UPDATE_UI_HOS_UPDATER_H */ 66