Skip to content
This repository has been archived by the owner on Oct 30, 2021. It is now read-only.

Commit

Permalink
Use LXQt translation infrastructure
Browse files Browse the repository at this point in the history
No LXQt dependency is added. It remains a Qt only app.

To update the translations set the CMake command line option
UPDATE_TRANSLATIONS to true. Ex: cmake -DUPDATE_TRASNLATIONS=ON ..
The translations will be extracted and also compiled.
To return to the compilation only mode just set the UPDATE_TRANSLATIONS
option to false. Ex: cmake -DCMAKE_UPDATE_TRANSLATIONS=OFF ..

Warning: When UPDATE_TRANSLATIONS is enabled a clean command will also
clean the generated .ts files. The CLEAN_NO_CUSTOM property should solve
that problem. But it didn't, at least for now. So, after updating the
translations, disable UPDATE_TRANSLATIONS right away.

The .desktop files translatable bits are now split by language in the
translations directory. screengrab.desktop.in holds the non-common, not
translatable parts. The LXQtTranslateDesktop.cmake module glues it all, so
only one file (screengrab.desktop) is installed.

The Qt5TranslationLoader.cmake module takes care of automatically produce
code to load the translations.
  • Loading branch information
luis-pereira committed Dec 10, 2014
1 parent 2f59a23 commit f20cbb4
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 33 deletions.
47 changes: 39 additions & 8 deletions CMakeLists.txt
Expand Up @@ -54,7 +54,6 @@ message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wnon-virtual-dtor -Woverloaded-virtual -Wall -Wextra")

add_definitions(-DPREFIX="${CMAKE_INSTALL_PREFIX}")
message(STATUS "Install prefix: " ${CMAKE_INSTALL_PREFIX})

if(DEFINED LIB_SUFFIX)
Expand All @@ -80,6 +79,7 @@ option(SG_GLOBALSHORTCUTS "Enable global shortcuts" OFF)
option(SG_XDG_CONFIG_SUPPORT "Put config files into XDGC_ONFIG_HOME" ON)
option(SG_EXT_UPLOADS "Enable upload screenshots to MediaCrush and imgur" ON)
option(SG_EXT_EDIT "Enable ability to edit screenshots in external editor" ON)
option(UPDATE_TRANSLATIONS "Update source translation translations/*.ts files" OFF)

# The following line will add additional finders to CMake without the need to be placed in the CMake install path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
Expand Down Expand Up @@ -113,6 +113,7 @@ message(STATUS "XDG_CONFIG_HOME support: " ${SG_XDG_CONFIG_SUPPORT})
message(STATUS "Upload to MediaCrush and imgur support: " ${SG_EXT_UPLOADS})
message(STATUS "Editing screenshots in external editor support: " ${SG_EXT_EDIT})
message(STATUS "Use system Qxt Library: " ${SG_USE_SYSTEM_QXT})
message(STATUS "Update source translation translations/*.ts files: " ${UPDATE_TRANSLATIONS})

