Parse hints.ini for translatable text as part of gettext_make_pot

This commit is contained in:
David Kocik 2021-08-23 16:26:47 +02:00
parent e5f0099ded
commit d96f75105f
5 changed files with 121 additions and 0 deletions

View File

@ -481,6 +481,7 @@ add_custom_target(gettext_make_pot
COMMAND xgettext --keyword=L --keyword=_L --keyword=_u8L --keyword=L_CONTEXT:1,2c --keyword=_L_PLURAL:1,2 --add-comments=TRN --from-code=UTF-8 --debug COMMAND xgettext --keyword=L --keyword=_L --keyword=_u8L --keyword=L_CONTEXT:1,2c --keyword=_L_PLURAL:1,2 --add-comments=TRN --from-code=UTF-8 --debug
-f "${L10N_DIR}/list.txt" -f "${L10N_DIR}/list.txt"
-o "${L10N_DIR}/PrusaSlicer.pot" -o "${L10N_DIR}/PrusaSlicer.pot"
COMMAND hintsToPot ${SLIC3R_RESOURCES_DIR} ${L10N_DIR}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMENT "Generate pot file from strings in the source tree" COMMENT "Generate pot file from strings in the source tree"
) )
@ -553,6 +554,8 @@ endfunction()
add_subdirectory(src) add_subdirectory(src)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT PrusaSlicer_app_console) set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT PrusaSlicer_app_console)
add_dependencies(gettext_make_pot hintsToPot)
# Perl bindings, currently only used for the unit / integration tests of libslic3r. # Perl bindings, currently only used for the unit / integration tests of libslic3r.
# Also runs the unit / integration tests. # Also runs the unit / integration tests.
#FIXME Port the tests into C++ to finally get rid of the Perl! #FIXME Port the tests into C++ to finally get rid of the Perl!

View File

@ -13,6 +13,7 @@ add_subdirectory(qhull)
add_subdirectory(Shiny) add_subdirectory(Shiny)
add_subdirectory(semver) add_subdirectory(semver)
add_subdirectory(libigl) add_subdirectory(libigl)
add_subdirectory(hints)
# Adding libnest2d project for bin packing... # Adding libnest2d project for bin packing...
add_subdirectory(libnest2d) add_subdirectory(libnest2d)

12
src/hints/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.13)
project(HintsToPot)
add_executable(hintsToPot
HintsToPot.cpp)
target_link_libraries(hintsToPot PRIVATE boost_libs)
#encoding_check(HintsToPot)

84
src/hints/HintsToPot.cpp Normal file
View File

