Add ruby bindings for internal structures, allow better controlled localization

This commit is contained in:
2026-05-06 22:01:48 +01:00
parent 3894b90398
commit 65109cc3c4
8 changed files with 1047 additions and 114 deletions
+83 -9
View File
@@ -17,20 +17,87 @@ inline JoinedKey join_keys(LanguageCode lang_code, Key loc_key) {
return (static_cast<JoinedKey>(lang_code) << 16) | loc_key;
}
inline std::unordered_map<JoinedKey, std::string> localized_strings;
struct Segment {
bool is_var;
std::string text;
};
inline std::string get(Key key) {
auto it = localized_strings.find(join_keys(current_language, key));
if (it != localized_strings.end())
inline std::unordered_map<JoinedKey, std::vector<Segment>> localized_strings;
inline std::unordered_map<std::string, mrb_sym> var_sym_cache;
inline mrb_sym get_var_sym(mrb_state *mrb, const std::string &name) {
auto it = var_sym_cache.find(name);
if (it != var_sym_cache.end())
return it->second;
for (const auto &[joined, value] : localized_strings) {
Key k = (Key)(joined & 0xFFFF);
if (k == key)
return value;
mrb_sym sym = mrb_intern_cstr(mrb, name.c_str());
var_sym_cache[name] = sym;
return sym;
}
inline std::vector<Segment> parse_localized(const std::string &input) {
std::vector<Segment> out;
size_t i = 0;
while (i < input.size()) {
size_t start = input.find("%{", i);
if (start == std::string::npos) {
out.push_back({false, input.substr(i)});
break;
}
if (start > i) {
out.push_back({false, input.substr(i, start - i)});
}
size_t end = input.find("}", start);
if (end == std::string::npos) {
// malformed fallback
out.push_back({false, input.substr(start)});
break;
}
std::string var = input.substr(start + 2, end - (start + 2));
out.push_back({true, var});
i = end + 1;
}
return "Localization not set!";
return out;
}
inline void set(LanguageCode lang_code, Key key, const std::string &text) {
localized_strings[join_keys(lang_code, key)] = parse_localized(text);
}
inline std::string get(mrb_state *mrb, Key key, mrb_value hash) {
auto it = localized_strings.find(join_keys(current_language, key));
if (it == localized_strings.end())
return "Localization not set!";
const auto &entry = it->second;
std::string result;
result.reserve(10);
for (const auto &seg : entry) {
if (!seg.is_var) {
result += seg.text;
} else {
mrb_sym sym = get_var_sym(mrb, seg.text);
if (mrb_nil_p(hash) || mrb_nil_p(mrb_hash_get(mrb, hash, mrb_symbol_value(sym)))) {
result += "%{" + seg.text + "}";
continue;
}
mrb_value val = mrb_hash_get(mrb, hash, mrb_symbol_value(sym));
if (mrb_nil_p(val))
continue;
result += mrb_str_to_cstr(mrb, val);
}
}
return result;
}
inline LanguageCode lang_from_sym(mrb_state *mrb, mrb_sym sym) {
@@ -49,6 +116,13 @@ inline LanguageCode lang_from_sym(mrb_state *mrb, mrb_sym sym) {
return code;
}
inline mrb_sym lang_to_sym(mrb_state *mrb, LanguageCode code) {
char s[5] = {0};
for (int i = 0; i < 4; ++i)
s[i] = (code >> (8 * (3 - i))) & 0xFF;
return mrb_intern_cstr(mrb, s);
}
inline Key key_from_sym(mrb_state *mrb, mrb_sym sym) {
const char *key_name = mrb_sym2name(mrb, sym);