HintsToPot.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <boost/filesystem.hpp>
  5. #include <boost/dll.hpp>
  6. #include <boost/property_tree/ini_parser.hpp>
  7. #include <boost/nowide/fstream.hpp>
  8. #include <boost/algorithm/string/predicate.hpp>
  9. #include <boost/filesystem.hpp>
  10. bool write_to_pot(boost::filesystem::path path, const std::vector<std::pair<std::string, std::string>>& data)
  11. {
  12. boost::nowide::ofstream file(path.string(), std::ios_base::app);
  13. for (const auto& element : data)
  14. {
  15. //Example of .pot element
  16. //#: src/slic3r/GUI/GUI_App.cpp:1647 src/slic3r/GUI/wxExtensions.cpp:687
  17. //msgctxt "Mode"
  18. //msgid "Advanced"
  19. //msgstr ""
  20. file << "\n#: resources/data/hints.ini: ["<< element.first << "]\nmsgid \"" << element.second << "\"\nmsgstr \"\"\n";
  21. }
  22. file.close();
  23. return true;
  24. }
  25. bool read_hints_ini(boost::filesystem::path path, std::vector<std::pair<std::string, std::string>>& pot_elements)
  26. {
  27. namespace pt = boost::property_tree;
  28. pt::ptree tree;
  29. boost::nowide::ifstream ifs(path.string());
  30. try {
  31. pt::read_ini(ifs, tree);
  32. }
  33. catch (const boost::property_tree::ini_parser::ini_parser_error& err) {
  34. std::cout << err.what() << std::endl;
  35. return false;
  36. }
  37. for (const auto& section : tree) {
  38. if (boost::starts_with(section.first, "hint:")) {
  39. for (const auto& data : section.second) {
  40. if (data.first == "text")
  41. {
  42. pot_elements.emplace_back(section.first, data.second.data());
  43. break;
  44. }
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. int main(int argc, char* argv[])
  51. {
  52. std::vector<std::pair<std::string, std::string>> data;
  53. boost::filesystem::path path_to_ini;
  54. boost::filesystem::path path_to_pot;
  55. if (argc != 3)
  56. {
  57. std::cout << "HINTS_TO_POT FAILED: WRONG NUM OF ARGS" << std::endl;
  58. return -1;
  59. }
  60. try {
  61. path_to_ini = boost::filesystem::canonical(boost::filesystem::path(argv[1])).parent_path() / "resources" / "data" / "hints.ini";
  62. path_to_pot = boost::filesystem::canonical(boost::filesystem::path(argv[2])).parent_path() / "localization" /"PrusaSlicer.pot";
  63. } catch (std::exception&) {
  64. std::cout << "HINTS_TO_POT FAILED: BOOST CANNONICAL" << std::endl;
  65. return -1;
  66. }
  67. if (!boost::filesystem::exists(path_to_ini)){
  68. std::cout << "HINTS_TO_POT FAILED: PATH TO INI DOES NOT EXISTS" << std::endl;
  69. std::cout << path_to_ini.string() << std::endl;
  70. return -1;
  71. }
  72. if (!read_hints_ini(std::move(path_to_ini), data)) {
  73. std::cout << "HINTS_TO_POT FAILED TO READ HINTS INI" << std::endl;
  74. return -1;
  75. }
  76. if (!write_to_pot(std::move(path_to_pot), data)) {
  77. std::cout << "HINTS_TO_POT FAILED TO WRITE POT FILE" << std::endl;
  78. return -1;
  79. }
  80. std::cout << "HINTS_TO_POT SUCCESS" << std::endl;
  81. return 0;
  82. }