translations: Configure cmake to generate translations

- adds gettext-potfiles cmake target to regenerate *.pot templates from
  sources
- calls GETTEXT_CREATE_TRANSLATIONS to generate and install *.gmo files

The code is exactly the same for all components (libdnf, libdnf-cli,
dnf5, dnf5daemon) so it's for each component imported from common
cmake/Translations.cmake file.
This commit is contained in:
Marek Blaha 2022-10-12 15:36:25 +02:00 committed by Jan Kolarik
parent 74b26399b2
commit ba4c54a5ad
12 changed files with 84 additions and 5 deletions

View File

@ -6,6 +6,7 @@ include(VERSION.cmake)
project(dnf5 LANGUAGES CXX C VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
cmake_policy(VERSION ${CMAKE_VERSION})
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")
message("Building ${PROJECT_NAME} version ${PROJECT_VERSION}")
@ -35,6 +36,9 @@ option(WITH_ZCHUNK "Build with zchunk delta compression support" ON)
option(WITH_HTML "Build HTML documentation" ON)
option(WITH_MAN "Build man pages" ON)
# build options - translations
option(WITH_TRANSLATIONS "Build translations" ON)
# build options - tests
option(WITH_TESTS "Build tests" ON)
option(WITH_PERFORMANCE_TESTS "Build performance tests (DEBUG ONLY)" OFF)
@ -93,6 +97,12 @@ if(WITH_SANITIZERS)
link_libraries(asan ubsan)
endif()
if(WITH_TRANSLATIONS)
# define common command to generate the pot file from sources
list(APPEND XGETTEXT_COMMAND xgettext -F --from-code=UTF-8 --keyword=_ --keyword=M_ --keyword=P_:1,2 --keyword=MP_:1,2 --keyword=C_:1c,2 --keyword=MC_:1c,2 --keyword=CP_:1c,2,3 --keyword=MCP_:1c,2,3 -c)
add_custom_target(gettext-potfiles)
endif()
include_directories("${PROJECT_SOURCE_DIR}/include")
include_directories("${PROJECT_SOURCE_DIR}/common")

25
cmake/Translations.cmake Normal file
View File

@ -0,0 +1,25 @@
# to be included from component/po/CMakeLists.txt using include(Translations)
# requires setting GETTEXT_DOMAIN variable
find_package(Gettext)
set(COMPONENT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/..)
file(GLOB_RECURSE POT_SOURCES RELATIVE ${COMPONENT_PATH} ${COMPONENT_PATH}/*.cpp ${COMPONENT_PATH}/*.hpp)
# target to refresh pot file from current sources
add_custom_target(${GETTEXT_DOMAIN}-pot
COMMENT "Generating fresh ${GETTEXT_DOMAIN}.pot file from sources"
COMMAND ${XGETTEXT_COMMAND} --output=po/${GETTEXT_DOMAIN}.pot ${POT_SOURCES}
WORKING_DIRECTORY ${COMPONENT_PATH}
)
add_dependencies(gettext-potfiles ${GETTEXT_DOMAIN}-pot)
# convert all *.po files to *.gmo and install them
if (GETTEXT_FOUND)
# this process unfortunately reformats .po files so work on a copy
file(GLOB POS ${CMAKE_CURRENT_SOURCE_DIR}/*.po)
file(COPY ${POS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(GLOB POS_BIN ${CMAKE_CURRENT_BINARY_DIR}/*.po)
GETTEXT_CREATE_TRANSLATIONS(${GETTEXT_DOMAIN}.pot ALL ${POS_BIN})
endif()

View File

@ -5,7 +5,8 @@ endif()
find_package(Threads)
# set gettext domain for translations
add_definitions(-DGETTEXT_DOMAIN=\"dnf5\")
set(GETTEXT_DOMAIN dnf5)
add_definitions(-DGETTEXT_DOMAIN=\"${GETTEXT_DOMAIN}\")
# set LIBDIR, used to find the directory containing the dnf5 plugins
add_definitions(-DLIBDIR=\"${CMAKE_INSTALL_FULL_LIBDIR}\")
@ -43,3 +44,6 @@ install(FILES bash-completion/dnf5 DESTINATION ${BASH_COMPLETION_COMPLETIONSDIR}
install(FILES "README.plugins" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/dnf5/plugins" RENAME "README")
install(DIRECTORY "config/usr/" DESTINATION "${CMAKE_INSTALL_PREFIX}")
install(DIRECTORY "config/etc/" DESTINATION "${CMAKE_INSTALL_FULL_SYSCONFDIR}")
add_subdirectory(po)

5
dnf5/po/CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
if(NOT WITH_TRANSLATIONS)
return()
endif()
include(Translations)

View File

@ -6,7 +6,8 @@ set(DNF5DAEMON_CLIENT_BIN dnf5daemon-client)
file(GLOB_RECURSE DNF5DAEMON_CLIENT_SOURCES *.cpp)
#set gettext domain for translations
add_definitions(-DGETTEXT_DOMAIN=\"dnf5-daemon-client\")
set(GETTEXT_DOMAIN dnf5daemon-client)
add_definitions(-DGETTEXT_DOMAIN=\"${GETTEXT_DOMAIN}\")
include_directories(.)
# TODO(mblaha) workround for dnf5daemon-client using server's headers, fix
@ -27,3 +28,6 @@ target_link_libraries(
)
install(TARGETS ${DNF5DAEMON_CLIENT_BIN} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
add_subdirectory(po)

View File

@ -0,0 +1,5 @@
if(NOT WITH_TRANSLATIONS)
return()
endif()
include(Translations)

View File

@ -6,7 +6,8 @@ set(DNF5DAEMON_SERVER_BIN dnf5daemon-server)
file(GLOB_RECURSE DNF5DAEMON_SERVER_SOURCES *.cpp)
# set gettext domain for translations
add_definitions(-DGETTEXT_DOMAIN=\"dnf5-daemon-server\")
set(GETTEXT_DOMAIN dnf5daemon-server)
add_definitions(-DGETTEXT_DOMAIN=\"${GETTEXT_DOMAIN}\")
include_directories(.)
@ -27,5 +28,7 @@ target_link_libraries(
install(TARGETS ${DNF5DAEMON_SERVER_BIN} RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR})
add_subdirectory(polkit)
add_subdirectory(dbus)
add_subdirectory(po)

View File

@ -0,0 +1,5 @@
if(NOT WITH_TRANSLATIONS)
return()
endif()
include(Translations)

View File

@ -3,7 +3,8 @@ if(NOT WITH_LIBDNF5_CLI)
endif()
add_definitions(-DGETTEXT_DOMAIN=\"libdnf5-cli\")
set(GETTEXT_DOMAIN libdnf5-cli)
add_definitions(-DGETTEXT_DOMAIN=\"${GETTEXT_DOMAIN}\")
# use any sources found under the current directory
@ -51,3 +52,6 @@ list(JOIN LIBDNF5_CLI_PC_REQUIRES_PRIVATE ", " LIBDNF5_CLI_PC_REQUIRES_PRIVATE_S
# create a .pc file
configure_file("libdnf-cli.pc.in" ${CMAKE_CURRENT_BINARY_DIR}/libdnf-cli.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libdnf-cli.pc DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig)
add_subdirectory(po)

View File

@ -0,0 +1,5 @@
if(NOT WITH_TRANSLATIONS)
return()
endif()
include(Translations)

View File

@ -9,7 +9,8 @@ list(APPEND LIBDNF5_PC_REQUIRES)
list(APPEND LIBDNF5_PC_REQUIRES_PRIVATE)
# set gettext domain for translations
add_definitions(-DGETTEXT_DOMAIN=\"libdnf5\")
set(GETTEXT_DOMAIN libdnf5)
add_definitions(-DGETTEXT_DOMAIN=\"${GETTEXT_DOMAIN}\")
# If defined, libsolv adds the prefix "dep_" to solvable dependencies.
# As a result, `requires` is renamed to `dep_requires`.
@ -116,3 +117,6 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libdnf.pc DESTINATION ${CMAKE_INSTALL_
# Makes an empty directory for libdnf cache
install(DIRECTORY DESTINATION ${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/cache/libdnf)
add_subdirectory(po)

5
libdnf/po/CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
if(NOT WITH_TRANSLATIONS)
return()
endif()
include(Translations)