@ -0,0 +1,84 @@
#include <iostream>
#include <vector>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/dll.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/nowide/fstream.hpp>
#include <boost/algorithm/string/predicate.hpp>
bool write_to_pot(boost::filesystem::path path, const std::vector<std::pair<std::string, std::string>>& data)
{
boost::filesystem::ofstream file(std::move(path), std::ios_base::app);
for (const auto& element : data)
{
//Example of .pot element
//#: src/slic3r/GUI/GUI_App.cpp:1647 src/slic3r/GUI/wxExtensions.cpp:687
//msgctxt "Mode"
//msgid "Advanced"
//msgstr ""
file << "\n#: resources/data/hints.ini: ["<< element.first << "]\nmsgid \"" << element.second << "\"\nmsgstr \"\"\n";
}
file.close();
return true;
}
bool read_hints_ini(boost::filesystem::path path, std::vector<std::pair<std::string, std::string>>& pot_elements)
{
namespace pt = boost::property_tree;
pt::ptree tree;
boost::nowide::ifstream ifs(path.string());
try {
pt::read_ini(ifs, tree);
}
catch (const boost::property_tree::ini_parser::ini_parser_error& err) {
std::cout << err.what() << std::endl;
return false;
}
for (const auto& section : tree) {
if (boost::starts_with(section.first, "hint:")) {
for (const auto& data : section.second) {
if (data.first == "text")
{
pot_elements.emplace_back(section.first, data.second.data());
break;
}
}
}
}
return true;
}
int main(int argc, char* argv[])
{
std::vector<std::pair<std::string, std::string>> data;
boost::filesystem::path path_to_ini;
boost::filesystem::path path_to_pot;
if (argc != 3)
{
std::cout << "HINTS_TO_POT FAILED: WRONG NUM OF ARGS" << std::endl;
return -1;
}
try {
path_to_ini = boost::filesystem::canonical(boost::filesystem::path(argv[1])).parent_path() / "resources" / "data" / "hints.ini";
path_to_pot = boost::filesystem::canonical(boost::filesystem::path(argv[2])).parent_path() / "localization" /"PrusaSlicer.pot";
} catch (std::exception&) {
std::cout << "HINTS_TO_POT FAILED: BOOST CANNONICAL" << std::endl;
return -1;
}
if (!boost::filesystem::exists(path_to_ini)){
std::cout << "HINTS_TO_POT FAILED: PATH TO INI DOES NOT EXISTS" << std::endl;
std::cout << path_to_ini.string() << std::endl;
return -1;
}
if (!read_hints_ini(std::move(path_to_ini), data)) {
std::cout << "HINTS_TO_POT FAILED TO READ HINTS INI" << std::endl;
return -1;
}
if (!write_to_pot(std::move(path_to_pot), data)) {
std::cout << "HINTS_TO_POT FAILED TO WRITE POT FILE" << std::endl;
return -1;
}
std::cout << "HINTS_TO_POT SUCCESS" << std::endl;
return 0;
}

View File

@ -179,6 +179,21 @@ void launch_browser_if_allowed(const std::string& url)
if (wxGetApp().app_config->get("suppress_hyperlinks") != "1") if (wxGetApp().app_config->get("suppress_hyperlinks") != "1")
wxLaunchDefaultBrowser(url); wxLaunchDefaultBrowser(url);
} }
bool pot_exists()
{
return true;
// return boost::filesystem::exists(std::move(boost::filesystem::path(resources_dir()) / "data" / "hints.pot"));
}
void write_pot(const std::vector<std::string>& elements)
{
boost::filesystem::ofstream file(std::move(boost::filesystem::path(resources_dir()) / "data" / "hints.pot"));
for ( const auto &element : elements)
{
file << "msgid \"" << escape_string_cstyle(element) << "\"\nmsgstr \"\"\n\n";
}
file.close();
}
} //namespace } //namespace
void HintDatabase::init() void HintDatabase::init()
@ -195,6 +210,8 @@ void HintDatabase::load_hints_from_file(const boost::filesystem::path& path)
{ {
namespace pt = boost::property_tree; namespace pt = boost::property_tree;
pt::ptree tree; pt::ptree tree;
bool create_pot = !pot_exists();
std::vector<std::string> pot_elements;
boost::nowide::ifstream ifs(path.string()); boost::nowide::ifstream ifs(path.string());
try { try {
pt::read_ini(ifs, tree); pt::read_ini(ifs, tree);
@ -221,6 +238,8 @@ void HintDatabase::load_hints_from_file(const boost::filesystem::path& path)
std::string documentation_link; std::string documentation_link;
//unescape text1 //unescape text1
unescape_string_cstyle(_utf8(dict["text"]), fulltext); unescape_string_cstyle(_utf8(dict["text"]), fulltext);
if (create_pot)
pot_elements.emplace_back(fulltext);
// replace <b> and </b> for imgui markers // replace <b> and </b> for imgui markers
std::string marker_s(1, ImGui::ColorMarkerStart); std::string marker_s(1, ImGui::ColorMarkerStart);
std::string marker_e(1, ImGui::ColorMarkerEnd); std::string marker_e(1, ImGui::ColorMarkerEnd);
@ -319,6 +338,8 @@ void HintDatabase::load_hints_from_file(const boost::filesystem::path& path)
} }
} }
} }
if (create_pot)
write_pot(pot_elements);
} }
HintData* HintDatabase::get_hint(bool up) HintData* HintDatabase::get_hint(bool up)
{ {