From a27ae7e4a6a2e3fcfd79958f303c472634ea30aa Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Thu, 13 Aug 2020 19:53:36 +0200 Subject: [PATCH] Add CMake option to install translations Fixes #485 --- CMakeLists.txt | 2 ++ cmake/Translations.cmake | 25 +++++++++++++++---------- src/CMakeLists.txt | 4 ++++ src/config.h.in | 3 ++- src/main.cpp | 3 ++- src/settings/behavioursettingspage.cpp | 9 +++++++-- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ed32fd908..28bd0b2f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -368,6 +368,8 @@ else() ) endif() +option(INSTALL_TRANSLATIONS "Install translations" OFF) + optional_component(SUBSONIC ON "Subsonic support") optional_component(TIDAL ON "Tidal support") diff --git a/cmake/Translations.cmake b/cmake/Translations.cmake index 5794f38d5..77603f326 100644 --- a/cmake/Translations.cmake +++ b/cmake/Translations.cmake @@ -19,6 +19,8 @@ set (XGETTEXT_OPTIONS --from-code=utf-8 ) +execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/translations) + macro(add_pot outfiles header pot) # Make relative filenames for all source files set(add_pot_sources) @@ -64,18 +66,21 @@ macro(add_po outfiles po_prefix) ) list(APPEND ${outfiles} ${_qm_filepath}) + list(APPEND INSTALL_TRANSLATIONS_FILES ${_qm_filepath}) endforeach (_lang) # Generate a qrc file for the translations - set(_qrc ${CMAKE_CURRENT_BINARY_DIR}/${ADD_PO_DIRECTORY}/translations.qrc) - file(WRITE ${_qrc} "") - foreach(_lang ${ADD_PO_LANGUAGES}) - file(APPEND ${_qrc} "${po_prefix}${_lang}.qm") - endforeach(_lang) - file(APPEND ${_qrc} "") - if(WITH_QT6) - qt6_add_resources(${outfiles} ${_qrc}) - else() - qt5_add_resources(${outfiles} ${_qrc}) + if(NOT INSTALL_TRANSLATIONS) + set(_qrc ${CMAKE_CURRENT_BINARY_DIR}/${ADD_PO_DIRECTORY}/translations.qrc) + file(WRITE ${_qrc} "") + foreach(_lang ${ADD_PO_LANGUAGES}) + file(APPEND ${_qrc} "${po_prefix}${_lang}.qm") + endforeach(_lang) + file(APPEND ${_qrc} "") + if(WITH_QT6) + qt6_add_resources(${outfiles} ${_qrc}) + else() + qt5_add_resources(${outfiles} ${_qrc}) + endif() endif() endmacro(add_po) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 94a5e620a..3f7d7d531 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1243,6 +1243,10 @@ if(NOT APPLE) install(TARGETS strawberry RUNTIME DESTINATION bin) endif() +if(INSTALL_TRANSLATIONS_FILES) + install(FILES ${INSTALL_TRANSLATIONS_FILES} DESTINATION share/translations) +endif() + if(APPLE) set_target_properties(strawberry PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/../dist/macos/Info.plist") endif (APPLE) diff --git a/src/config.h.in b/src/config.h.in index 97616eb9d..52945c431 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -58,9 +58,10 @@ #cmakedefine HAVE_TAGLIB_DSDIFFFILE #cmakedefine USE_BUNDLE - #define USE_BUNDLE_DIR "${USE_BUNDLE_DIR}" #cmakedefine HAVE_TRANSLATIONS +#cmakedefine INSTALL_TRANSLATIONS +#define TRANSLATIONS_DIR "${CMAKE_INSTALL_PREFIX}/share/translations" #endif // CONFIG_H_IN diff --git a/src/main.cpp b/src/main.cpp index 71c0f007a..36a9ac9cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -225,7 +225,7 @@ int main(int argc, char* argv[]) { // Resources Q_INIT_RESOURCE(data); Q_INIT_RESOURCE(icons); -#ifdef HAVE_TRANSLATIONS +#if defined(HAVE_TRANSLATIONS) && !defined(INSTALL_TRANSLATIONS) Q_INIT_RESOURCE(translations); #endif @@ -254,6 +254,7 @@ int main(int argc, char* argv[]) { translations->LoadTranslation("qt", QLibraryInfo::location(QLibraryInfo::TranslationsPath), language); translations->LoadTranslation("strawberry", ":/translations", language); + translations->LoadTranslation("strawberry", TRANSLATIONS_DIR, language); translations->LoadTranslation("strawberry", a.applicationDirPath(), language); translations->LoadTranslation("strawberry", QDir::currentPath(), language); diff --git a/src/settings/behavioursettingspage.cpp b/src/settings/behavioursettingspage.cpp index 73dd9419c..4b88d0458 100644 --- a/src/settings/behavioursettingspage.cpp +++ b/src/settings/behavioursettingspage.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include "core/iconloader.h" #include "core/mainwindow.h" @@ -73,8 +74,12 @@ BehaviourSettingsPage::BehaviourSettingsPage(SettingsDialog *dialog) : SettingsP #ifdef HAVE_TRANSLATIONS // Populate the language combo box. We do this by looking at all the compiled in translations. - QDir dir(":/translations/"); - QStringList codes(dir.entryList(QStringList() << "*.qm")); + QDir dir1(":/translations/"); + QDir dir2(TRANSLATIONS_DIR); + QStringList codes(dir1.entryList(QStringList() << "*.qm")); + if (dir2.exists()) { + codes << dir2.entryList(QStringList() << "*.qm"); + } QRegularExpression lang_re("^strawberry_(.*).qm$"); for (const QString &filename : codes) {