Locale amb català (defecte) i anglès; Options.locale persistit a config.yaml (v0.10.0)

This commit is contained in:
JailDesigner committed 2026-04-19 12:11:42 +02:00
1 parent 1b430500f4
commit 837eb73398
11 files changed
+119 -3

No files matched your search

+1
View File
@@ -33,6 +33,7 @@ configure_file(${CMAKE_SOURCE_DIR}/source/version.h.in ${CMAKE_BINARY_DIR}/versi
set(APP_SOURCES set(APP_SOURCES
source/main.cpp source/main.cpp
source/core/system/director.cpp source/core/system/director.cpp
source/core/locale/locale.cpp
source/core/rendering/screen.cpp source/core/rendering/screen.cpp
source/core/rendering/notifier.cpp source/core/rendering/notifier.cpp
source/core/rendering/render_info.cpp source/core/rendering/render_info.cpp
+5
View File
@@ -0,0 +1,5 @@
# AEE 2026 — Català
# lang: ca
notification:
test: "notificacio de prova"
+5
View File
@@ -0,0 +1,5 @@
# AEE 2026 — English
# lang: en
notification:
test: "test notification"
+57
View File
@@ -0,0 +1,57 @@
#include "core/locale/locale.hpp"
#include <fstream>
#include <iostream>
#include <string>
#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<std::string, std::string>& out) {
for (auto it = node.begin(); it != node.end(); ++it) {
const std::string KEY = prefix.empty()
? it.key().get_value<std::string>()
: prefix + "." + it.key().get_value<std::string>();
const auto& value = it.value();
if (value.is_mapping()) {
flatten(value, KEY, out);
} else if (value.is_string()) {
out[KEY] = value.get_value<std::string>();
}
}
}
} // namespace
auto Locale::get() -> Locale& {
static Locale instance_;
return instance_;
}
void Locale::load(const std::string& file_path) {
std::ifstream file(file_path);
if (!file.is_open()) {
std::cerr << "Locale: no s'ha pogut obrir " << file_path << '\n';
return;
}
try {
auto yaml = fkyaml::node::deserialize(file);
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;
}
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <string>
#include <unordered_map>
// Locale amb claus en notació de punts ("notification.test"). El YAML pot ser
// jerarquitzat però s'aplana en càrrega, així el consumidor només veu claus
// planes. No hi ha hot-swap d'idioma — l'idioma es fixa a l'arrencada via
// Options i es recàrrega només amb reinici del joc.
class Locale {
public:
static auto get() -> Locale&;
Locale(const Locale&) = delete;
Locale(Locale&&) = delete;
auto operator=(const Locale&) -> Locale& = delete;
auto operator=(Locale&&) -> Locale& = delete;
// Llig el fitxer YAML i emplena el mapping intern. Si hi ha un error de
// parse o el fitxer no existeix, deixa el mapping com estava i ho
// notifica per stderr.
void load(const std::string& file_path);
// Retorna la traducció; si la clau no existeix, retorna la pròpia clau
// com a fallback visible (així una clau mal escrita és detectable sense
// trencar el render).
[[nodiscard]] auto text(const std::string& key) const -> std::string;
private:
Locale() = default;
~Locale() = default;
std::unordered_map<std::string, std::string> strings_;
};
+5
View File
@@ -10,6 +10,7 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "core/locale/locale.hpp"
#include "core/rendering/notifier.hpp" #include "core/rendering/notifier.hpp"
#include "core/rendering/render_info.hpp" #include "core/rendering/render_info.hpp"
#include "core/rendering/screen.hpp" #include "core/rendering/screen.hpp"
@@ -54,6 +55,10 @@ Director::Director() {
Screen::get().applyCrtPiParams(Options::buildCrtPiParams()); Screen::get().applyCrtPiParams(Options::buildCrtPiParams());
(void)Resource::Cache::get(); (void)Resource::Cache::get();
// Traduccions per a tots els textos visibles. Si el fitxer no existeix
// o hi ha un error de parse, Locale::text() caurà a retornar la clau.
Locale::get().load(std::string(Defaults::I18n::LOCALE_DIR) + Options::locale + ".yaml");
// Recursos compartits d'UI (paleta + font). Es carreguen abans que els // Recursos compartits d'UI (paleta + font). Es carreguen abans que els
// overlays perquè tots els reaprofiten sense duplicar la lògica. // overlays perquè tots els reaprofiten sense duplicar la lògica.
Resource::Cache::get().loadPalette("ui", "data/palettes/sega-cube.pal"); Resource::Cache::get().loadPalette("ui", "data/palettes/sega-cube.pal");
+3 -1
View File
@@ -258,6 +258,7 @@ namespace Options {
if (file_version != Texts::VERSION) { return saveToFile(); } if (file_version != Texts::VERSION) { return saveToFile(); }
config_version = file_version; config_version = file_version;
if (yaml.contains("locale")) { Options::locale = yaml["locale"].get_value<std::string>(); }
parseWindow(yaml); parseWindow(yaml);
parseVideo(yaml); parseVideo(yaml);
parseAudio(yaml); parseAudio(yaml);
@@ -276,7 +277,8 @@ namespace Options {
std::ostringstream out; std::ostringstream out;
out << "# Configuració d'AEE 2026. Generat automàticament; editable a mà.\n"; out << "# Configuració d'AEE 2026. Generat automàticament; editable a mà.\n";
out << "version: \"" << config_version << "\"\n\n"; out << "version: \"" << config_version << "\"\n";
out << "locale: \"" << locale << "\"\n\n";
out << "window:\n"; out << "window:\n";
out << " zoom: " << window.zoom << "\n"; out << " zoom: " << window.zoom << "\n";
+1
View File
@@ -94,6 +94,7 @@ namespace Options {
inline std::string config_file_path{}; inline std::string config_file_path{};
inline std::string postfx_file_path{}; inline std::string postfx_file_path{};
inline std::string crtpi_file_path{}; inline std::string crtpi_file_path{};
inline std::string locale{Defaults::I18n::LOCALE};
inline Window window{}; inline Window window{};
inline Video video{}; inline Video video{};
inline Audio audio{}; inline Audio audio{};
+2 -1
View File
@@ -2,6 +2,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include "core/locale/locale.hpp"
#include "core/rendering/notifier.hpp" #include "core/rendering/notifier.hpp"
#include "core/rendering/render_info.hpp" #include "core/rendering/render_info.hpp"
#include "core/rendering/screen.hpp" #include "core/rendering/screen.hpp"
@@ -36,7 +37,7 @@ void GameScene::update(int delta_ms) {
if (event.type == SDL_EVENT_KEY_DOWN) { if (event.type == SDL_EVENT_KEY_DOWN) {
if (event.key.scancode == SDL_SCANCODE_ESCAPE) { wants_quit_ = true; } if (event.key.scancode == SDL_SCANCODE_ESCAPE) { wants_quit_ = true; }
if (event.key.scancode == SDL_SCANCODE_F3) { RenderInfo::get().toggle(); } if (event.key.scancode == SDL_SCANCODE_F3) { RenderInfo::get().toggle(); }
if (event.key.scancode == SDL_SCANCODE_F4) { Notifier::get().notify("notificacio de prova"); } if (event.key.scancode == SDL_SCANCODE_F4) { Notifier::get().notify(Locale::get().text("notification.test")); }
} }
} }
+5
View File
@@ -28,6 +28,11 @@ namespace Defaults {
constexpr bool FULLSCREEN = false; constexpr bool FULLSCREEN = false;
} // namespace Window } // namespace Window
namespace I18n {
constexpr const char* LOCALE = "ca"; // Idioma per defecte (codi ISO 639-1).
constexpr const char* LOCALE_DIR = "data/locale/"; // Carpeta amb els YAML per idioma.
} // namespace I18n
namespace Video { namespace Video {
constexpr bool VSYNC = true; constexpr bool VSYNC = true;
constexpr ScalingMode SCALING_MODE = ScalingMode::LETTERBOX; constexpr ScalingMode SCALING_MODE = ScalingMode::LETTERBOX;
+1 -1
View File
@@ -3,7 +3,7 @@
namespace Texts { namespace Texts {
constexpr const char* WINDOW_TITLE = "© 2026 Aventures en Egipte — JailDesigner"; constexpr const char* WINDOW_TITLE = "© 2026 Aventures en Egipte — JailDesigner";
constexpr const char* COPYRIGHT = "© 2026 JailDesigner"; constexpr const char* COPYRIGHT = "© 2026 JailDesigner";
constexpr const char* VERSION = "0.9.0"; // Single source of truth — Makefile el grep constexpr const char* VERSION = "0.10.0"; // Single source of truth — Makefile el grep
} // namespace Texts } // namespace Texts
namespace GameCanvas { namespace GameCanvas {