Add CMake option to install translations

Fixes #485
This commit is contained in:
Jonas Kvinge 2020-08-13 19:53:36 +02:00
parent dd0ab897aa
commit a27ae7e4a6
6 changed files with 32 additions and 14 deletions

View File

@ -368,6 +368,8 @@ else()
)
endif()
option(INSTALL_TRANSLATIONS "Install translations" OFF)
optional_component(SUBSONIC ON "Subsonic support")
optional_component(TIDAL ON "Tidal support")

View File

@ -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} "<RCC><qresource prefix=\"/${ADD_PO_DIRECTORY}\">")
foreach(_lang ${ADD_PO_LANGUAGES})
file(APPEND ${_qrc} "<file>${po_prefix}${_lang}.qm</file>")
endforeach(_lang)
file(APPEND ${_qrc} "</qresource></RCC>")
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} "<RCC><qresource prefix=\"/${ADD_PO_DIRECTORY}\">")
foreach(_lang ${ADD_PO_LANGUAGES})
file(APPEND ${_qrc} "<file>${po_prefix}${_lang}.qm</file>")
endforeach(_lang)
file(APPEND ${_qrc} "</qresource></RCC>")
if(WITH_QT6)
qt6_add_resources(${outfiles} ${_qrc})
else()
qt5_add_resources(${outfiles} ${_qrc})
endif()
endif()
endmacro(add_po)

View File

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

View File

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

View File

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

View File

@ -37,6 +37,7 @@
#include <QSpinBox>
#include <QComboBox>
#include <QGroupBox>
#include <QStandardPaths>
#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) {