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 string(SUBSTRING ${source} 0 1 init_char)
25 string(COMPARE EQUAL ${init_char} "/" is_absolute)
29 set(file "${CMAKE_CURRENT_SOURCE_DIR}/${source}")
31 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
32 list(APPEND scripts ${relfile})
33 list(APPEND abs_scripts ${file})
35 foreach(target ${targets})
36 get_target_property(source_dir ${target} SOURCE_DIR)
37 get_target_property(sources ${target} SOURCES)
38 foreach(source ${sources})
39 string(SUBSTRING ${source} 0 1 init_char)
40 string(COMPARE EQUAL ${init_char} "/" is_absolute)
44 set(file "${source_dir}/${source}")
46 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
47 set(files ${files} ${relfile})
48 set(abs_files ${abs_files} ${file})
51 target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}")
54 if("${scripts}" STREQUAL "")
55 set(sh_pot "/dev/null")
57 set(sh_pot ${PROJECT_BINARY_DIR}/${domain}.sh.pot)
58 # Create the template for this specific sub-domain
59 add_custom_command (OUTPUT ${sh_pot}
60 COMMAND xgettext ${xgettext_params} -L Shell
61 -o ${sh_pot} ${scripts}
62 DEPENDS ${abs_scripts}
64 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
69 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.c.pot
70 COMMAND xgettext ${xgettext_params} -k_ -kN_
72 -o ${PROJECT_BINARY_DIR}/${domain}.c.pot ${files}
75 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
78 # We are building a ${domain}.pot with a header for launchpad, but we also
79 # build a ${domain.pot}-tmp as a byproduct. The msgfmt command than depend
80 # on the byproduct while their target depends on the output, so that msgfmt
81 # does not have to be rerun if nothing in the template changed.
82 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.pot
83 BYPRODUCTS ${PROJECT_BINARY_DIR}/${domain}.pot-tmp
84 COMMAND msgcomm --more-than=0 --sort-by-file
86 ${PROJECT_BINARY_DIR}/${domain}.c.pot
87 --output=${PROJECT_BINARY_DIR}/${domain}.pot
88 COMMAND msgcomm --more-than=0 --omit-header --sort-by-file
90 ${PROJECT_BINARY_DIR}/${domain}.c.pot
91 --output=${PROJECT_BINARY_DIR}/${domain}.pot-tmp0
92 COMMAND cmake -E copy_if_different
93 ${PROJECT_BINARY_DIR}/${domain}.pot-tmp0
94 ${PROJECT_BINARY_DIR}/${domain}.pot-tmp
96 ${PROJECT_BINARY_DIR}/${domain}.c.pot
97 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
100 # We need a target to depend on otherwise, the msgmerge might not get called
101 # with the make generator
102 add_custom_target(nls-${domain}-template DEPENDS ${PROJECT_BINARY_DIR}/${domain}.pot)
105 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
106 list(SORT translations)
107 foreach(file ${translations})
108 get_filename_component(langcode ${file} NAME_WE)
109 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
112 set(outdir ${PROJECT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES)
113 file(MAKE_DIRECTORY ${outdir})
114 # Command to merge and compile the messages. As explained in the custom
115 # command for msgcomm, this depends on byproduct to avoid reruns
116 add_custom_command(OUTPUT ${outdir}/${domain}.po
117 COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${PROJECT_BINARY_DIR}/${domain}.pot-tmp
118 DEPENDS ${file} ${PROJECT_BINARY_DIR}/${domain}.pot-tmp
120 add_custom_command(OUTPUT ${outdir}/${domain}.mo
121 COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po
122 DEPENDS ${outdir}/${domain}.po
125 set(mofiles ${mofiles} ${outdir}/${domain}.mo)
126 install(FILES ${outdir}/${domain}.mo
127 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${langcode}/LC_MESSAGES")
128 endforeach(file ${translations})
130 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles} nls-${domain}-template)
133 # Usage: apt_add_update_po(output domain [domain ...])
134 function(apt_add_update_po)
136 set(oneValueArgs TEMPLATE)
137 set(multiValueArgs DOMAINS EXCLUDE_LANGUAGES)
138 cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
139 set(output ${CMAKE_CURRENT_SOURCE_DIR}/${NLS_TEMPLATE}.pot)
140 foreach(domain ${NLS_DOMAINS})
141 list(APPEND potfiles ${PROJECT_BINARY_DIR}/${domain}.pot)
144 get_filename_component(master_name ${output} NAME_WE)
145 add_custom_target(nls-${master_name}
146 COMMAND msgcomm --sort-by-file --add-location=file
147 --more-than=0 --output=${output}
151 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
152 if (NOT TARGET update-po)
153 add_custom_target(update-po)
155 foreach(translation ${translations})
156 get_filename_component(langcode ${translation} NAME_WE)
157 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
160 add_custom_target(update-po-${langcode}
161 COMMAND msgmerge -q --update --backup=none ${translation} ${output}
162 DEPENDS nls-${master_name}
164 add_dependencies(update-po update-po-${langcode})
166 add_dependencies(update-po nls-${master_name})
169 function(apt_add_po_statistics excluded)
170 add_custom_target(statistics)
171 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
172 foreach(translation ${translations})
173 get_filename_component(langcode ${translation} NAME_WE)
174 if ("${langcode}" IN_LIST excluded)
176 TARGET statistics PRE_BUILD
177 COMMAND printf "%-6s " "${langcode}:"
178 COMMAND echo "ignored"
184 TARGET statistics PRE_BUILD
185 COMMAND printf "%-6s " "${langcode}:"
186 COMMAND msgfmt --statistics -o /dev/null ${translation}