~aleteoryx/muditaos

ref: sign_test muditaos/module-utils/i18n/i18nImpl.hpp -rw-r--r-- 2.5 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
69
70
71
72
73
74
75
76
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "JSONLoader.hpp"
#include "Metadata.hpp"

#include <optional>
#include <mutex.hpp>
#include <json11.hpp>
#include <i18n/i18n.hpp>

#include <purefs/filesystem_paths.hpp>

namespace utils
{
    class i18n
    {
      private:
        using Loader  = std::function<std::optional<json11::Json>(const std::filesystem::path &path)>;
        Loader loader = JSONLoader;

        json11::Json displayLanguage;
        json11::Json fallbackLanguage; // backup language if item not found
        Language fallbackLanguageName = getDefaultLanguage();
        Language inputLanguage        = fallbackLanguageName;
        Language inputLanguageFilename;
        Language currentDisplayLanguage;
        std::filesystem::path InputLanguageDirPath   = purefs::dir::getSystemDataDirPath() / "profiles";
        std::filesystem::path DisplayLanguageDirPath = purefs::dir::getSystemDataDirPath() / "lang";
        cpp_freertos::MutexStandard mutex;
        std::vector<LanguageMetadata> metadata;

        void loadFallbackLanguage();
        void loadMetadata();
        std::optional<LanguageMetadata> getMetadata(const Language &lang) const;
        std::optional<LanguageMetadata> fetchMetadata(const std::filesystem::path &path) const;

      public:
        json11::Json &getDisplayLanguageJSON()
        {
            return displayLanguage;
        }
        json11::Json &getFallbackLanguageJSON()
        {
            return fallbackLanguage;
        }
        const std::string &getDisplayLanguage()
        {
            return currentDisplayLanguage;
        }
        const std::string &getInputLanguage()
        {
            return inputLanguage;
        }
        const std::string &getInputLanguageFilename(const std::string &inputMode);
        bool setInputLanguage(const Language &lang);
        bool setDisplayLanguage(const Language &lang);
        const std::filesystem::path getInputLanguagePath() const
        {
            return InputLanguageDirPath;
        }
        const std::filesystem::path getDisplayLanguagePath() const
        {
            return DisplayLanguageDirPath;
        }

        std::vector<Language> getAvailableDisplayLanguages() const;
        std::vector<Language> getAvailableInputLanguages() const;

        void resetDisplayLanguages();
        void resetAssetsPath(const std::filesystem::path &);
    };

} // namespace utils