From efa48162a82115a287ac5fea75dd3884d669f4de Mon Sep 17 00:00:00 2001 From: Dominic Radermacher Date: Sun, 3 Aug 2025 09:42:30 +0200 Subject: [PATCH] better gettext support (thanks to Michael Schulz) --- CMakeLists.txt | 3 +- cmake/gettext.cmake | 76 +++++++++++++++++++++++++++++++++++++++++++++ po/de.po | 5 ++- po/en.po | 7 +++-- src/ptouch-print.c | 6 +++- 5 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 cmake/gettext.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index fd5a3bb..61f7c40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,6 @@ target_link_libraries(${PROJECT_NAME} PRIVATE target_sources(${PROJECT_NAME} PRIVATE include/ptouch.h - include/gettext.h src/libptouch.c src/ptouch-print.c ) @@ -66,6 +65,8 @@ add_custom_target(git-version ALL ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/gitversion.cmake ) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/gettext.cmake) + install(TARGETS ${PROJECT_NAME} DESTINATION bin) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/ptouch-print.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) diff --git a/cmake/gettext.cmake b/cmake/gettext.cmake new file mode 100644 index 0000000..21d6155 --- /dev/null +++ b/cmake/gettext.cmake @@ -0,0 +1,76 @@ +# Utilize gettext multilingual internationalization services +if(Gettext_FOUND) + add_custom_target( + pot-update + ALL + DEPENDS ${CMAKE_SOURCE_DIR}/po/ptouch.pot + ) + add_dependencies(pot-update git-version) + + # generate pot files using xgettext + if(GETTEXT_XGETTEXT_EXECUTABLE) + file(GLOB_RECURSE C_FILES RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/src/*.c) + file(STRINGS ${CMAKE_BINARY_DIR}/version.h VERSION_LINE REGEX "VERSION") + string(REGEX MATCH "\".*\"$" PVERSION ${VERSION_LINE}) + add_custom_command( + TARGET pot-update + PRE_BUILD + COMMAND ${GETTEXT_XGETTEXT_EXECUTABLE} + --keyword=_ + --keyword=N_ + --force-po + --package-name=${PROJECT_NAME} + --package-version=${PVERSION} + --copyright-holder="Dominic Radermacher " + --msgid-bugs-address="dominic@familie-radermacher.ch" + --output ${CMAKE_SOURCE_DIR}/po/ptouch.pot + ${C_FILES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + ) + else() + message(DEBUG "Variable GETTEXT_XGETTEXT_EXECUTABLE not set") + endif() + + # read available languages from LINGUAS file while ignoring comments + file(STRINGS po/LINGUAS LINGUAS REGEX "^[^#]") + + # merge po files + if(GETTEXT_MSGMERGE_EXECUTABLE) + add_custom_target( + po-merge + ALL + DEPENDS ${CMAKE_SOURCE_DIR}/po/ptouch.pot + ) + add_dependencies(po-merge pot-update) + + foreach(LINGUA IN LISTS LINGUAS) + add_custom_command( + TARGET po-merge + PRE_BUILD + COMMAND ${GETTEXT_MSGMERGE_EXECUTABLE} + --update + --quiet + ${CMAKE_SOURCE_DIR}/po/${LINGUA}.po + ${CMAKE_SOURCE_DIR}/po/ptouch.pot + ) + endforeach() + endif() + + # compile po files + foreach(LINGUA IN LISTS LINGUAS) + gettext_process_po_files( + ${LINGUA} + ALL + PO_FILES ${CMAKE_SOURCE_DIR}/po/${LINGUA}.po + ) + endforeach() + + # install mo files + foreach(LINGUA IN LISTS LINGUAS) + install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/${LINGUA}.gmo" + DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${LINGUA}/LC_MESSAGES" + RENAME "${PROJECT_NAME}.mo" + ) + endforeach() +endif() diff --git a/po/de.po b/po/de.po index d67eab7..d544bb1 100644 --- a/po/de.po +++ b/po/de.po @@ -56,7 +56,8 @@ msgstr "" #: src/libptouch.c:158 #, c-format -msgid "No P-Touch printer found on USB (remember to put switch to position E)\n" +msgid "" +"No P-Touch printer found on USB (remember to put switch to position E)\n" msgstr "Kein P-Ptouch Drucker am USB gefunden (Schalter muss auf E stehen)\n" #: src/libptouch.c:178 @@ -66,7 +67,6 @@ msgstr "" #: src/libptouch.c:182 #, fuzzy, c-format -#| msgid "read error: got %i instead of 32 bytes\n" msgid "write error: could send only %i of %ld bytes\n" msgstr "Lesefehler: %i anstatt 32 bytes empfangen\n" @@ -152,7 +152,6 @@ msgstr "" #: src/ptouch-print.c:133 #, fuzzy, c-format -#| msgid "ptouch_send() failed\n" msgid "ptouch_sendraster() failed\n" msgstr "ptouch_send() fehlgeschlagen\n" diff --git a/po/en.po b/po/en.po index e50f765..c495d46 100644 --- a/po/en.po +++ b/po/en.po @@ -55,8 +55,10 @@ msgstr "interface claim error: %s\n" #: src/libptouch.c:158 #, c-format -msgid "No P-Touch printer found on USB (remember to put switch to position E)\n" -msgstr "No P-Touch printer found on USB (remember to put switch to position E)\n" +msgid "" +"No P-Touch printer found on USB (remember to put switch to position E)\n" +msgstr "" +"No P-Touch printer found on USB (remember to put switch to position E)\n" #: src/libptouch.c:178 #, c-format @@ -65,7 +67,6 @@ msgstr "write error: %s\n" #: src/libptouch.c:182 #, fuzzy, c-format -#| msgid "write error: could send only %i of %i bytes\n" msgid "write error: could send only %i of %ld bytes\n" msgstr "write error: could send only %i of %i bytes\n" diff --git a/src/ptouch-print.c b/src/ptouch-print.c index 2a40e94..512bfeb 100644 --- a/src/ptouch-print.c +++ b/src/ptouch-print.c @@ -535,7 +535,11 @@ int main(int argc, char *argv[]) ptouch_dev ptdev = NULL; setlocale(LC_ALL, ""); - bindtextdomain("ptouch-print", "/usr/share/locale/"); + const char *textdomain_dir = getenv("TEXTDOMAINDIR"); + if (!textdomain_dir) { + textdomain_dir = "/usr/share/locale/"; + } + bindtextdomain("ptouch-print", textdomain_dir); textdomain("ptouch-print"); int i = parse_args(argc, argv); if (i != argc) {