Add translation support, german translation.

This commit is contained in:
tastytea 2021-05-20 07:07:47 +02:00
parent 678b506a8c
commit 94c25552a1
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 138 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/build/
/doc/
/examples/example99*
/translations/de

View File

@ -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)

View File

@ -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 "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")

View File

@ -16,13 +16,23 @@
#include "options.hpp"
#include <boost/locale/generator.hpp>
#include <cstdlib>
#include <iostream>
#include <locale>
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)

View File

@ -18,6 +18,7 @@
#include "version.hpp"
#include <boost/locale/message.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
@ -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 <tastytea@tastytea.de>\n"
"License AGPLv3: GNU AGPL version 3 "
"<https://gnu.org/licenses/agpl.html>.\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 <tastytea@tastytea.de>\n"
"License AGPL-3.0-only <https://gnu.org/licenses/agpl.html>.\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;

View File

@ -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)

62
translations/de.po Normal file
View File

@ -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 <tastytea@tastytea.de>\n"
"Language-Team: tastytea <https://schlomp.space/tastytea/epubgrep>\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 <tastytea@tastytea.de>\n"
"License AGPL-3.0-only <https://gnu.org/licenses/agpl.html>.\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 <tastytea@tastytea.de>\n"
"Lizenz AGPL-3.0-only <https://gnu.org/licenses/agpl.html>.\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"