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