#include "core/locale/locale.hpp" #include #include #include #include "core/resources/resource_helper.hpp" #include "external/fkyaml_node.hpp" namespace { // Recorre el node YAML i aplana jerarquies en claus "a.b.c". Claus no // cadena (p.ex. llistes o nombres solts) es ignoren silenciosament — // només textos útils per traduir acaben al mapping. void flatten(const fkyaml::node& node, const std::string& prefix, std::unordered_map& out) { for (auto it = node.begin(); it != node.end(); ++it) { const std::string KEY = prefix.empty() ? it.key().get_value() : prefix + "." + it.key().get_value(); const auto& value = it.value(); if (value.is_mapping()) { flatten(value, KEY, out); } else if (value.is_string()) { out[KEY] = value.get_value(); } } } } // namespace auto Locale::get() -> Locale& { static Locale instance_; return instance_; } void Locale::load(const std::string& file_path) { auto bytes = Resource::Helper::loadFile(file_path); if (bytes.empty()) { std::cerr << "Locale: no s'ha pogut obrir " << file_path << '\n'; return; } try { const auto* begin = reinterpret_cast(bytes.data()); const auto* end = begin + bytes.size(); auto yaml = fkyaml::node::deserialize(begin, end); strings_.clear(); flatten(yaml, "", strings_); std::cout << "Locale: " << strings_.size() << " traduccions des de " << file_path << '\n'; } catch (const std::exception& e) { std::cerr << "Locale: error parsejant " << file_path << ": " << e.what() << '\n'; } } auto Locale::text(const std::string& key) const -> std::string { auto it = strings_.find(key); if (it != strings_.end()) { return it->second; } std::cerr << "Locale: clau no trobada: " << key << '\n'; return key; }