Adds the LXQtranslateTs cmake module
authorLuís Pereira <luis.artur.pereira@gmail.com>
Mon, 27 Oct 2014 22:25:40 +0000 (15:25 -0700)
committerLuís Pereira <luis.artur.pereira@gmail.com>
Mon, 27 Oct 2014 22:25:40 +0000 (15:25 -0700)
We now have the ability to choose when to update (extract) the
translations from the sources files.

To update the translations set the CMake command line option
UPDATE_TRANSLATIONS to true. Ex: cmake -DUPDATE_TRASNLATIONS=Yes ..
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 LxQtTranslateTs module is part of the lxqt find_package(). The module
path is appended to CMAKE_MODULE_PATH, so the user only needs to include
the modules.

CMakeLists.txt
cmake/lxqt-config.cmake.in
cmake/lxqtX-config.cmake.in
cmake/lxqt_use.cmake.in
cmake/modules/LXQtTranslateTs.cmake [new file with mode: 0644]

index 72b5067..3c6a02a 100644 (file)
@@ -205,6 +205,7 @@ set(CFG_LXQT_INCLUDE_DIR    ${LXQT_INTREE_INCLUDE_DIR})
 set(CFG_LXQT_LIBRARY        ${LXQT_LIBRARY_NAME})
 set(CFG_LXQT_USE_FILE       ${CMAKE_BINARY_DIR}/${LXQT_LIBRARY_NAME}_use.cmake)
 set(CFG_LXQT_TARGETS_FILE   ${LXQT_INTREE_TARGETS_FILE})
+set(CFG_LXQT_CMAKE_MODULES_DIR "${PROJECT_SOURCE_DIR}/cmake/modules")
 
 configure_file(
     ${CMAKE_CURRENT_SOURCE_DIR}/cmake/lxqtX-config.cmake.in
@@ -228,6 +229,7 @@ set(CFG_LXQT_INCLUDE_DIR "${LXQT_INSTALL_INCLUDE_DIR}")
 set(CFG_LXQT_LIBRARY ${LXQT_LIBRARY_NAME})
 set(CFG_LXQT_USE_FILE "${LXQT_INSTALL_CMAKE_DIR}/${LXQT_LIBRARY_NAME}/${LXQT_LIBRARY_NAME}_use.cmake")
 set(CFG_LXQT_TARGETS_FILE "${LXQT_INSTALL_CMAKE_DIR}/${LXQT_LIBRARY_NAME}/${LXQT_LIBRARY_NAME}-targets.cmake")
+set(CFG_LXQT_CMAKE_MODULES_DIR "${LXQT_INSTALL_CMAKE_DIR}/${LXQT_LIBRARY_NAME}/modules")
 
 configure_file(
     "${CMAKE_CURRENT_SOURCE_DIR}/cmake/lxqtX-config.cmake.in"
@@ -263,6 +265,13 @@ install(EXPORT
     COMPONENT Devel
 )
 
+file(GLOB toInstallModules "${PROJECT_SOURCE_DIR}/cmake/modules/*.cmake")
+install(
+    FILES ${toInstallModules}
+    DESTINATION  "${LXQT_INSTALL_CMAKE_DIR}/${LXQT_LIBRARY_NAME}/modules"
+    COMPONENT Devel
+)
+
 
 include(translatorsinfo/CMakeLists.txt)
 get_translatorsinfo_qrc(translatorsinfo_qrc)
index 22cb637..6d1451b 100644 (file)
@@ -63,6 +63,7 @@ set(LXQT_LIBRARY_DIRS  @CMAKE_INSTALL_PREFIX@/@LIB_INSTALL_DIR@)
 set(LXQT_LIBRARIES     ${LXQT_LIBRARY} )
 set(LXQT_INCLUDE_DIRS  ${LXQT_INCLUDE_DIR} @CMAKE_INSTALL_PREFIX@/include)
 set(LXQT_USE_FILE      ${CMAKE_CURRENT_LIST_DIR}/lxqt_use.cmake)
+set(LXQT_CMAKE_MODULES_DIR @CFG_LXQT_CMAKE_MODULES_DIR@)
 set(LXQT_FOUND         1)
 
 if(LXQT_FIND_QUIETLY)
index fb74275..48354e4 100644 (file)
@@ -63,6 +63,7 @@ set(LXQT_SHARE_DIR          @LXQT_SHARE_DIR@)
 set(LXQT_RELATIVE_TRANSLATIONS_DIR   @LXQT_RELATIVE_TRANSLATIONS_DIR@)
 set(LXQT_TRANSLATIONS_DIR   @LXQT_TRANSLATIONS_DIR@)
 set(LXQT_ETC_XDG_DIR        @LXQT_ETC_XDG_DIR@)
+set(LXQT_CMAKE_MODULES_DIR  @CFG_LXQT_CMAKE_MODULES_DIR@)
 
 set(LXQT_INCLUDE_DIR        @CFG_LXQT_INCLUDE_DIR@)
 set(LXQT_INCLUDE_DIRS       @CFG_LXQT_INCLUDE_DIR@)
index dabc604..5da8dc7 100644 (file)
@@ -90,6 +90,8 @@ else()
     message(FATAL "Qt${LXQT_QT_VERSION} is not supported.")
 endif()
 
+list(APPEND CMAKE_MODULE_PATH ${LXQT_CMAKE_MODULES_DIR})
+
 # Works for Qt4 and Qt5
 include(${QTXDG_USE_FILE})
 
diff --git a/cmake/modules/LXQtTranslateTs.cmake b/cmake/modules/LXQtTranslateTs.cmake
new file mode 100644 (file)
index 0000000..d6821b1
--- /dev/null
@@ -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()