From ed3724193eb157f79605e1d6a0e69798cd5b574c Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 30 Mar 2026 17:59:59 +0200 Subject: [PATCH] fix: no es llegien els fitxers de Locale desde resources.pack --- source/core/locale/locale.cpp | 25 +++++++++++++++++++++++++ source/core/locale/locale.hpp | 8 +++++--- source/core/system/director.cpp | 9 +++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/source/core/locale/locale.cpp b/source/core/locale/locale.cpp index 9aa05ed61..a5a340d78 100644 --- a/source/core/locale/locale.cpp +++ b/source/core/locale/locale.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "external/fkyaml_node.hpp" // Para fkyaml::node @@ -15,6 +16,12 @@ void Locale::init(const std::string& file_path) { // NOLINT(readability-convert Locale::instance->loadFromFile(file_path); } +// [SINGLETON] Crea el objeto desde contenido en memoria (para release con pack) +void Locale::initFromContent(const std::string& content) { // NOLINT(readability-convert-member-functions-to-static) + Locale::instance = new Locale(); + Locale::instance->loadFromContent(content); +} + // [SINGLETON] Destruye el objeto con esta función estática void Locale::destroy() { delete Locale::instance; @@ -55,6 +62,24 @@ void Locale::flatten(const void* node_ptr, const std::string& prefix) { // NOLI } } +// Carga las traducciones desde contenido YAML en memoria +void Locale::loadFromContent(const std::string& content) { // NOLINT(readability-convert-member-functions-to-static) + if (content.empty()) { + std::cerr << "Locale: contenido vacío, sin traducciones cargadas\n"; + return; + } + + try { + std::istringstream stream(content); + auto yaml = fkyaml::node::deserialize(stream); + flatten(&yaml, ""); + + std::cout << "Locale: " << strings_.size() << " traducciones cargadas desde pack\n"; + } catch (const fkyaml::exception& e) { + std::cerr << "Locale: error al parsear YAML: " << e.what() << '\n'; + } +} + // Carga las traducciones desde el fichero YAML indicado void Locale::loadFromFile(const std::string& file_path) { // NOLINT(readability-convert-member-functions-to-static) if (file_path.empty()) { diff --git a/source/core/locale/locale.hpp b/source/core/locale/locale.hpp index 29c31b316..870479e8e 100644 --- a/source/core/locale/locale.hpp +++ b/source/core/locale/locale.hpp @@ -8,9 +8,10 @@ // No se permite cambio de idioma en caliente. class Locale { public: - static void init(const std::string& file_path); // Crea e inicializa el singleton - static void destroy(); // Destruye el singleton - static auto get() -> Locale*; // Devuelve el singleton + static void init(const std::string& file_path); // Crea e inicializa el singleton + static void initFromContent(const std::string& content); // Crea e inicializa desde contenido en memoria (pack) + static void destroy(); // Destruye el singleton + static auto get() -> Locale*; // Devuelve el singleton // Devuelve la traducción de la clave dada. // Si la clave no existe, devuelve la propia clave como fallback. @@ -19,6 +20,7 @@ class Locale { private: Locale() = default; void loadFromFile(const std::string& file_path); + void loadFromContent(const std::string& content); void flatten(const void* node_ptr, const std::string& prefix); // Aplana nodos YAML anidados static Locale* instance; diff --git a/source/core/system/director.cpp b/source/core/system/director.cpp index f1188b209..217609a28 100644 --- a/source/core/system/director.cpp +++ b/source/core/system/director.cpp @@ -189,8 +189,13 @@ Director::Director() { // Inicializa el sistema de localización (antes de Cheevos que usa textos traducidos) #ifdef RELEASE_BUILD - std::string locale_path = executable_path_ + PREFIX + "/data/locale/" + Options::language + ".yaml"; - Locale::init(locale_path); + { + // En release el locale está en el pack, no en el filesystem + std::string locale_key = Resource::List::get()->get(Options::language + ".yaml"); // NOLINT(readability-static-accessed-through-instance) + auto locale_bytes = Resource::Helper::loadFile(locale_key); + std::string locale_content(locale_bytes.begin(), locale_bytes.end()); + Locale::initFromContent(locale_content); + } #else Locale::init(Resource::List::get()->get(Options::language + ".yaml")); // NOLINT(readability-static-accessed-through-instance) #endif