DarkflameServer/dGame/dUtilities/dLocale.cpp

82 lines
1.6 KiB
C++
Raw Normal View History

#include "dLocale.h"
#include <clocale>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include "tinyxml2.h"
#include "Game.h"
#include "dConfig.h"
dLocale::dLocale() {
2022-07-28 13:39:57 +00:00
if (Game::config->GetValue("locale_enabled") != "1") {
return;
}
2022-07-28 13:39:57 +00:00
std::ifstream file(m_LocalePath);
2022-07-28 13:39:57 +00:00
if (!file.good()) {
return;
}
2022-07-28 13:39:57 +00:00
std::stringstream data;
data << file.rdbuf();
2022-07-28 13:39:57 +00:00
if (data.str().empty()) {
return;
}
2022-07-28 13:39:57 +00:00
auto* doc = new tinyxml2::XMLDocument();
2022-07-28 13:39:57 +00:00
if (doc == nullptr) {
return;
}
2022-07-28 13:39:57 +00:00
if (doc->Parse(data.str().c_str(), data.str().size()) != 0) {
return;
}
2022-07-28 13:39:57 +00:00
std::hash<std::string> hash;
2022-07-28 13:39:57 +00:00
auto* localization = doc->FirstChildElement("localization");
auto* phrases = localization->FirstChildElement("phrases");
2022-07-28 13:39:57 +00:00
auto* phrase = phrases->FirstChildElement("phrase");
2022-07-28 13:39:57 +00:00
while (phrase != nullptr) {
// Add the phrase hash to the vector
m_Phrases.push_back(hash(phrase->Attribute("id")));
phrase = phrase->NextSiblingElement("phrase");
}
2022-07-28 13:39:57 +00:00
file.close();
2022-07-28 13:39:57 +00:00
delete doc;
}
dLocale::~dLocale() = default;
std::string dLocale::GetTemplate(const std::string& phraseID) {
2022-07-28 13:39:57 +00:00
return "%[" + phraseID + "]";
}
bool dLocale::HasPhrase(const std::string& phraseID) {
2022-07-28 13:39:57 +00:00
if (Game::config->GetValue("locale_enabled") != "1") {
return true;
}
// Compute the hash and see if it's in the vector
std::hash<std::string> hash;
std::size_t hashValue = hash(phraseID);
return std::find(m_Phrases.begin(), m_Phrases.end(), hashValue) != m_Phrases.end();
}
/*std::string dLocale::GetPhrase(const std::string& phraseID) {
2022-07-28 13:39:57 +00:00
if (m_Phrases.find(phraseID) == m_Phrases.end()) {
return "";
}
return m_Phrases[phraseID];
}*/