cmake: Don't needlessly sync translations
[quassel.git] / po / CMakeLists.txt
index d1df92c..8e5a45f 100644 (file)
@@ -1,72 +1,84 @@
-# Generate and add translations
-# The LINGUAS variable can be used to limit that set
+# Generate translations
+# The LINGUAS environment variable can be used to limit the set of supported languages
 
-if(QT_LCONVERT_EXECUTABLE)
-  # get environment variable for translations
-  set(LINGUAS "$ENV{LINGUAS}")
-  string(REGEX REPLACE "[ \t]+" \; output "${LINGUAS}")
-  # Find more languages
-  # We support xx.po and xx_YY.po, and additionally translations for qt using qt_xx.po or qt_xx_YY.po
-  file(GLOB avail_pofiles *.po)
-  foreach(PO_FILE ${avail_pofiles})
-    get_filename_component(basename ${PO_FILE} NAME_WE)
-    # CMake can't use MATCH to get the second catch...
-    string(REGEX REPLACE "(qt_)?([a-zA-Z]+)(_.+)?$" "\\2" lang ${basename})
+# Generate a .ts file from a .po file
+function(generate_ts basename)
+    set(pofile ${CMAKE_CURRENT_SOURCE_DIR}/${basename}.po)
+    set(tsfile ${CMAKE_CURRENT_BINARY_DIR}/${basename}.ts)
 
-    # test if we want this language
-    set(flg 1)
-    if(LINGUAS)
-      string(REGEX MATCH "${lang}" flg ${LINGUAS})
-    endif(LINGUAS)
-    if(flg)
-      generate_ts(QM ${basename})
-      generate_qm(QM ${basename})
-      list(APPEND qm_files ${QM})
-      list(APPEND gen_linguas ${lang})
-    endif(flg)
-  endforeach(PO_FILE ${avail_pofiles})
-  if(gen_linguas)
-    list(REMOVE_DUPLICATES gen_linguas)
-  endif(gen_linguas)
-  message(STATUS "Including languages: ${gen_linguas}")
-else(QT_LCONVERT_EXECUTABLE)
-  message(STATUS "WARNING: lconvert not found, you won't have translations!")
-endif(QT_LCONVERT_EXECUTABLE)
+    add_custom_command(VERBATIM
+        COMMENT "Preparing translation for language '${basename}'"
+        COMMAND $<TARGET_FILE:Qt5::lconvert> -i ${pofile} -target-language ${basename} -of ts -o ${tsfile}
+        DEPENDS ${pofile}
+        OUTPUT  ${tsfile}
+    )
+endfunction()
 
-# For a static or win32 build, we need to include Qt translations if available
-if(QT_TRANSLATIONS_DIR)
-  if(STATIC OR WIN32)
-    foreach(LANG ${gen_linguas})
-      file(GLOB lang_files ${QT_TRANSLATIONS_DIR}/qt_${LANG}*.qm)
-      foreach(absfile ${lang_files})
-        get_filename_component(filename ${absfile} NAME)
-        message(STATUS "Importing ${filename}")
-        configure_file(${absfile} ${CMAKE_CURRENT_BINARY_DIR}/${filename} COPYONLY)
-        list(APPEND qm_files ${CMAKE_CURRENT_BINARY_DIR}/${filename})
-      endforeach(absfile ${lang_files})
-    endforeach(LANG ${gen_linguas})
-  endif(STATIC OR WIN32)
-endif(QT_TRANSLATIONS_DIR)
+# Clear variables just in case
+set(tsfiles )
+set(qmfiles )
+set(linguas )
 
