50 lines
1.5 KiB
C++
50 lines
1.5 KiB
C++
#ifndef LOCALIZATION_H
|
|
#define LOCALIZATION_H
|
|
|
|
#include "pch.h"
|
|
#include "utils.h"
|
|
|
|
namespace Localization {
|
|
using Key = uint16_t;
|
|
|
|
// Maps localization keys to their string identifiers. This is used for text elements to look up the correct localized string.
|
|
inline std::unordered_map<std::string, Key> localization_key_map;
|
|
|
|
using LanguageCode = uint32_t; // 32-bit integer to store the language code (e.g., "en", "fr", "es")
|
|
|
|
inline LanguageCode current_language = 'e' << 24 | 'n' << 16;
|
|
|
|
using JoinedKey = uint64_t; // 64-bit integer to store the joined language code and key
|
|
|
|
inline JoinedKey join_keys(LanguageCode lang_code, Key loc_key) {
|
|
return (static_cast<JoinedKey>(lang_code) << 16) | loc_key;
|
|
}
|
|
|
|
struct Segment {
|
|
bool is_var;
|
|
std::string text;
|
|
};
|
|
|
|
inline std::unordered_map<JoinedKey, std::vector<Segment>> localized_strings;
|
|
inline std::unordered_map<std::string, mrb_sym> var_sym_cache;
|
|
|
|
std::vector<Segment> parse_localized(const std::string &input);
|
|
|
|
inline void set(LanguageCode lang_code, Key key, const std::string &text) {
|
|
localized_strings[join_keys(lang_code, key)] = parse_localized(text);
|
|
}
|
|
|
|
mrb_sym get_var_sym(mrb_state *mrb, const std::string &name);
|
|
|
|
std::string get(Key key, const std::unordered_map<std::string, std::string> ¶ms);
|
|
|
|
LanguageCode lang_from_sym(mrb_state *mrb, mrb_sym sym);
|
|
|
|
mrb_sym lang_to_sym(mrb_state *mrb, LanguageCode code);
|
|
|
|
Key key_from_sym(mrb_state *mrb, mrb_sym sym);
|
|
|
|
std::string get_key_name(Key key);
|
|
} // namespace Localization
|
|
|
|
#endif |