CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
cdc::ui::I18n Class Reference

#include <I18n.h>

Public Types

using LanguageChangedCallback = std::function<void()>
 Callback invoked whenever the active translation table changes.

Public Member Functions

bool init ()
 Initialize and load persisted language code from NVS.
bool loadOverlay ()
 Rescan available languages and (re)load the active overlay.
void registerEnglishTable (const I18nEntry *entries, std::size_t count)
 Append English entries to the lookup table.
const char * tr (const char *key) const
 Look up a translation by key.
const char * overlayTr (const char *key) const
 Overlay-only lookup with no English fallback.
const std::string & getLanguageCode () const
 Current language code (lower-case ISO-639-1, e.g. "en", "de").
bool setLanguageCode (const char *code)
 Set the active language by code.
const std::vector< OverlayLanguage > & availableOverlayLanguages () const
 Languages discovered on the plugins FAT (does not include "en").
const char * languageName (const char *code) const
 Display name (endonym) for a language code, for the picker.
void setOnLanguageChanged (LanguageChangedCallback cb)

Static Public Member Functions

static I18n & instance ()
 Singleton accessor.

Static Public Attributes

static constexpr const char * OVERLAY_DIR = "/plugins/i18n"
 Directory on the plugins FAT holding the per-language files.

Detailed Description

Definition at line 66 of file I18n.h.

Member Typedef Documentation

◆ LanguageChangedCallback

using cdc::ui::I18n::LanguageChangedCallback = std::function<void()>

Callback invoked whenever the active translation table changes.

Triggered on:

UI code uses this to refresh any cached label pointers - menu items hold const char* into either rodata (English fallback) or the overlay's PSRAM-backed string storage, both of which are invalidated by a language change.

Definition at line 171 of file I18n.h.

Member Function Documentation

◆ availableOverlayLanguages()

const std::vector< OverlayLanguage > & cdc::ui::I18n::availableOverlayLanguages ( ) const
inline

Languages discovered on the plugins FAT (does not include "en").

Definition at line 145 of file I18n.h.

◆ getLanguageCode()

const std::string & cdc::ui::I18n::getLanguageCode ( ) const
inline

Current language code (lower-case ISO-639-1, e.g. "en", "de").

Definition at line 130 of file I18n.h.

Referenced by cdc::plugin_manager::PluginManager::getLockscreenItems(), and cdc::plugin_manager::Plugin::loadLangOverlay().

◆ init()

bool cdc::ui::I18n::init ( )

Initialize and load persisted language code from NVS.

Returns
true on success.

Definition at line 298 of file I18n.cpp.

References LOG_I, and cdc::ui::TAG.

Referenced by cdc::ui::ui_init().

◆ instance()

◆ languageName()

const char * cdc::ui::I18n::languageName ( const char * code) const

Display name (endonym) for a language code, for the picker.

"en" returns the in-code English name ("English"); any other code returns the core.lang_name read from its lang_<code>.json, falling back to the code itself if unknown.

Parameters
codeLanguage code (e.g. "en", "de").
Returns
CP437-encoded name; stable until the next rescan.

Definition at line 557 of file I18n.cpp.

◆ loadOverlay()

bool cdc::ui::I18n::loadOverlay ( )

Rescan available languages and (re)load the active overlay.

Scans OVERLAY_DIR for lang_<code>.json files to populate the picker list, then parses the file for the current language into the active table. A lang_<code>.json is a flat object:

{ "core.lang_name": "Deutsch", "core.save": "Speichern", ... }

Safe to call when the current language is "en" (just rescans).

Returns
true if the active overlay loaded (or language is "en"), false if the active language's file was missing/invalid.

Definition at line 536 of file I18n.cpp.

References LOG_I, and cdc::ui::TAG.

Referenced by initPluginSystem().

◆ overlayTr()

const char * cdc::ui::I18n::overlayTr ( const char * key) const

Overlay-only lookup with no English fallback.

Returns the translation from the currently active overlay language, or nullptr if the key is missing OR the active language is "en". Useful for plugin code that wants to try a namespaced key in the central overlay before falling back to its own English manifest table.

Parameters
keyStable string key.
Returns
Pointer into PSRAM overlay storage (stable until the active language changes), or nullptr.

Definition at line 352 of file I18n.cpp.

◆ registerEnglishTable()

void cdc::ui::I18n::registerEnglishTable ( const I18nEntry * entries,
std::size_t count )

Append English entries to the lookup table.

Typically called once per module from its init(). Entries are stored by-pointer (no copy); the caller must keep the array alive for the lifetime of the firmware (rodata is fine).

Parameters
entriesPointer to the first entry.
countNumber of entries.

Definition at line 307 of file I18n.cpp.

Referenced by cdc::mod_2fa::registerStrings(), cdc::mod_fido2::registerStrings(), cdc::mod_gpg::registerStrings(), cdc::mod_password::registerStrings(), cdc::mod_sao::registerStrings(), and cdc::mod_vcard::registerStrings().

◆ setLanguageCode()

bool cdc::ui::I18n::setLanguageCode ( const char * code)

Set the active language by code.

If the overlay does not contain the requested language, the call still succeeds and persists the choice, but tr() will fall back to English until an overlay covering the language is loaded.

Parameters
codeLanguage code (e.g. "en", "de"). Empty defaults to "en".
Returns
true on success.

Definition at line 370 of file I18n.cpp.

References LOG_I, and cdc::ui::TAG.

Referenced by cdc::ui::onLanguageSelect().

◆ setOnLanguageChanged()

void cdc::ui::I18n::setOnLanguageChanged ( LanguageChangedCallback cb)
inline

Definition at line 172 of file I18n.h.

Referenced by cdc::ui::ui_init().

◆ tr()

const char * cdc::ui::I18n::tr ( const char * key) const

Look up a translation by key.

Parameters
keyStable string key. Must not be null.
Returns
Pointer to translation text. Never null - returns "?<key>" if no match.

Definition at line 358 of file I18n.cpp.

Referenced by cdc::ui::tr().

Member Data Documentation

◆ OVERLAY_DIR

const char* cdc::ui::I18n::OVERLAY_DIR = "/plugins/i18n"
staticconstexpr

Directory on the plugins FAT holding the per-language files.

Definition at line 69 of file I18n.h.


The documentation for this class was generated from the following files:
  • components/cdc_ui/include/cdc_ui/I18n.h
  • components/cdc_ui/src/I18n.cpp