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