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 EXCLUDE_LANGUAGES)
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})
19 --package-name=${PROJECT_NAME}
20 --package-version=${PACKAGE_VERSION}
21 --msgid-bugs-address=${PACKAGE_MAIL}
23 foreach(source ${NLS_SCRIPTS})
24 path_join(file "${CMAKE_CURRENT_SOURCE_DIR}" "${source}")
25 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
26 list(APPEND scripts ${relfile})
27 list(APPEND abs_scripts ${file})
29 foreach(target ${targets})
30 get_target_property(source_dir ${target} SOURCE_DIR)
31 get_target_property(sources ${target} SOURCES)
32 foreach(source ${sources})
33 path_join(file "${source_dir}" "${source}")
34 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
35 set(files ${files} ${relfile})
36 set(abs_files ${abs_files} ${file})
39 target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}")
42 if("${scripts}" STREQUAL "")
43 set(sh_pot "/dev/null")
45 set(sh_pot ${CMAKE_CURRENT_BINARY_DIR}/${domain}.sh.pot)
46 # Create the template for this specific sub-domain
47 add_custom_command (OUTPUT ${sh_pot}
48 COMMAND xgettext ${xgettext_params} -L Shell
49 -o ${sh_pot} ${scripts}
50 DEPENDS ${abs_scripts}
52 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
57 add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
58 COMMAND xgettext ${xgettext_params} -k_ -kN_
60 -o ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot ${files}
63 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
66 # We are building a ${domain}.pot with a header for launchpad, but we also
67 # build a ${domain.pot}-tmp as a byproduct. The msgfmt command than depend
68 # on the byproduct while their target depends on the output, so that msgfmt
69 # does not have to be rerun if nothing in the template changed.
70 add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot
71 BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
72 COMMAND msgcomm --more-than=0 --sort-by-file
74 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
75 --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot
76 COMMAND msgcomm --more-than=0 --omit-header --sort-by-file
78 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
79 --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0
80 COMMAND cmake -E copy_if_different
81 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0
82 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
84 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
85 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
88 # We need a target to depend on otherwise, the msgmerge might not get called
89 # with the make generator
90 add_custom_target(nls-${domain}-template DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot)
93 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
94 list(SORT translations)
95 foreach(file ${translations})
96 get_filename_component(langcode ${file} NAME_WE)
97 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
100 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES)
101 file(MAKE_DIRECTORY ${outdir})
102 # Command to merge and compile the messages. As explained in the custom
103 # command for msgcomm, this depends on byproduct to avoid reruns
104 add_custom_command(OUTPUT ${outdir}/${domain}.po
105 COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
106 DEPENDS ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
108 add_custom_command(OUTPUT ${outdir}/${domain}.mo
109 COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po
110 DEPENDS ${outdir}/${domain}.po
113 set(mofiles ${mofiles} ${outdir}/${domain}.mo)
114 install(FILES ${outdir}/${domain}.mo
115 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${langcode}/LC_MESSAGES")
116 endforeach(file ${translations})
118 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles} nls-${domain}-template)
121 # Usage: apt_add_update_po(output domain [domain ...])
122 function(apt_add_update_po)
124 set(oneValueArgs TEMPLATE)
125 set(multiValueArgs DOMAINS EXCLUDE_LANGUAGES)
126 cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
127 set(output ${CMAKE_CURRENT_SOURCE_DIR}/${NLS_TEMPLATE}.pot)
128 foreach(domain ${NLS_DOMAINS})
129 list(APPEND potfiles ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot)
132 get_filename_component(master_name ${output} NAME_WE)
133 add_custom_target(nls-${master_name}
134 COMMAND msgcomm --sort-by-file --add-location=file
135 --more-than=0 --output=${output}
139 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
140 if (NOT TARGET update-po)
141 add_custom_target(update-po)
143 foreach(translation ${translations})
144 get_filename_component(langcode ${translation} NAME_WE)
145 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
148 add_custom_target(update-po-${langcode}
149 COMMAND msgmerge -q --update --backup=none ${translation} ${output}
150 DEPENDS nls-${master_name}
152 add_dependencies(update-po update-po-${langcode})
154 add_dependencies(update-po nls-${master_name})
157 function(apt_add_po_statistics excluded)
158 add_custom_target(statistics)
159 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
160 foreach(translation ${translations})
161 get_filename_component(langcode ${translation} NAME_WE)
162 if ("${langcode}" IN_LIST excluded)
164 TARGET statistics PRE_BUILD
165 COMMAND printf "%-6s " "${langcode}:"
166 COMMAND echo "ignored"
172 TARGET statistics PRE_BUILD
173 COMMAND printf "%-6s " "${langcode}:"
174 COMMAND msgfmt --statistics -o /dev/null ${translation}