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
27namespace Updater {
28namespace Lang {
29enum class Language {
30    CHINESE = 0,
31    ENGLISH = 1,
32    SPANISH = 2
33};
34
35class LanguageUI final {
36    DISALLOW_COPY_MOVE(LanguageUI);
37public:
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);
45private:
46    ~LanguageUI() = default;
47    LanguageUI();
48    bool Parse();
49    bool SetRes(const Res &res);
50    bool CheckLevel(int level);
51    bool ParseJson(const std::string &file);
52    std::unordered_map<std::string, std::string> strMap_;
53    std::vector<std::string> res_;
54    LangResource langRes_;
55    Language language_;
56    Language defaultLanguage_ = Language::ENGLISH;
57    static constexpr auto LANG_RES_KEY = "locale";
58    const static std::unordered_map<std::string, Language> LOCALES;
59};
60}
61
62#define TR(tag) Lang::LanguageUI::GetInstance().Translate(STRINGFY(tag))
63} // namespace Updater
64#else
65#define TR(tag) ""
66#endif
67#endif /* UPDATE_UI_HOS_UPDATER_H */
68