CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
I18n.h
Go to the documentation of this file.
1
25
26#pragma once
27
28#include "cdc_core/Raii.h"
29
30#include <cstdint>
31#include <cstddef>
32#include <functional>
33#include <string>
34#include <vector>
35
36namespace cdc::ui {
37
44struct I18nEntry {
45 const char* key;
46 const char* en;
47};
48
62 std::string code;
63 std::string name;
64};
65
66class I18n {
67public:
69 static constexpr const char* OVERLAY_DIR = "/plugins/i18n";
70
72 static I18n& instance();
73
78 bool init();
79
94 bool loadOverlay();
95
106 void registerEnglishTable(const I18nEntry* entries, std::size_t count);
107
113 const char* tr(const char* key) const;
114
127 const char* overlayTr(const char* key) const;
128
130 const std::string& getLanguageCode() const { return currentLang_; }
131
142 bool setLanguageCode(const char* code);
143
145 const std::vector<OverlayLanguage>& availableOverlayLanguages() const { return overlayLangs_; }
146
157 const char* languageName(const char* code) const;
158
171 using LanguageChangedCallback = std::function<void()>;
172 void setOnLanguageChanged(LanguageChangedCallback cb) { onChanged_ = std::move(cb); }
173
174private:
175 I18n();
176
177 void registerCoreEnglishTable();
178 bool sortIfNeeded() const;
179 const char* enLookup(const char* key) const;
180 const char* overlayLookup(const char* key) const;
181 void loadLanguageFromNvs();
182 void saveLanguageToNvs();
183
185 void scanAvailableLanguages();
187 bool loadActiveOverlayFile();
188
189 mutable std::vector<I18nEntry> en_;
190 mutable bool enSorted_ = false;
191
192 // Active-language overlay stored entirely in PSRAM: a packed
193 // "key\0value\0..." blob plus a key-sorted reference index for binary
194 // search. Keeps the (potentially large) translation table off the scarce
195 // internal heap, which WiFi/BLE need for contiguous allocations.
196 struct OverlayRef { const char* key; const char* value; };
199 std::size_t overlayCount_ = 0;
200 std::vector<OverlayLanguage> overlayLangs_;
201
202 std::string currentLang_ = "en";
203
204 LanguageChangedCallback onChanged_;
205};
206
208inline const char* tr(const char* key)
209{
210 return I18n::instance().tr(key);
211}
212
213} // namespace cdc::ui
Shared RAII wrappers for firmware resources.
const std::string & getLanguageCode() const
Current language code (lower-case ISO-639-1, e.g. "en", "de").
Definition I18n.h:130
static I18n & instance()
Singleton accessor.
Definition I18n.cpp:287
void setOnLanguageChanged(LanguageChangedCallback cb)
Definition I18n.h:172
const char * overlayTr(const char *key) const
Overlay-only lookup with no English fallback.
Definition I18n.cpp:352
std::function< void()> LanguageChangedCallback
Callback invoked whenever the active translation table changes.
Definition I18n.h:171
bool setLanguageCode(const char *code)
Set the active language by code.
Definition I18n.cpp:370
bool loadOverlay()
Rescan available languages and (re)load the active overlay.
Definition I18n.cpp:536
const std::vector< OverlayLanguage > & availableOverlayLanguages() const
Languages discovered on the plugins FAT (does not include "en").
Definition I18n.h:145
void registerEnglishTable(const I18nEntry *entries, std::size_t count)
Append English entries to the lookup table.
Definition I18n.cpp:307
const char * languageName(const char *code) const
Display name (endonym) for a language code, for the picker.
Definition I18n.cpp:557
static constexpr const char * OVERLAY_DIR
Directory on the plugins FAT holding the per-language files.
Definition I18n.h:69
bool init()
Initialize and load persisted language code from NVS.
Definition I18n.cpp:298
const char * tr(const char *key) const
Look up a translation by key.
Definition I18n.cpp:358
std::unique_ptr< T[], CapsFreeDeleter > PsramUniquePtr
Definition Raii.h:44
Centralized key-code constants for cdc_views.
Definition IModule.h:8
const char * tr(const char *key)
Look up a translation by string key.
Definition I18n.h:208
Single English translation entry.
Definition I18n.h:44
const char * en
English translation - rodata literal.
Definition I18n.h:46
const char * key
Stable string key, e.g. "core.save" or "mod_2fa.codes".
Definition I18n.h:45
Internationalization singleton.
Definition I18n.h:61
std::string code
Language code, e.g. "de" (lower-case).
Definition I18n.h:62
std::string name
Endonym for the picker (CP437-encoded), e.g. "Deutsch".
Definition I18n.h:63