afegit fixer de locales (textos centralitzats)
This commit is contained in:
57
source/core/locale/locale.cpp
Normal file
57
source/core/locale/locale.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "core/locale/locale.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "core/jail/jfile.hpp"
|
||||
#include "external/fkyaml_node.hpp"
|
||||
|
||||
namespace Locale {
|
||||
|
||||
static std::unordered_map<std::string, std::string> strings_;
|
||||
|
||||
// Aplana un node YAML en claus amb notació punt
|
||||
static void traverse(const fkyaml::node& node, const std::string& prefix) {
|
||||
if (node.is_mapping()) {
|
||||
for (auto it = node.begin(); it != node.end(); ++it) {
|
||||
std::string key = it.key().get_value<std::string>();
|
||||
std::string full = prefix.empty() ? key : prefix + "." + key;
|
||||
traverse(it.value(), full);
|
||||
}
|
||||
} else if (node.is_scalar()) {
|
||||
try {
|
||||
strings_[prefix] = node.get_value<std::string>();
|
||||
} catch (...) {}
|
||||
}
|
||||
}
|
||||
|
||||
bool load(const char* filename) {
|
||||
int size = 0;
|
||||
char* buffer = file_getfilebuffer(filename, size, true);
|
||||
if (!buffer || size <= 0) {
|
||||
std::cerr << "Locale: unable to load " << filename << '\n';
|
||||
return false;
|
||||
}
|
||||
std::string content(buffer, size);
|
||||
free(buffer);
|
||||
|
||||
try {
|
||||
auto yaml = fkyaml::node::deserialize(content);
|
||||
strings_.clear();
|
||||
traverse(yaml, "");
|
||||
std::cout << "Locale loaded: " << strings_.size() << " string(s) from " << filename << '\n';
|
||||
return true;
|
||||
} catch (const fkyaml::exception& e) {
|
||||
std::cerr << "Locale: error parsing " << filename << ": " << e.what() << '\n';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
auto get(const char* key) -> const char* {
|
||||
auto it = strings_.find(key);
|
||||
if (it != strings_.end()) return it->second.c_str();
|
||||
return key; // fallback: retorna la clau mateixa
|
||||
}
|
||||
|
||||
} // namespace Locale
|
||||
Reference in New Issue
Block a user