# docs
if(NOT SG_DOCDIR)
Expand Down Expand Up @@ -169,12 +170,44 @@ set(SCREENGRAB_QRC
qt5_wrap_ui(SCREENGRAB_UI_H ${SCREENGRAB_UI})
qt5_add_resources(QRC_SOURCES ${SCREENGRAB_QRC})


message(STATUS "Generating localize ...")
file(GLOB SCREENGRAB_TS ${CMAKE_CURRENT_SOURCE_DIR}/translations/*.ts)
qt5_add_translation(SCREENGRAB_QMS ${SCREENGRAB_TS})
add_custom_target(translations ALL DEPENDS ${SCREENGRAB_QMS})
# Although the names, LXQtTranslateTs and LXQtTranslateDesktop, they don't
# bring any dependency on lxqt.
include(LXQtTranslateTs)
include(LXQtTranslateDesktop)
include(Qt5TranslationLoader)

set(SCREENGRAB_DESKTOP_FILES_IN
screengrab.desktop.in
)

add_executable( screengrab ${SCREENGRAB_SRC} ${SCREENGRAB_UI_H} ${QRC_SOURCES} )
lxqt_translate_ts(SCREENGRAB_QMS
USE_QT5 TRUE
UPDATE_TRANSLATIONS ${UPDATE_TRANSLATIONS}
SOURCES
${SCREENGRAB_SRC}
${SCREENGRAB_UI}
INSTALL_DIR "share/${PROJECT_NAME}/translations"
)

lxqt_translate_desktop(SCREENGRAB_DESKTOP_FILES
SOURCES ${SCREENGRAB_DESKTOP_FILES_IN}
)

qt5_translation_loader(SCREENGRAB_QM_LOADER
"${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/translations"
${PROJECT_NAME}
)

add_executable(screengrab
${SCREENGRAB_SRC}
${SCREENGRAB_UI_H}
${QRC_SOURCES}
${SCREENGRAB_QMS}
${SCREENGRAB_DESKTOP_FILES}
${SCREENGRAB_QM_LOADER}
)

if(SG_GLOBALSHORTCUTS)
if(SG_USE_SYSTEM_QXT)
Expand Down Expand Up @@ -223,8 +256,6 @@ install(FILES ${DOCS} DESTINATION ${DOCPATH})
# install html docs
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/docs/html/" DESTINATION "${DOCPATH}/html")
# install desktop files
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/screengrab.desktop" DESTINATION share/applications)
install(FILES ${SCREENGRAB_DESKTOP_FILES} DESTINATION share/applications)
# install pixmap
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/img/screengrab.png" DESTINATION share/pixmaps)
# install translations
install(FILES ${SCREENGRAB_QMS} DESTINATION share/screengrab/translations)
93 changes: 93 additions & 0 deletions cmake/LXQtTranslateDesktop.cmake
@@ -0,0 +1,93 @@
#=============================================================================
# The lxqt_translate_desktop() function was copied from the the
# LXQt LxQtTranste.cmake
#
# Original Author: Alexander Sokolov <sokoloff.a@gmail.com>
#
#=============================================================================

function(lxqt_translate_desktop _RESULT)
set(_translationDir "translations")

# Parse arguments ***************************************
set(_state "")
foreach (_arg ${ARGN})
if (
("${_arg}_I_HATE_CMAKE" STREQUAL "SOURCES_I_HATE_CMAKE") OR
("${_arg}_I_HATE_CMAKE" STREQUAL "TRANSLATION_DIR_I_HATE_CMAKE")
)

set(_state ${_arg})

else()
if("${_state}" STREQUAL "SOURCES")
get_filename_component (__file ${_arg} ABSOLUTE)
set(_sources ${_sources} ${__file})
#set(_sources ${_sources} ${_arg})

elseif("${_state}" STREQUAL "TRANSLATION_DIR")
set(_translationDir ${_arg})
set(_state "")

else()
MESSAGE(FATAL_ERROR
"Unknown argument '${_arg}'.\n"
"See ${CMAKE_CURRENT_LIST_FILE} for more information.\n"
)
endif()
endif()
endforeach(_arg)

get_filename_component (_translationDir ${_translationDir} ABSOLUTE)

foreach (_inFile ${_sources})
get_filename_component(_inFile ${_inFile} ABSOLUTE)
get_filename_component(_fileName ${_inFile} NAME_WE)
#Extract the real extension ............
get_filename_component(_fileExt ${_inFile} EXT)
string(REPLACE ".in" "" _fileExt ${_fileExt})
#.......................................
set(_outFile "${CMAKE_CURRENT_BINARY_DIR}/${_fileName}${_fileExt}")

file(GLOB _translations
${_translationDir}/${_fileName}_*${_fileExt}
${_translationDir}/local/${_fileName}_*${_fileExt}
)

set(_pattern "'\\[.*]\\s*='")
if (_translations)
add_custom_command(OUTPUT ${_outFile}
COMMAND grep -v "'#TRANSLATIONS_DIR='" ${_inFile} > ${_outFile}
COMMAND grep --no-filename ${_pattern} ${_translations} >> ${_outFile}
COMMENT "Generating ${_fileName}${_fileExt}"
)
else()
add_custom_command(OUTPUT ${_outFile}
COMMAND grep -v "'#TRANSLATIONS_DIR='" ${_inFile} > ${_outFile}
COMMENT "Generating ${_fileName}${_fileExt}"
)
endif()

set(__result ${__result} ${_outFile})


# TX file ***********************************************
set(_txFile "${CMAKE_BINARY_DIR}/tx/${_fileName}${_fileExt}.tx.sh")
string(REPLACE "${CMAKE_SOURCE_DIR}/" "" _tx_translationDir ${_translationDir})
string(REPLACE "${CMAKE_SOURCE_DIR}/" "" _tx_inFile ${_inFile})
string(REPLACE "." "" _fileType ${_fileExt})

file(WRITE ${_txFile}
"[ -f ${_inFile} ] || exit 0\n"
"echo '[lxde-qt.${_fileName}_${_fileType}]'\n"
"echo 'type = DESKTOP'\n"
"echo 'source_lang = en'\n"
"echo 'source_file = ${_tx_inFile}'\n"
"echo 'file_filter = ${_tx_translationDir}/${_fileName}_<lang>${_fileExt}'\n"
"echo ''\n"
)

endforeach()

set(${_RESULT} ${__result} PARENT_SCOPE)
endfunction(lxqt_translate_desktop)
129 changes: 129 additions & 0 deletions cmake/LXQtTranslateTs.cmake
@@ -0,0 +1,129 @@
#=============================================================================
# Copyright 2014 Luís Pereira <luis.artur.pereira@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#=============================================================================
#
# funtion lxqt_translate_ts(qmFiles
# [USE_QT5 [Yes | No]]
# [UPDATE_TRANSLATIONS [Yes | No]]
# SOURCES <sources>
# [TEMPLATE] translation_template
# [TRANSLATIONS_DIRECTORY] translation_directory
# [INSTALL_DIR] install_directory
# )
# Output:
# qmFiles The generated compiled translations (.qm) files
#
# Input:
# USE_QT5 Optional flag to choose between Qt4 and Qt5. Defaults to Qt5
#
# UPDATE_TRANSLATIONS Optional flag. Setting it to Yes, extracts and
# compiles the translations. Setting it No, only
# compiles them.
#
# TEMPLATE Optional translations files base name. Defaults to
# ${PROJECT_NAME}. An .ts extensions is added.
#
# TRANSLATIONS_DIR Optional path to the directory with the .ts files,
# relative to the CMakeList.txt. Defaults to
# "translations".
#
# INSTALL_DIR Optional destination of the file compiled files (qmFiles).
# If not present no installation is performed



# CMake v2.8.3 needed to use the CMakeParseArguments module
cmake_minimum_required(VERSION 2.8.3 FATAL_ERROR)


function(lxqt_translate_ts qmFiles)
set(oneValueArgs USE_QT5 UPDATE_TRANSLATIONS TEMPLATE TRANSLATION_DIR INSTALL_DIR)
set(multiValueArgs SOURCES)
cmake_parse_arguments(TR "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

if (NOT DEFINED TR_UPDATE_TRANSLATIONS)
set(TR_UPDATE_TRANSLATIONS "No")
endif()

if (NOT DEFINED TR_USE_QT5)
set(TR_USE_QT5 "Yes")
endif()

if(NOT DEFINED TR_TEMPLATE)
set(TR_TEMPLATE "${PROJECT_NAME}")
endif()

if (NOT DEFINED TR_TRANSLATION_DIR)
set(TR_TRANSLATION_DIR "translations")
endif()

file(GLOB tsFiles "${TR_TRANSLATION_DIR}/${TR_TEMPLATE}_*.ts")
set(templateFile "${TR_TRANSLATION_DIR}/${TR_TEMPLATE}.ts")

if(TR_USE_QT5)
# Qt5
if (TR_UPDATE_TRANSLATIONS)
qt5_create_translation(QMS
${TR_SOURCES}
${templateFile}
OPTIONS -locations absolute
)
qt5_create_translation(QM
${TR_SOURCES}
${tsFiles}
OPTIONS -locations absolute
)
else()
qt5_add_translation(QM ${tsFiles})
endif()
else()
# Qt4
if(TR_UPDATE_TRANSLATIONS)
qt4_create_translation(QMS
${TR_SOURCES}
${templateFile}
OPTIONS -locations absolute
)
qt4_create_translation(QM
${TR_SOURCES}
${tsFiles}
OPTIONS -locations absolute
)
else()
qt4_add_translation(QM ${tsFiles})
endif()
endif()

if(TR_UPDATE_TRANSLATIONS)
add_custom_target("update_${TR_TEMPLATE}_ts" ALL DEPENDS ${QMS})
endif()

if(DEFINED TR_INSTALL_DIR)
install(FILES ${QM} DESTINATION ${TR_INSTALL_DIR})
endif()

set(${qmFiles} ${QM} PARENT_SCOPE)
endfunction()
49 changes: 49 additions & 0 deletions cmake/Qt5TranslationLoader.cmake
@@ -0,0 +1,49 @@
#=============================================================================
# Copyright 2014 Luís Pereira <luis.artur.pereira@gmail.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#=============================================================================
#
# These functions enables "automatic" translation loading in Qt5 apps
# and libs. They generate a .cpp file that takes care of everything. The
# user doesn't have to do anything in the source code.
#
# qt5_translation_loader(<source_files> <translations_dir> <catalog_name>)
#
# Output:
# <source_files> Appends the generated file to this variable.
#
# Input:
# <translations_dir> Full path name to the translations dir.
# <catalog_name> Translation catalog to be loaded.

set(__CURRENT_DIR ${CMAKE_CURRENT_LIST_DIR})

function(qt5_translation_loader source_files translations_dir catalog_name)
configure_file(
${__CURRENT_DIR}/Qt5TranslationLoader.cpp.in
Qt5TranslationLoader.cpp @ONLY
)
set(${source_files} ${${source_files}} ${CMAKE_CURRENT_BINARY_DIR}/Qt5TranslationLoader.cpp PARENT_SCOPE)
endfunction()
33 changes: 33 additions & 0 deletions cmake/Qt5TranslationLoader.cpp.in
@@ -0,0 +1,33 @@
/* This file has been generated by the CMake qt_translation_loader().
* It loads Qt application translations.
*
* Attention: All changes will be overwritten!!!
*/

#include <QCoreApplication>
#include <QLocale>
#include <QTranslator>
#include <QLibraryInfo>

static void loadQtTranslation()
{
QString locale = QLocale::system().name();
QTranslator *qtTranslator = new QTranslator(qApp);

if (qtTranslator->load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
qApp->installTranslator(qtTranslator);
} else {
delete qtTranslator;
}

QTranslator *appTranslator = new QTranslator(qApp);
if (appTranslator->load(QString("@translations_dir@/@catalog_name@_%1.qm").arg(locale))) {
QCoreApplication::installTranslator(appTranslator);
} else if (locale == QLatin1String("C") ||
locale.startsWith(QLatin1String("en"))) {
// English is the default. It's translated anyway.
delete appTranslator;
}
}

Q_COREAPP_STARTUP_FUNCTION(loadQtTranslation)

0 comments on commit f20cbb4

Please sign in to comment.