Files
coffee_crisis_arcade_edition/source/lang.cpp

58 lines
1.2 KiB
C++

#include "lang.h"
#include <fstream>
#include <unordered_map>
#include "json.hpp"
using json = nlohmann::json;
namespace lang
{
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();
std::ifstream rfile(file_path);
if (!rfile.is_open())
return false;
try
{
json j;
rfile >> j;
for (auto &el : j.items())
{
texts[el.key()] = el.value();
}
}
catch (const std::exception &e)
{
// Puedes loguear el error si quieres
return false;
}
return true;
}
// Obtiene el texto por clave
std::string getText(const std::string &key)
{
auto it = texts.find(key);
if (it != texts.end())
return it->second;
else
return "[missing text: " + key + "]";
}
// 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);
}
}