]>
Commit | Line | Data |
---|---|---|
7def2482 JAK |
1 | # translations.cmake - Translations using APT's translation system. |
2 | # Copyright (C) 2009, 2016 Julian Andres Klode <jak@debian.org> | |
3 | ||
6ff8727a JAK |
4 | function(apt_add_translation_domain) |
5 | set(options) | |
6 | set(oneValueArgs DOMAIN) | |
32a32d75 | 7 | set(multiValueArgs TARGETS SCRIPTS EXCLUDE_LANGUAGES) |
6ff8727a | 8 | cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
7def2482 JAK |
9 | # Build the list of source files of the target |
10 | set(files "") | |
ba69ce6d | 11 | set(abs_files "") |
6ff8727a JAK |
12 | set(scripts "") |
13 | set(abs_scripts "") | |
14 | set(targets ${NLS_TARGETS}) | |
15 | set(domain ${NLS_DOMAIN}) | |
9a5537fc JAK |
16 | set(xgettext_params |
17 | --add-comments | |
18 | --foreign | |
19 | --package-name=${PROJECT_NAME} | |
20 | --package-version=${PACKAGE_VERSION} | |
21 | --msgid-bugs-address=${PACKAGE_MAIL} | |
22 | ) | |
6ff8727a JAK |
23 | foreach(source ${NLS_SCRIPTS}) |
24 | string(SUBSTRING ${source} 0 1 init_char) | |
25 | string(COMPARE EQUAL ${init_char} "/" is_absolute) | |
26 | if (${is_absolute}) | |
27 | set(file "${source}") | |
28 | else() | |
29 | set(file "${CMAKE_CURRENT_SOURCE_DIR}/${source}") | |
30 | endif() | |
31 | file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file}) | |
32 | list(APPEND scripts ${relfile}) | |
33 | list(APPEND abs_scripts ${file}) | |
34 | endforeach() | |
7def2482 JAK |
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) | |
41 | if (${is_absolute}) | |
42 | set(file "${source}") | |
43 | else() | |
44 | set(file "${source_dir}/${source}") | |
45 | endif() | |
46 | file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file}) | |
47 | set(files ${files} ${relfile}) | |
ba69ce6d | 48 | set(abs_files ${abs_files} ${file}) |
7def2482 JAK |
49 | endforeach() |
50 | ||
51 | target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}") | |
52 | endforeach() | |
53 | ||
6ff8727a JAK |
54 | if("${scripts}" STREQUAL "") |
55 | set(sh_pot "/dev/null") | |
56 | else() | |
173fa882 | 57 | set(sh_pot ${CMAKE_CURRENT_BINARY_DIR}/${domain}.sh.pot) |
6ff8727a JAK |
58 | # Create the template for this specific sub-domain |
59 | add_custom_command (OUTPUT ${sh_pot} | |
9a5537fc | 60 | COMMAND xgettext ${xgettext_params} -L Shell |
6ff8727a JAK |
61 | -o ${sh_pot} ${scripts} |
62 | DEPENDS ${abs_scripts} | |
9a5537fc | 63 | VERBATIM |
6ff8727a JAK |
64 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} |
65 | ) | |
66 | endif() | |
67 | ||
68 | ||
173fa882 | 69 | add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot |
9a5537fc | 70 | COMMAND xgettext ${xgettext_params} -k_ -kN_ |
6ff8727a | 71 | --keyword=P_:1,2 |
173fa882 | 72 | -o ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot ${files} |
ba69ce6d | 73 | DEPENDS ${abs_files} |
9a5537fc | 74 | VERBATIM |
7def2482 JAK |
75 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} |
76 | ) | |
77 | ||
e164341c JAK |
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. | |
173fa882 JAK |
82 | add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot |
83 | BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp | |
6ff8727a JAK |
84 | COMMAND msgcomm --more-than=0 --sort-by-file |
85 | ${sh_pot} | |
173fa882 JAK |
86 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot |
87 | --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot | |
e164341c JAK |
88 | COMMAND msgcomm --more-than=0 --omit-header --sort-by-file |
89 | ${sh_pot} | |
173fa882 JAK |
90 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot |
91 | --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0 | |
e164341c | 92 | COMMAND cmake -E copy_if_different |
173fa882 JAK |
93 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0 |
94 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp | |
6ff8727a | 95 | DEPENDS ${sh_pot} |
173fa882 | 96 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot |
6ff8727a JAK |
97 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} |
98 | ) | |
99 | ||
e164341c JAK |
100 | # We need a target to depend on otherwise, the msgmerge might not get called |
101 | # with the make generator | |
173fa882 | 102 | add_custom_target(nls-${domain}-template DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot) |
e164341c | 103 | |
7def2482 JAK |
104 | # Build .mo files |
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) | |
32a32d75 JAK |
109 | if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES) |
110 | continue() | |
111 | endif() | |
173fa882 | 112 | set(outdir ${CMAKE_CURRENT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES) |
7def2482 | 113 | file(MAKE_DIRECTORY ${outdir}) |
e164341c JAK |
114 | # Command to merge and compile the messages. As explained in the custom |
115 | # command for msgcomm, this depends on byproduct to avoid reruns | |
a331fb70 | 116 | add_custom_command(OUTPUT ${outdir}/${domain}.po |
173fa882 JAK |
117 | COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp |
118 | DEPENDS ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp | |
7def2482 | 119 | ) |
a331fb70 JAK |
120 | add_custom_command(OUTPUT ${outdir}/${domain}.mo |
121 | COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po | |
122 | DEPENDS ${outdir}/${domain}.po | |
123 | ) | |
7def2482 JAK |
124 | |
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}) | |
129 | ||
e164341c | 130 | add_custom_target(nls-${domain} ALL DEPENDS ${mofiles} nls-${domain}-template) |
7def2482 | 131 | endfunction() |
9a5537fc JAK |
132 | |
133 | # Usage: apt_add_update_po(output domain [domain ...]) | |
134 | function(apt_add_update_po) | |
135 | set(options) | |
136 | set(oneValueArgs TEMPLATE) | |
32a32d75 | 137 | set(multiValueArgs DOMAINS EXCLUDE_LANGUAGES) |
9a5537fc JAK |
138 | cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
139 | set(output ${CMAKE_CURRENT_SOURCE_DIR}/${NLS_TEMPLATE}.pot) | |
140 | foreach(domain ${NLS_DOMAINS}) | |
173fa882 | 141 | list(APPEND potfiles ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot) |
9a5537fc JAK |
142 | endforeach() |
143 | ||
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} | |
148 | ${potfiles} | |
149 | DEPENDS ${potfiles}) | |
150 | ||
151 | file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po") | |
152 | if (NOT TARGET update-po) | |
153 | add_custom_target(update-po) | |
154 | endif() | |
155 | foreach(translation ${translations}) | |
156 | get_filename_component(langcode ${translation} NAME_WE) | |
32a32d75 JAK |
157 | if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES) |
158 | continue() | |
159 | endif() | |
9a5537fc JAK |
160 | add_custom_target(update-po-${langcode} |
161 | COMMAND msgmerge -q --update --backup=none ${translation} ${output} | |
162 | DEPENDS nls-${master_name} | |
163 | ) | |
164 | add_dependencies(update-po update-po-${langcode}) | |
165 | endforeach() | |
166 | add_dependencies(update-po nls-${master_name}) | |
167 | endfunction() | |
ddf40a42 | 168 | |
32a32d75 | 169 | function(apt_add_po_statistics excluded) |
ddf40a42 JAK |
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) | |
32a32d75 JAK |
174 | if ("${langcode}" IN_LIST excluded) |
175 | add_custom_command( | |
176 | TARGET statistics PRE_BUILD | |
177 | COMMAND printf "%-6s " "${langcode}:" | |
178 | COMMAND echo "ignored" | |
179 | VERBATIM | |
180 | ) | |
181 | continue() | |
182 | endif() | |
ddf40a42 JAK |
183 | add_custom_command( |
184 | TARGET statistics PRE_BUILD | |
32a32d75 | 185 | COMMAND printf "%-6s " "${langcode}:" |
ddf40a42 JAK |
186 | COMMAND msgfmt --statistics -o /dev/null ${translation} |
187 | VERBATIM | |
188 | ) | |
189 | endforeach() | |
190 | endfunction() |