• 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     void SetDefaultLanguage(Language language);
45     Language ParseLangIfZh(const std::string &globalLang) const;
46 private:
47     ~LanguageUI() = default;
48     LanguageUI();
49     bool Parse();
50     bool SetRes(const Res &res);
51     bool CheckLevel(int level);
52     bool ParseJson(const std::string &file);
53     std::unordered_map<std::string, std::string> strMap_;
54     std::vector<std::string> res_;
55     LangResource langRes_;
56     Language language_;
57     Language defaultLanguage_ = Language::ENGLISH;
58     static constexpr auto LANG_RES_KEY = "locale";
59     const static std::unordered_map<std::string, Language> LOCALES;
60 };
61 }
62 
63 #define TR(tag) Lang::LanguageUI::GetInstance().Translate(STRINGFY(tag))
64 } // namespace Updater
65 #else
66 #define TR(tag) ""
67 #endif
68 #endif /* UPDATE_UI_HOS_UPDATER_H */
69