-# Write resource file
-set(resfile ${CMAKE_CURRENT_BINARY_DIR}/i18n.qrc)
-file(WRITE ${resfile} "<!DOCTYPE RCC><RCC version=\"1.0\">\n"
-                      "<qresource prefix=\"/i18n\">\n")
-foreach(file ${qm_files})
-  get_filename_component(file ${file} NAME)
-  file(APPEND ${resfile} "    <file>${file}</file>\n")
-endforeach(file ${qm_files})
-file(APPEND ${resfile} "</qresource>\n</RCC>\n")
+if (TARGET Qt5::lconvert)
+    # LINGUAS can be used to limit the included languages
+    set(LINGUAS "$ENV{LINGUAS}")
+    # Normalize and convert into list
+    string(REGEX REPLACE "[ \t,;]+" ";" LINGUAS "${LINGUAS}")
 
-#add_custom_command(OUTPUT ${resfile} DEPENDS ${qm_files})
-set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${qm_files};i18n.qrc")
+    # We support xx.po and xx_YY.po, and additionally translations for qt using qt_xx.po or qt_xx_YY.po
+    file(GLOB pofiles *.po)
+    foreach(pofile ${pofiles})
+        get_filename_component(basename ${pofile} NAME_WE)
+        # CMake can't use MATCH to get the second catch...
+        string(REGEX REPLACE "(qt_)?([a-zA-Z]+)(_.+)?$" "\\2" lang ${basename})
+        # Test if we want this language
+        set(idx 0)
+        if(LINGUAS)
+            list(FIND LINGUAS ${lang} idx)  # idx will be -1 if ${lang} is not found in LINGUAS
+        endif()
+        if(idx GREATER -1)
+            generate_ts(${basename})
+            list(APPEND tsfiles ${CMAKE_CURRENT_BINARY_DIR}/${basename}.ts)
+            list(APPEND qmfiles ${CMAKE_CURRENT_BINARY_DIR}/${basename}.qm)
+            list(APPEND linguas ${lang})
+        endif()
+    endforeach()
 
-if(EMBED_DATA)
-  set(COMMON_RCS ${COMMON_RCS} ${resfile} PARENT_SCOPE)
-else(EMBED_DATA)
-  install(FILES ${qm_files} DESTINATION ${DATA_INSTALL_DIR}/quassel/translations)
-endif(EMBED_DATA)
+    if (tsfiles)
+        # Synchronize the (possibly outdated) .ts files with the current source tree
+        add_custom_command(VERBATIM
+            COMMENT "Syncing translations"
+            COMMAND $<TARGET_FILE:Qt5::lupdate> -silent ${CMAKE_SOURCE_DIR}/src -ts ${tsfiles}
+            COMMAND ${CMAKE_COMMAND} -E touch tsfiles.done
+            DEPENDS ${tsfiles}
+            OUTPUT tsfiles.done
+        )
 
-add_custom_target(po DEPENDS ${qm_files})
+        # Generate the final translation files (.qm) for use by Qt
+        add_custom_command(VERBATIM
+            COMMENT "Compressing translations"
+            COMMAND $<TARGET_FILE:Qt5::lrelease> -silent ${tsfiles}
+            DEPENDS tsfiles.done
+            OUTPUT ${qmfiles}
+        )
 
+        # Curate the language list and give diagnostic output
+        list(REMOVE_DUPLICATES linguas)
+        list(SORT linguas)
+        string(REPLACE ";" ", " linguas_string "${linguas}")
+        message(STATUS "Enabling translations for: ${linguas_string}")
+    else()
+        message(STATUS "No translations enabled")
+    endif()
+else()
+    message(WARNING "Qt Linguist Tools not found, you won't have translations!")
+endif()
 
+# Always generate translations
+add_custom_target(translations ALL DEPENDS ${qmfiles})
+
+if (EMBED_DATA)
+    quassel_add_resource(I18n PREFIX i18n BASEDIR ${CMAKE_CURRENT_BINARY_DIR} PATTERNS ${qmfiles} DEPENDS translations)
+else()
+    install(FILES ${qmfiles} DESTINATION ${CMAKE_INSTALL_DATADIR}/quassel/translations)
+endif()