~aleteoryx/muditaos

ref: 10a2c379699cbb9783be27ff7b1fa30fbd38953c muditaos/module-utils/i18n/i18n.hpp -rw-r--r-- 2.2 KiB
10a2c379 — Wiktor S. Ovalle Correa [EGD-3649] Fix paths in UT 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "json/json11.hpp"
#include <string>
#include <filesystem>
#include <mutex.hpp>

using Language = std::string;

namespace utils
{
    namespace files
    {
        constexpr auto jsonExtension = ".json";
        constexpr auto breakSign     = "_";
    } // namespace files

    class LangLoader
    {
      public:
        virtual ~LangLoader() = default;
        std::vector<Language> getAvailableDisplayLanguages() const;
        std::vector<Language> getAvailableInputLanguages() const;
        json11::Json createJson(const std::string &filename);
    };

    class i18n
    {
      private:
        json11::Json displayLanguage;
        json11::Json fallbackLanguage; // backup language if item not found
        LangLoader loader;
        Language fallbackLanguageName = DefaultLanguage;
        Language inputLanguage = fallbackLanguageName;
        Language inputLanguageFilename;
        Language currentDisplayLanguage;
        mutable cpp_freertos::MutexStandard mutex;

        void changeDisplayLanguage(const json11::Json &lang);
        void loadFallbackLanguage();

      public:
        static constexpr auto DefaultLanguage              = "English";
        std::filesystem::path DisplayLanguageDirPath       = "assets/lang";
        std::filesystem::path InputLanguageDirPath         = "assets/profiles";
        void resetAssetsPath(const std::filesystem::path &);

        virtual ~i18n() = default;
        void setInputLanguage(const Language &lang);
        const std::string &getInputLanguage(const std::string &inputMode);
        const std::string &getInputLanguageFilename(const std::string &inputMode);
        const std::string &getInputLanguage();
        const std::string &getDisplayLanguage();
        const std::string &get(const std::string &str);
        bool setDisplayLanguage(const Language &lang);
        void resetDisplayLanguages();
    };

    // Global instance of i18 class
    extern i18n localize;
    inline auto translateI18(const std::string &text)
    {
        return utils::localize.get(text);
    };
} // namespace utils