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

+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_;
};