#include "LocalizationData.hpp" void LocalizationData::LoadFromPath(const std::filesystem::path& path) { auto cfgPath = path / "config.cfg"; if (!std::filesystem::exists(cfgPath)) { std::stringstream ss; ss << "Config file 'config.cfg' does not exist in path " << path; throw std::logic_error(ss.str()); } std::ifstream cfgFile; cfgFile.open(cfgPath, std::ios::in); if (!cfgFile.is_open()) { throw std::logic_error("Something went wrong loading config file."); } std::string line; while (std::getline(cfgFile, line)) { std::string key; std::string value; std::stringstream linestream(line); std::getline(linestream, key, '='); std::getline(linestream, value, '='); if (key == "language-code") _code = value; else if (key == "language-display") _display = value; else if (key == "global-path") _globalPath = value; else if (key == "temp-path") _tempPath = value; } cfgFile.close(); std::cout << "Language code: " << _code << std::endl; std::cout << "Language Display: " << _display << std::endl; std::cout << "Globals Path: " << _globalPath << std::endl; std::cout << "Temp Path: " << _tempPath << std::endl; for (const auto& p : std::filesystem::recursive_directory_iterator(path / _globalPath)) { if (p.path().extension() != ".csv") continue; LocalizationFile file; file.LoadFile(p); _globalFiles[p.path().filename().stem().string()] = file; } for (const auto& p : std::filesystem::recursive_directory_iterator(path / _tempPath)) { if (p.path().extension() != ".csv") continue; LocalizationFile file; file.LoadFile(p); _tempFiles[p.path().filename().stem().string()] = file; } } void LocalizationData::WriteToFile(const std::filesystem::path& path) { std::ofstream outFile; outFile.open(path, std::ios::out | std::ios::trunc); if (!outFile.is_open()) { std::stringstream ss; ss << "Something went wrong opening output file " << path; throw std::logic_error(ss.str()); } outFile << _code << "~" << _display << std::endl; outFile << "global" << std::endl; std::unordered_map globalFilesPositions; for (const auto& file : _globalFiles) { outFile << file.first << "~"; auto p1 = outFile.tellp(); outFile << "0000~0000" << std::endl; globalFilesPositions[file.first] = p1; } outFile << "temp" << std::endl; std::unordered_map tempFilesPositions; for (const auto& file : _tempFiles) { outFile << file.first << "~"; auto p1 = outFile.tellp(); outFile << "0000~0000" << std::endl; tempFilesPositions[file.first] = p1; } outFile << "ENDDATA" << std::endl; // actual localization for (const auto& file : _globalFiles) { auto pos = outFile.tellp(); for (const auto& kv : file.second.GetMap()) { outFile << kv.first << "|" << kv.second << std::endl; } auto end = outFile.tellp(); auto length = end - pos; auto data = globalFilesPositions[file.first]; outFile.seekp(data); outFile << std::setfill ('0') << std::setw(4) << std::hex << pos; outFile << "~"; outFile << std::setfill ('0') << std::setw(4) << std::hex << length; outFile.seekp(end); } for (const auto& file : _tempFiles) { auto pos = outFile.tellp(); for (const auto& kv : file.second.GetMap()) { outFile << kv.first << "|" << kv.second << std::endl; } auto end = outFile.tellp(); auto length = end - pos; auto data = tempFilesPositions[file.first]; outFile.seekp(data); outFile << std::setfill ('0') << std::setw(4) << std::hex << pos; outFile << "~"; outFile << std::setfill ('0') << std::setw(4) << std::hex << length; outFile.seekp(end); } outFile.close(); std::cout << "Wrote language file to " << path << std::endl; }