#include "core/locale/locale.hpp" #include #include #include "core/resources/resource_helper.hpp" #include "external/fkyaml_node.hpp" #include "utils/log.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()) { Log::error("Locale", "no s'ha pogut obrir {}", file_path); 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_); Log::info("Locale", "{} traduccions des de {}", strings_.size(), file_path); } catch (const std::exception& e) { Log::error("Locale", "error parsejant {}: {}", file_path, e.what()); } } auto Locale::text(const std::string& key) const -> std::string { auto it = strings_.find(key); if (it != strings_.end()) { return it->second; } Log::warn("Locale", "clau no trobada: {}", key); return key; }