diff --git a/CMakeLists.txt b/CMakeLists.txt index 2457da3..3e9c489 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # # Qt # +option(PROJECT_TRANSLATIONS_UPDATE "Update source translations [default: OFF]" OFF) set(PROJECT_QT_VERSION 5 CACHE STRING "Qt version to use: 5 (default) or 6") set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) @@ -49,7 +50,6 @@ set(PROJECT_SOURCES src/window.cpp src/main.cpp ) -set(PROJECT_RESOURCES resources/app.qrc) # # Resources @@ -61,14 +61,58 @@ ecm_add_app_icon(PROJECT_SOURCES ICONS resources/icons/32-apps-${PROJECT_ID}.png resources/icons/sc-apps-${PROJECT_ID}.svg ) -source_group("Resource Files" FILES ${PROJECT_RESOURCES}) +set(PROJECT_RESOURCES + resources/app.qrc + resources/freedesktop/${PROJECT_ID}.desktop +) +set(PROJECT_TRANSLATION_TEMPLATE resources/translations/${PROJECT_ID}.ts) +file(GLOB PROJECT_TRANSLATION_SOURCES "resources/translations/${PROJECT_ID}_*.ts") + +source_group("Resource Files" FILES + ${PROJECT_RESOURCES} + ${PROJECT_TRANSLATION_TEMPLATE} + ${PROJECT_TRANSLATION_SOURCES} +) + +# +# Translations +# +if(${QT_VERSION_MAJOR} VERSION_LESS 6.2) + if(PROJECT_TRANSLATIONS_UPDATE) + # the first call creates/updates the .ts template file, it should not be compiled, + # the other the actual _.ts translation files to be installed later. + qt_create_translation(QM_DUMMY + ${PROJECT_SOURCES} + ${PROJECT_TRANSLATION_TEMPLATE} + ) + qt_create_translation(PROJECT_QM_FILES + ${PROJECT_SOURCES} + ${PROJECT_TRANSLATION_SOURCES} + ) + add_custom_target(TemplateUpdate ALL DEPENDS ${QM_DUMMY}) + endif() + qt_add_translation(PROJECT_QM_FILES ${PROJECT_TRANSLATION_SOURCES}) +else() + # FIXME: updating the .ts files seems not to work in Qt6, even by using the deprecated + # qt_create_translation() used with Qt5. It's not clear where qt_add_lupdate() call + # should be placed, qt_add_lrelease() wants to be called after add_executable() for sure. + if(PROJECT_TRANSLATIONS_UPDATE) + qt_add_lupdate(${PROJECT_NAME} + TS_FILES ${PROJECT_TRANSLATION_TEMPLATE} ${PROJECT_TRANSLATION_SOURCES} + SOURCES ${PROJECT_SOURCES} + ) + endif() +endif() # # Build # add_executable(${PROJECT_NAME} - ${PROJECT_SOURCES} + ${PROJECT_QM_FILES} ${PROJECT_RESOURCES} + ${PROJECT_SOURCES} + ${PROJECT_TRANSLATION_SOURCES} + ${PROJECT_TRANSLATION_TEMPLATE} ) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} @@ -91,6 +135,13 @@ set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_ID}") target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Woverloaded-virtual) target_compile_definitions(${PROJECT_NAME} PRIVATE PROJECT_DATA_DIR="${PROJECT_DATA_DIR}") +if(${QT_VERSION_MAJOR} VERSION_GREATER_EQUAL 6.2) + qt_add_lrelease(${PROJECT_NAME} + TS_FILES ${PROJECT_TRANSLATION_SOURCES} + QM_FILES_OUTPUT_VARIABLE PROJECT_QM_FILES + ) +endif() + # # Install # @@ -102,6 +153,9 @@ if (UNIX AND NOT APPLE) install(FILES "resources/freedesktop/${PROJECT_ID}.desktop" DESTINATION "${CMAKE_INSTALL_DATADIR}/applications" ) + install(FILES "${PROJECT_QM_FILES}" + DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_ID}/translations" + ) ecm_install_icons(ICONS "sc-apps-${PROJECT_ID}.svg" DESTINATION "${CMAKE_INSTALL_DATADIR}/icons" ) @@ -111,7 +165,8 @@ endif() # Project information # message(STATUS " -Qt version: ${QT_VERSION} -Build type: ${CMAKE_BUILD_TYPE} -Install prefix: ${CMAKE_INSTALL_PREFIX} +Qt version: ${QT_VERSION} +Build type: ${CMAKE_BUILD_TYPE} +Install prefix: ${CMAKE_INSTALL_PREFIX} +Update translations before build: ${PROJECT_TRANSLATIONS_UPDATE} ") diff --git a/README.md b/README.md index 4c00737..a49dd33 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,19 @@ Install (must be run as root if installing to /usr or /usr/local): make install +## Translations + +To contribute language translations, use Qt Linguist and/or manually: + +- Copy `resource/translations/colorpick.ts` language template to + `resource/translations/colorpick_.ts`, e.g.: `colorpick_de.ts` +- Set the language in the file: + from `` to `` +- Translate it and the `resources/freedesktop/colorpick.desktop` file by adding + e.g.: `GenericName[de]`, `Comment[de]` and optionally `Name[de]` + (see the existing translations as reference) +- Create a Pull Request with your changes. + ## Author Aurélien Gâteau diff --git a/resources/freedesktop/colorpick.desktop b/resources/freedesktop/colorpick.desktop index 062edf3..b59b0b5 100644 --- a/resources/freedesktop/colorpick.desktop +++ b/resources/freedesktop/colorpick.desktop @@ -1,9 +1,13 @@ [Desktop Entry] Type=Application Version=1.0 -Name=Colorpick -GenericName=A color picker Icon=colorpick TryExec=colorpick Exec=colorpick Categories=Qt;KDE;Graphics;Utility; + +Name=Colorpick +GenericName=Color picker +Comment=Color picker with contrast control +GenericName[it]=Selettore di colori +Comment[it]=Selettore di colori con controllo del contrasto diff --git a/resources/translations/colorpick.ts b/resources/translations/colorpick.ts new file mode 100644 index 0000000..e1805a8 --- /dev/null +++ b/resources/translations/colorpick.ts @@ -0,0 +1,101 @@ + + + + + ColorEditor + + + Inkscape + + + + + Hexa with # + + + + + Quoted hexa with # + + + + + Float values + + + + + Int values + + + + + CSS RGB Value + + + + + ContrastPreview + + + Contrast Example Text + + + + + Bad + + + + + Acceptable + + + + + Good + + + + + Contrast Ratio + + + + + HsvColorSpace + + + H + + + + + S + + + + + V + + + + + RgbColorSpace + + + R + + + + + G + + + + + B + + + + diff --git a/resources/translations/colorpick_it.ts b/resources/translations/colorpick_it.ts new file mode 100644 index 0000000..c57fa7c --- /dev/null +++ b/resources/translations/colorpick_it.ts @@ -0,0 +1,101 @@ + + + + + ColorEditor + + + Inkscape + + + + + Hexa with # + Esadecimale con # + + + + Quoted hexa with # + Esadecimale virgolettato con # + + + + Float values + Valori decimali + + + + Int values + Valori interi + + + + CSS RGB Value + Valore CSS RGB + + + + ContrastPreview + + + Contrast Example Text + Esempio Contrasto + + + + Bad + Pessimo + + + + Acceptable + Accettabile + + + + Good + Ottimo + + + + Contrast Ratio + Rapporto Contrasto + + + + HsvColorSpace + + + H + + + + + S + + + + + V + + + + + RgbColorSpace + + + R + + + + + G + + + + + B + + + + diff --git a/src/coloreditor.cpp b/src/coloreditor.cpp index b58619c..207a775 100644 --- a/src/coloreditor.cpp +++ b/src/coloreditor.cpp @@ -171,7 +171,7 @@ void ColorEditor::fillCopyMenu() }; auto addColorAction = [this](const QString &text, const QString &value) { - QString fullText = ColorEditor::tr("%1: %2").arg(text, value); + QString fullText = QString("%1: %2").arg(text, value); QAction *action = mCopyMenu->addAction(fullText); connect(action, &QAction::triggered, this, [value]() { QApplication::clipboard()->setText(value); diff --git a/src/contrastpreview.cpp b/src/contrastpreview.cpp index d017e78..469e851 100644 --- a/src/contrastpreview.cpp +++ b/src/contrastpreview.cpp @@ -58,7 +58,8 @@ void ContrastPreview::updateRatioLabel() } else { level = tr("Good"); } - QString text = tr("Contrast Ratio: %1:1 (%2)").arg(QLocale::system().toString(ratio, 'g', 2)).arg(level); + QString text = tr("Contrast Ratio"); + text = text + QString(": %1:1 (%2)").arg(QLocale::system().toString(ratio, 'g', 2), level); mRatioLabel->setText(text); } diff --git a/src/main.cpp b/src/main.cpp index 945d256..2c82cd7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "window.h" @@ -6,8 +9,45 @@ int main(int argc, char** argv) { QApplication app(argc, argv); Q_INIT_RESOURCE(app); + + QTranslator qtTranslator, translator; + + // For some reason Qtc loads the wrong locale, force it for debugging. +#if 1 + QLocale locale = QLocale::system(); +#else + QLocale locale(QLocale("it")); + QLocale::setDefault(locale); +#endif + // install the translations built-into Qt itself + if (qtTranslator.load(QStringLiteral("qt_") + locale.name(), +#if QT_VERSION < 0x060000 + QLibraryInfo::location(QLibraryInfo::TranslationsPath))) +#else + QLibraryInfo::path(QLibraryInfo::TranslationsPath))) +#endif + app.installTranslator(&qtTranslator); + + // E.g. "_en" + QString translationsFileName = QCoreApplication::applicationName().toLower() + '_' + + locale.name(); + // Try first in the same binary directory, in case we are building, + // otherwise read from system data + QString translationsPath = QCoreApplication::applicationDirPath(); + + bool isLoaded = translator.load(translationsFileName, translationsPath); + if (!isLoaded) { + // "/usr/share//translations + isLoaded = translator.load(translationsFileName, + QStringLiteral(PROJECT_DATA_DIR) + + QStringLiteral("/translations")); + } + if (isLoaded) + app.installTranslator(&translator); + auto iconName = QString(":/icons/sc-apps-colorpick.svg"); app.setWindowIcon(QIcon(iconName)); + Window window; window.show(); return app.exec();