2021-12-05 17:54:36 +00:00
|
|
|
#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;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::ifstream file(m_LocalePath);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!file.good()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::stringstream data;
|
|
|
|
data << file.rdbuf();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (data.str().empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* doc = new tinyxml2::XMLDocument();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (doc == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (doc->Parse(data.str().c_str(), data.str().size()) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::hash<std::string> hash;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* localization = doc->FirstChildElement("localization");
|
|
|
|
auto* phrases = localization->FirstChildElement("phrases");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* phrase = phrases->FirstChildElement("phrase");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
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");
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
file.close();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
delete doc;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dLocale::~dLocale() = default;
|
|
|
|
|
|
|
|
std::string dLocale::GetTemplate(const std::string& phraseID) {
|
2022-07-28 13:39:57 +00:00
|
|
|
return "%[" + phraseID + "]";
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*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];
|
|
|
|
}*/
|