8e5a45f73ca9de478f20f8ae37ad47ea78a8fd80
[quassel.git] / po / CMakeLists.txt
1 # Generate translations
2 # The LINGUAS environment variable can be used to limit the set of supported languages
3
4 # Generate a .ts file from a .po file
5 function(generate_ts basename)
6     set(pofile ${CMAKE_CURRENT_SOURCE_DIR}/${basename}.po)
7     set(tsfile ${CMAKE_CURRENT_BINARY_DIR}/${basename}.ts)
8
9     add_custom_command(VERBATIM
10         COMMENT "Preparing translation for language '${basename}'"
11         COMMAND $<TARGET_FILE:Qt5::lconvert> -i ${pofile} -target-language ${basename} -of ts -o ${tsfile}
12         DEPENDS ${pofile}
13         OUTPUT  ${tsfile}
14     )
15 endfunction()
16
17 # Clear variables just in case
18 set(tsfiles )
19 set(qmfiles )
20 set(linguas )
21
22 if (TARGET Qt5::lconvert)
23     # LINGUAS can be used to limit the included languages
24     set(LINGUAS "$ENV{LINGUAS}")
25     # Normalize and convert into list
26     string(REGEX REPLACE "[ \t,;]+" ";" LINGUAS "${LINGUAS}")
27
28     # We support xx.po and xx_YY.po, and additionally translations for qt using qt_xx.po or qt_xx_YY.po
29     file(GLOB pofiles *.po)
30     foreach(pofile ${pofiles})
31         get_filename_component(basename ${pofile} NAME_WE)
32         # CMake can't use MATCH to get the second catch...
33         string(REGEX REPLACE "(qt_)?([a-zA-Z]+)(_.+)?$" "\\2" lang ${basename})
34         # Test if we want this language
35         set(idx 0)
36         if(LINGUAS)
37             list(FIND LINGUAS ${lang} idx)  # idx will be -1 if ${lang} is not found in LINGUAS
38         endif()
39         if(idx GREATER -1)
40             generate_ts(${basename})
41             list(APPEND tsfiles ${CMAKE_CURRENT_BINARY_DIR}/${basename}.ts)
42             list(APPEND qmfiles ${CMAKE_CURRENT_BINARY_DIR}/${basename}.qm)
43             list(APPEND linguas ${lang})
44         endif()
45     endforeach()
46
47     if (tsfiles)
48         # Synchronize the (possibly outdated) .ts files with the current source tree
49         add_custom_command(VERBATIM
50             COMMENT "Syncing translations"
51             COMMAND $<TARGET_FILE:Qt5::lupdate> -silent ${CMAKE_SOURCE_DIR}/src -ts ${tsfiles}
52             COMMAND ${CMAKE_COMMAND} -E touch tsfiles.done
53             DEPENDS ${tsfiles}
54             OUTPUT tsfiles.done
55         )
56
57         # Generate the final translation files (.qm) for use by Qt
58         add_custom_command(VERBATIM
59             COMMENT "Compressing translations"
60             COMMAND $<TARGET_FILE:Qt5::lrelease> -silent ${tsfiles}
61             DEPENDS tsfiles.done
62             OUTPUT ${qmfiles}
63         )
64
65         # Curate the language list and give diagnostic output
66         list(REMOVE_DUPLICATES linguas)
67         list(SORT linguas)
68         string(REPLACE ";" ", " linguas_string "${linguas}")
69         message(STATUS "Enabling translations for: ${linguas_string}")
70     else()
71         message(STATUS "No translations enabled")
72     endif()
73 else()
74     message(WARNING "Qt Linguist Tools not found, you won't have translations!")
75 endif()
76
77 # Always generate translations
78 add_custom_target(translations ALL DEPENDS ${qmfiles})
79
80 if (EMBED_DATA)
81     quassel_add_resource(I18n PREFIX i18n BASEDIR ${CMAKE_CURRENT_BINARY_DIR} PATTERNS ${qmfiles} DEPENDS translations)
82 else()
83     install(FILES ${qmfiles} DESTINATION ${CMAKE_INSTALL_DATADIR}/quassel/translations)
84 endif()