lang: convertits els fitxers de text a json
This commit is contained in:
@@ -1,51 +1,57 @@
|
||||
#include "lang.h"
|
||||
#include <fstream> // Para basic_ifstream, basic_istream, ifstream
|
||||
#include <vector> // Para vector
|
||||
#include <fstream>
|
||||
#include <unordered_map>
|
||||
#include "json.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace lang
|
||||
{
|
||||
std::vector<std::string> texts; // Vector con los textos
|
||||
std::unordered_map<std::string, std::string> texts;
|
||||
|
||||
// Inicializa los textos del juego en el idioma seleccionado
|
||||
bool loadFromFile(const std::string &file_path)
|
||||
{
|
||||
texts.clear();
|
||||
|
||||
bool success = false;
|
||||
|
||||
std::ifstream rfile(file_path);
|
||||
if (rfile.is_open() && rfile.good())
|
||||
{
|
||||
success = true;
|
||||
std::string line;
|
||||
if (!rfile.is_open())
|
||||
return false;
|
||||
|
||||
// Lee el resto de datos del fichero
|
||||
while (std::getline(rfile, line))
|
||||
try
|
||||
{
|
||||
json j;
|
||||
rfile >> j;
|
||||
|
||||
for (auto &el : j.items())
|
||||
{
|
||||
// Almacena solo las lineas que no empiezan por # o no esten vacias
|
||||
const bool TEST1 = line.substr(0, 1) != "#";
|
||||
const bool TEST2 = !line.empty();
|
||||
if (TEST1 && TEST2)
|
||||
{
|
||||
texts.push_back(line);
|
||||
}
|
||||
};
|
||||
texts[el.key()] = el.value();
|
||||
}
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
// Puedes loguear el error si quieres
|
||||
return false;
|
||||
}
|
||||
|
||||
return success;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Obtiene la cadena de texto del indice
|
||||
std::string getText(int index)
|
||||
// Obtiene el texto por clave
|
||||
std::string getText(const std::string &key)
|
||||
{
|
||||
return texts.at(index);
|
||||
auto it = texts.find(key);
|
||||
if (it != texts.end())
|
||||
return it->second;
|
||||
else
|
||||
return "[missing text: " + key + "]";
|
||||
}
|
||||
|
||||
// Obtiene el codigo del idioma del siguiente idioma
|
||||
// Obtiene el código del siguiente idioma disponible
|
||||
Code getNextLangCode(Code lang)
|
||||
{
|
||||
auto index = static_cast<int>(lang);
|
||||
index = (index + 1) % 3;
|
||||
return static_cast<Code>(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user