~aleteoryx/muditaos

ref: 2a53becd2524fc0dba7260632d3ec409eb3a61fd muditaos/module-utils/i18n/i18n.cpp -rw-r--r-- 7.0 KiB
2a53becd — Maciej Janicki [BH-1136] Fix bootloop after low power 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "i18nImpl.hpp"
#include <log/log.hpp>
#include <algorithm>
#include <cstdio>
#include <purefs/filesystem_paths.hpp>

namespace utils
{
    namespace
    {
        auto returnNonEmptyString(const std::string &str, const std::string &ret) -> const std::string &
        {
            return str.empty() ? ret : str;
        }
    } // namespace

    namespace
    {
        class i18nPrivateInterface : public i18n
        {
          public:
            const std::string &get(const std::string &str);
            const std::vector<std::string> get_array(const std::string &str);
            using i18n::getDisplayLanguage;
            using i18n::getDisplayLanguagePath;
            using i18n::getInputLanguage;
            using i18n::getInputLanguageFilename;
            using i18n::getInputLanguagePath;
            using i18n::resetAssetsPath;
            using i18n::resetDisplayLanguages;
            using i18n::setDisplayLanguage;
            using i18n::setInputLanguage;
        };

        const std::string &i18nPrivateInterface::get(const std::string &str)
        {
            // if language pack returned nothing then try default language
            if (getDisplayLanguageJSON()[str].string_value().empty()) {
                return returnNonEmptyString(getFallbackLanguageJSON()[str].string_value(), str);
            }
            return returnNonEmptyString(getDisplayLanguageJSON()[str].string_value(), str);
        }

        const std::vector<std::string> i18nPrivateInterface::get_array(const std::string &str)
        {
            std::vector<std::string> out;
            auto object = getDisplayLanguageJSON()[str];
            // if language pack returned nothing then try default language
            if (not object.is_array()) {
                object = getFallbackLanguageJSON()[str];
            }
            if (object.is_array()) {
                out.reserve(object.array_items().size());
                std::for_each(object.array_items().begin(), object.array_items().end(), [&out](const auto &o) {
                    out.emplace_back(o.string_value());
                });
            }
            return out;
        }

    } // namespace

    i18nPrivateInterface localize;

    json11::Json LangLoaderImpl::createJson(const std::string &filename)
    {
        const auto path = localize.getDisplayLanguagePath() / (filename + utils::files::jsonExtension);
        auto fd         = std::fopen(path.c_str(), "r");
        if (fd == nullptr) {
            LOG_FATAL("Error during opening file %s", path.c_str());
            return json11::Json();
        }

        uint32_t fsize = std::filesystem::file_size(path);

        auto stream = std::make_unique<char[]>(fsize + 1); // +1 for NULL terminator

        std::fread(stream.get(), 1, fsize, fd);

        std::string err;
        json11::Json js = json11::Json::parse(stream.get(), err);

        std::fclose(fd);

        // Error
        if (err.length() != 0) {
            LOG_FATAL("%s", err.c_str());
            return json11::Json();
        }
        else {
            return js;
        }
    }

    std::vector<Language> LangLoader::getAvailableDisplayLanguages() const
    {
        std::vector<std::string> languageNames;
        for (const auto &entry : std::filesystem::directory_iterator(getDisplayLanguagePath())) {
            languageNames.push_back(std::filesystem::path(entry.path()).stem());
        }
        return languageNames;
    }

    std::vector<Language> LangLoader::getAvailableInputLanguages() const
    {
        std::vector<std::string> languageNames;
        for (const auto &entry : std::filesystem::directory_iterator(getInputLanguagePath())) {
            languageNames.push_back(std::filesystem::path(entry.path()).stem());
        }
        return languageNames;
    }

    void i18n::resetAssetsPath(const std::filesystem::path &assets)
    {
        DisplayLanguageDirPath = assets / "lang";
        InputLanguageDirPath   = assets / "profiles";
    }

    bool i18n::setInputLanguage(const Language &lang)
    {
        if (lang.empty() || lang == inputLanguage) {
            return false;
        }

        inputLanguage = lang;
        return true;
    }

    const std::string &i18n::getInputLanguageFilename(const std::string &inputMode)
    {
        // if language pack returned nothing then try default language
        if (inputLanguage.empty()) {
            inputLanguageFilename = fallbackLanguageName + utils::files::breakSign + inputMode;
        }
        else {
            inputLanguageFilename = inputLanguage + utils::files::breakSign + inputMode;
        }
        return inputLanguageFilename;
    }

    bool i18n::setDisplayLanguage(const Language &lang)
    {
        if (fallbackLanguage == json11::Json()) {
            loadFallbackLanguage();
        }

        if ((lang.empty() || lang == currentDisplayLanguage) && !currentDisplayLanguage.empty()) {

            return false;
        }
        else if (json11::Json pack = loader.createJson(lang); pack != json11::Json()) {

            currentDisplayLanguage = lang;
            changeDisplayLanguage(pack);

            return true;
        }
        return false;
    }

    void i18n::changeDisplayLanguage(const json11::Json &lang)
    {
        cpp_freertos::LockGuard lock(mutex);
        displayLanguage = lang;
    }

    void i18n::loadFallbackLanguage()
    {
        cpp_freertos::LockGuard lock(mutex);
        currentDisplayLanguage = fallbackLanguageName;
        fallbackLanguage       = loader.createJson(fallbackLanguageName);
    }

    const std::string &translate(const std::string &text)
    {
        return utils::localize.get(text);
    }

    const std::vector<std::string> translate_array(const std::string &text)
    {
        return utils::localize.get_array(text);
    }

    const std::string &getDisplayLanguage()
    {
        return utils::localize.getDisplayLanguage();
    }

    const std::string &getInputLanguage()
    {
        return utils::localize.getInputLanguage();
    }

    const std::string &getInputLanguageFilename(const std::string &inputMode)
    {
        return localize.getInputLanguageFilename(inputMode);
    }

    bool setInputLanguage(const Language &lang)
    {
        return localize.setInputLanguage(lang);
    }

    bool setDisplayLanguage(const Language &lang)
    {
        return localize.setDisplayLanguage(lang);
    }

    const std::filesystem::path getInputLanguagePath()
    {
        return localize.getInputLanguagePath();
    }

    const std::filesystem::path getDisplayLanguagePath()
    {
        return localize.getDisplayLanguagePath();
    }

    void i18n::resetDisplayLanguages()
    {
        currentDisplayLanguage.clear();
        displayLanguage  = json11::Json();
        fallbackLanguage = json11::Json();
    }

    void resetDisplayLanguages()
    {
        return localize.resetDisplayLanguages();
    }

    void resetAssetsPath(const std::filesystem::path &p)
    {
        return localize.resetAssetsPath(p);
    }

} // namespace utils