1 # translations.cmake - Translations using APT's translation system.
2 # Copyright (C) 2009, 2016 Julian Andres Klode <jak@debian.org>
4 function(apt_add_translation_domain)
6 set(oneValueArgs DOMAIN)
7 set(multiValueArgs TARGETS SCRIPTS)
8 cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
9 # Build the list of source files of the target
14 set(targets ${NLS_TARGETS})
15 set(domain ${NLS_DOMAIN})
16 foreach(source ${NLS_SCRIPTS})
17 string(SUBSTRING ${source} 0 1 init_char)
18 string(COMPARE EQUAL ${init_char} "/" is_absolute)
22 set(file "${CMAKE_CURRENT_SOURCE_DIR}/${source}")
24 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
25 list(APPEND scripts ${relfile})
26 list(APPEND abs_scripts ${file})
28 foreach(target ${targets})
29 get_target_property(source_dir ${target} SOURCE_DIR)
30 get_target_property(sources ${target} SOURCES)
31 foreach(source ${sources})
32 string(SUBSTRING ${source} 0 1 init_char)
33 string(COMPARE EQUAL ${init_char} "/" is_absolute)
37 set(file "${source_dir}/${source}")
39 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
40 set(files ${files} ${relfile})
41 set(abs_files ${abs_files} ${file})
44 target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}")
47 if("${scripts}" STREQUAL "")
48 set(sh_pot "/dev/null")
50 set(sh_pot ${PROJECT_BINARY_DIR}/${domain}.sh.pot)
51 # Create the template for this specific sub-domain
52 add_custom_command (OUTPUT ${sh_pot}
53 COMMAND xgettext --add-comments --foreign -L Shell
54 -o ${sh_pot} ${scripts}
55 DEPENDS ${abs_scripts}
56 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
61 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.c.pot
62 COMMAND xgettext --add-comments --foreign -k_ -kN_
64 -o ${PROJECT_BINARY_DIR}/${domain}.c.pot ${files}
66 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
69 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.pot
70 COMMAND msgcomm --more-than=0 --sort-by-file
72 ${PROJECT_BINARY_DIR}/${domain}.c.pot
73 --output=${PROJECT_BINARY_DIR}/${domain}.pot
75 ${PROJECT_BINARY_DIR}/${domain}.c.pot
76 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
80 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
81 list(SORT translations)
82 foreach(file ${translations})
83 get_filename_component(langcode ${file} NAME_WE)
84 set(outdir ${PROJECT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES)
85 file(MAKE_DIRECTORY ${outdir})
86 # Command to merge and compile the messages
87 add_custom_command(OUTPUT ${outdir}/${domain}.po
88 COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${PROJECT_BINARY_DIR}/${domain}.pot
89 DEPENDS ${file} ${PROJECT_BINARY_DIR}/${domain}.pot
91 add_custom_command(OUTPUT ${outdir}/${domain}.mo
92 COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po
93 DEPENDS ${outdir}/${domain}.po
96 set(mofiles ${mofiles} ${outdir}/${domain}.mo)
97 install(FILES ${outdir}/${domain}.mo
98 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${langcode}/LC_MESSAGES")
99 endforeach(file ${translations})
101 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles})