From 94c25552a1ee93206682c3d49830f350ee278470 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 20 May 2021 07:07:47 +0200 Subject: [PATCH] Add translation support, german translation. --- .gitignore | 1 + CMakeLists.txt | 7 ++++- src/CMakeLists.txt | 2 +- src/main.cpp | 10 ++++++ src/options.cpp | 39 ++++++++++++++--------- translations/CMakeLists.txt | 34 ++++++++++++++++++++ translations/de.po | 62 +++++++++++++++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 17 deletions(-) create mode 100644 translations/CMakeLists.txt create mode 100644 translations/de.po diff --git a/.gitignore b/.gitignore index 830cc83..64ef882 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /build/ /doc/ /examples/example99* +/translations/de diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b661d1..6904344 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.12...3.18) # Global build options. set(CMAKE_BUILD_TYPE "Release" CACHE STRING "The type of build.") +set(XGETTEXT_CMD "xgettext" CACHE STRING "The command for xgettext.") project(epubgrep VERSION 0.0.0 @@ -12,6 +13,7 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") # Project build options. option(WITH_TESTS "Compile tests." NO) +option(GENERATE_POT "Generate / Update .pot file" NO) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -19,10 +21,13 @@ set(CMAKE_CXX_EXTENSIONS OFF) include(cmake/debug_flags.cmake) -find_package(Boost 1.75.0 REQUIRED COMPONENTS program_options) +find_package(Boost 1.75.0 REQUIRED COMPONENTS program_options locale) +find_package(Gettext REQUIRED) add_subdirectory(src) if(WITH_TESTS) add_subdirectory(tests) endif() + +add_subdirectory(translations) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 38162f2..ead4a69 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,7 @@ unset(sources_src) unset(headers_src) target_link_libraries(${PROJECT_NAME} - Boost::program_options) + Boost::program_options Boost::locale) target_include_directories(${PROJECT_NAME} PRIVATE "$") diff --git a/src/main.cpp b/src/main.cpp index 2817a6a..62ad6fa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,13 +16,23 @@ #include "options.hpp" +#include + #include #include +#include int main(int argc, char *argv[]) { using std::cout; + boost::locale::generator locale_generator; + locale_generator.add_messages_path("translations"); + locale_generator.add_messages_path("/usr/share/locale"); + locale_generator.add_messages_domain("epubgrep"); + std::locale::global(locale_generator("")); + cout.imbue(std::locale()); + auto vm{epubgrep::parse_options(argc, argv)}; if (vm.count("help") + vm.count("version") > 0) diff --git a/src/options.cpp b/src/options.cpp index 06f6081..9eb6f94 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -18,6 +18,7 @@ #include "version.hpp" +#include #include #include #include @@ -28,18 +29,26 @@ namespace epubgrep { namespace po = boost::program_options; + +using boost::locale::translate; using std::cout; po::variables_map parse_options(int argc, char *argv[]) { - po::options_description desc("Available options"); + po::options_description desc(translate("Available options")); // clang-format off desc.add_options() - ("help", "Prints help information") - ("version", "Prints version information") - ("extended-regexp", "PATTERNS are extended regular expressions") - ("perl-regexp", "PATTERNS are Perl regular expressions") - ("ignore-case", "Ignore case distinctions in patterns and data") + ("help", + translate("Display this help and exit").str().data()) + ("version", + translate("Display version information and exit").str().data()) + ("extended-regexp", + translate("PATTERNS are extended regular expressions").str().data()) + ("perl-regexp", + translate("PATTERNS are Perl regular expressions").str().data()) + ("ignore-case", + translate("Ignore case distinctions in patterns and data") + .str().data()) ; // clang-format on po::variables_map vm; @@ -48,19 +57,19 @@ po::variables_map parse_options(int argc, char *argv[]) if (vm.count("help") != 0) { - cout << "Usage: epubgrep [OPTION]… PATTERNS [FILE]…\n"; - cout << desc << '\n'; + cout << translate("Usage: epubgrep [OPTION]… PATTERNS [FILE]…\n"); + cout << desc; } else if (vm.count("version") != 0) { cout << "epubgrep " << epubgrep::version << '\n'; - cout << "Copyright © 2021 tastytea \n" - "License AGPLv3: GNU AGPL version 3 " - ".\n" - "This program comes with ABSOLUTELY NO WARRANTY. This is free " - "software,\n" - "and you are welcome to redistribute it under certain " - "conditions.\n"; + cout << translate( + "Copyright © 2021 tastytea \n" + "License AGPL-3.0-only .\n" + "This program comes with ABSOLUTELY NO WARRANTY. This is " + "free software,\n" + "and you are welcome to redistribute it under certain " + "conditions.\n"); } return vm; diff --git a/translations/CMakeLists.txt b/translations/CMakeLists.txt new file mode 100644 index 0000000..7633523 --- /dev/null +++ b/translations/CMakeLists.txt @@ -0,0 +1,34 @@ +set(potfile "${PROJECT_SOURCE_DIR}/translations/${PROJECT_NAME}.pot") + +if(GENERATE_POT) + file(GLOB po_src_files "../src/*pp") + + add_custom_command(OUTPUT ${potfile} + COMMAND "${XGETTEXT_CMD}" + "--language=C++" + "--keyword=translate:1,1t" + "--keyword=translate:1c,2,2t" + "--keyword=translate:1,2,3t" + "--keyword=translate:1c,2,3,4t" + "--package-name=${PROJECT_NAME}" + "--package-version=${PROJECT_VERSION}" + "--from-code=utf-8" + "--foreign-user" + "--output=${potfile}" + ${po_src_files} + DEPENDS "${po_src_files}" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + COMMENT "Extract translatable messages to ${potfile}") + add_custom_target(${PROJECT_NAME}_pot + ALL DEPENDS ${potfile}) + + unset(po_src_files) +endif(GENERATE_POT) + +file(GLOB po_files "*.po") + +# Creates install target automatically. +GETTEXT_CREATE_TRANSLATIONS(${potfile} ALL ${po_files}) + +unset(po_files) +unset(potfile) diff --git a/translations/de.po b/translations/de.po new file mode 100644 index 0000000..57e1f93 --- /dev/null +++ b/translations/de.po @@ -0,0 +1,62 @@ +msgid "" +msgstr "" +"Project-Id-Version: epubgrep 0.0.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2021-05-20 07:08+0200\n" +"PO-Revision-Date: 2021-05-20 07:06+0200\n" +"Last-Translator: tastytea \n" +"Language-Team: tastytea \n" +"Language: de\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 2.2.4\n" +"X-Poedit-Basepath: ../src\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Poedit-SourceCharset: UTF-8\n" +"X-Poedit-KeywordsList: translate\n" +"X-Poedit-SearchPath-0: .\n" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:38 +msgid "Available options" +msgstr "Verfügbare Optionen" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:67 +msgid "" +"Copyright © 2021 tastytea \n" +"License AGPL-3.0-only .\n" +"This program comes with ABSOLUTELY NO WARRANTY. This is free software,\n" +"and you are welcome to redistribute it under certain conditions.\n" +msgstr "" +"Copyright © 2021 tastytea \n" +"Lizenz AGPL-3.0-only .\n" +"Für dieses Programm besteht KEINERLEI GARANTIE. Dies ist freie Software,\n" +"die Sie unter bestimmten Bedingungen weitergeben dürfen.\n" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:42 +msgid "Display this help and exit" +msgstr "Diese Hilfe ausgeben und beenden" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:44 +msgid "Display version information and exit" +msgstr "Versionsinformationen ausgeben und beenden" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:50 +msgid "Ignore case distinctions in patterns and data" +msgstr "Unterschied zwischen Groß- und Kleinschreibung ignorieren" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:48 +msgid "PATTERNS are Perl regular expressions" +msgstr "MUSTER sind reguläre Ausdrücke, wie Perl sie akzeptiert" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:46 +msgid "PATTERNS are extended regular expressions" +msgstr "MUSTER sind extended regular expressions" + +#: /home/tastytea/Projekte/epubgrep/translations/../src/options.cpp:60 +msgid "Usage: epubgrep [OPTION]… PATTERNS [FILE]…\n" +msgstr "Aufruf: epubgrep [OPTION]… MUSTER [DATEI]…\n" + +#, fuzzy +#~ msgid "Prints help information" +#~ msgstr "Gibt Hilfsinformationen aus"