CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_i18n.cpp
Go to the documentation of this file.
1
13
16#include "cdc_ui/I18n.h"
17#include "cdc_log.h"
18
19#include <cstring>
20
21extern "C" void* plg_get_active_plugin(void);
22
23namespace {
24
26{
28}
29
30uint8_t lang_index()
31{
32 const std::string& code = cdc::ui::I18n::instance().getLanguageCode();
33 if (code == "de") return HOST_LANG_DE;
34 return HOST_LANG_EN;
35}
36
37const std::string* lookup_localised(
38 const std::map<std::string, cdc::plugin_manager::LocalizedString>& table,
39 const std::string& key, const std::string& default_lang)
40{
41 auto it = table.find(key);
42 if (it == table.end()) return nullptr;
43 uint8_t lang = lang_index();
44 const char* lang_name = (lang == HOST_LANG_DE) ? "de" : "en";
45 auto by_lang_it = it->second.by_lang.find(lang_name);
46 if (by_lang_it != it->second.by_lang.end()) return &by_lang_it->second;
47 by_lang_it = it->second.by_lang.find(default_lang);
48 if (by_lang_it != it->second.by_lang.end()) return &by_lang_it->second;
49 if (!it->second.by_lang.empty()) return &it->second.by_lang.begin()->second;
50 return nullptr;
51}
52
53int copy_to_buffer(const char* src, char* out, uint32_t out_cap)
54{
55 if (!out || out_cap == 0) return -1;
56 if (!src) src = "";
57 size_t len = std::strlen(src);
58 size_t to_copy = (len < out_cap - 1) ? len : (out_cap - 1);
59 std::memcpy(out, src, to_copy);
60 out[to_copy] = '\0';
61 return static_cast<int>(to_copy);
62}
63
64} // namespace
65
66extern "C" {
67
68int host_i18n_tr_key(const char* key, char* out, uint32_t out_cap)
69{
70 if (!out || out_cap == 0) return -1;
71 auto* p = active();
72 if (!p || !key) return copy_to_buffer("", out, out_cap);
73 if (const char* s = p->trKey(key)) return copy_to_buffer(s, out, out_cap);
74 const auto& mf = p->manifest();
75 const std::string* s = lookup_localised(mf.i18n_strings, key, mf.default_language);
76 return copy_to_buffer(s ? s->c_str() : "", out, out_cap);
77}
78
79int host_i18n_tr_meta(const char* field, char* out, uint32_t out_cap)
80{
81 if (!out || out_cap == 0) return -1;
82 auto* p = active();
83 if (!p || !field) {
84 LOG_W("I18N", "tr_meta: no active plugin (%p) or field (%p)", p, field);
85 return copy_to_buffer("", out, out_cap);
86 }
87 {
88 std::string composed = "meta.";
89 composed += field;
90 if (const char* s = p->trKey(composed.c_str())) return copy_to_buffer(s, out, out_cap);
91 }
92 const auto& mf = p->manifest();
93 const std::string* s = lookup_localised(mf.i18n_meta, field, mf.default_language);
94 if (!s) {
95 LOG_W("I18N", "tr_meta('%s'): no entry; meta size=%u, default_lang='%s'",
96 field, static_cast<unsigned>(mf.i18n_meta.size()), mf.default_language.c_str());
97 }
98 return copy_to_buffer(s ? s->c_str() : "", out, out_cap);
99}
100
102{
103 return lang_index();
104}
105
106int host_i18n_tr_core(const char* key, char* out, uint32_t out_cap)
107{
108 if (!out || out_cap == 0) return -1;
109 if (!key) return copy_to_buffer("", out, out_cap);
110 return copy_to_buffer(cdc::ui::I18n::instance().tr(key), out, out_cap);
111}
112
113} // extern "C"
Internationalization with English fallbacks in code and overlay translations loaded at runtime from a...
Owned WAMR module instance + per-plugin state.
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_W(tag, fmt,...)
Definition cdc_log.h:146
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
#define HOST_LANG_EN
Definition host_api.h:1177
#define HOST_LANG_DE
Definition host_api.h:1178
uint8_t host_i18n_current_language(void)
Active language code (HOST_LANG_*).
int host_i18n_tr_core(const char *key, char *out, uint32_t out_cap)
Translate a core.* key from the firmware string table.
int host_i18n_tr_meta(const char *field, char *out, uint32_t out_cap)
Read a metadata field (name, description, ...) from the plugin manifest.
int host_i18n_tr_key(const char *key, char *out, uint32_t out_cap)
Translate a plugin-local key into the current language.
CDC Badge OS plugin host API - canonical C ABI contract.
void * plg_get_active_plugin(void)