]>
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 | 23 | foreach(source ${NLS_SCRIPTS}) |
0d04a498 | 24 | path_join(file "${CMAKE_CURRENT_SOURCE_DIR}" "${source}") |
6ff8727a JAK |
25 | file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file}) |
26 | list(APPEND scripts ${relfile}) | |
27 | list(APPEND abs_scripts ${file}) | |
28 | endforeach() | |
7def2482 JAK |
29 | foreach(target ${targets}) |
30 | get_target_property(source_dir ${target} SOURCE_DIR) | |
31 | get_target_property(sources ${target} SOURCES) | |
32 | foreach(source ${sources}) | |
0d04a498 | 33 | path_join(file "${source_dir}" "${source}") |
7def2482 JAK |
34 | file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file}) |
35 | set(files ${files} ${relfile}) | |
ba69ce6d | 36 | set(abs_files ${abs_files} ${file}) |
7def2482 JAK |
37 | endforeach() |
38 | ||
39 | target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}") | |
40 | endforeach() | |
41 | ||
6ff8727a JAK |
42 | if("${scripts}" STREQUAL "") |
43 | set(sh_pot "/dev/null") | |
44 | else() | |
173fa882 | 45 | set(sh_pot ${CMAKE_CURRENT_BINARY_DIR}/${domain}.sh.pot) |
6ff8727a JAK |
46 | # Create the template for this specific sub-domain |
47 | add_custom_command (OUTPUT ${sh_pot} | |
9a5537fc | 48 | COMMAND xgettext ${xgettext_params} -L Shell |
6ff8727a JAK |
49 | -o ${sh_pot} ${scripts} |
50 | DEPENDS ${abs_scripts} | |
9a5537fc | 51 | VERBATIM |
6ff8727a JAK |
52 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} |
53 | ) | |
54 | endif() | |
55 | ||
56 | ||
173fa882 | 57 | add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot |
9a5537fc | 58 | COMMAND xgettext ${xgettext_params} -k_ -kN_ |
6ff8727a | 59 | --keyword=P_:1,2 |
173fa882 | 60 | -o ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot ${files} |
ba69ce6d | 61 | DEPENDS ${abs_files} |
9a5537fc | 62 | VERBATIM |
7def2482 JAK |
63 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} |
64 | ) | |
65 | ||
e164341c JAK |
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. | |
ade5888b JAK |
70 | # |
71 | # Make sure the .pot-tmp has no line numbers, to avoid useless rebuilding | |
72 | # of .mo files. | |
173fa882 JAK |
73 | add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot |
74 | BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp | |
ade5888b | 75 | COMMAND msgcomm --more-than=0 --omit-header --sort-by-file --add-location=file |
e164341c | 76 | ${sh_pot} |
173fa882 JAK |
77 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot |
78 | --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0 | |
c43a7ed4 JAK |
79 | COMMAND msgcomm --more-than=0 --sort-by-file |
80 | ${sh_pot} | |
81 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot | |
82 | --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot | |
e164341c | 83 | COMMAND cmake -E copy_if_different |
173fa882 JAK |
84 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0 |
85 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp | |
6ff8727a | 86 | DEPENDS ${sh_pot} |
173fa882 | 87 | ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot |
6ff8727a JAK |
88 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} |
89 | ) | |
90 | ||
e164341c JAK |
91 | # We need a target to depend on otherwise, the msgmerge might not get called |
92 | # with the make generator | |
173fa882 | 93 | add_custom_target(nls-${domain}-template DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot) |
e164341c | 94 | |
7def2482 JAK |
95 | # Build .mo files |
96 | file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po") | |
97 | list(SORT translations) | |
98 | foreach(file ${translations}) | |
99 | get_filename_component(langcode ${file} NAME_WE) | |
32a32d75 JAK |
100 | if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES) |
101 | continue() | |
102 | endif() | |
173fa882 | 103 | set(outdir ${CMAKE_CURRENT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES) |
7def2482 | 104 | file(MAKE_DIRECTORY ${outdir}) |
e164341c JAK |
105 | # Command to merge and compile the messages. As explained in the custom |
106 | # command for msgcomm, this depends on byproduct to avoid reruns | |
a331fb70 | 107 | add_custom_command(OUTPUT ${outdir}/${domain}.po |
173fa882 JAK |
108 | COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp |
109 | DEPENDS ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp | |
7def2482 | 110 | ) |
a331fb70 JAK |
111 | add_custom_command(OUTPUT ${outdir}/${domain}.mo |
112 | COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po | |
113 | DEPENDS ${outdir}/${domain}.po | |
114 | ) | |
7def2482 JAK |
115 | |
116 | set(mofiles ${mofiles} ${outdir}/${domain}.mo) | |
117 | install(FILES ${outdir}/${domain}.mo | |
118 | DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${langcode}/LC_MESSAGES") | |
119 | endforeach(file ${translations}) | |
120 | ||
e164341c | 121 | add_custom_target(nls-${domain} ALL DEPENDS ${mofiles} nls-${domain}-template) |
7def2482 | 122 | endfunction() |
9a5537fc JAK |
123 | |
124 | # Usage: apt_add_update_po(output domain [domain ...]) | |
125 | function(apt_add_update_po) | |
126 | set(options) | |
127 | set(oneValueArgs TEMPLATE) | |
32a32d75 | 128 | set(multiValueArgs DOMAINS EXCLUDE_LANGUAGES) |
9a5537fc JAK |
129 | cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
130 | set(output ${CMAKE_CURRENT_SOURCE_DIR}/${NLS_TEMPLATE}.pot) | |
131 | foreach(domain ${NLS_DOMAINS}) | |
173fa882 | 132 | list(APPEND potfiles ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot) |
9a5537fc JAK |
133 | endforeach() |
134 | ||
135 | get_filename_component(master_name ${output} NAME_WE) | |
136 | add_custom_target(nls-${master_name} | |
137 | COMMAND msgcomm --sort-by-file --add-location=file | |
138 | --more-than=0 --output=${output} | |
139 | ${potfiles} | |
140 | DEPENDS ${potfiles}) | |
141 | ||
142 | file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po") | |
143 | if (NOT TARGET update-po) | |
144 | add_custom_target(update-po) | |
145 | endif() | |
146 | foreach(translation ${translations}) | |
147 | get_filename_component(langcode ${translation} NAME_WE) | |
32a32d75 JAK |
148 | if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES) |
149 | continue() | |
150 | endif() | |
9a5537fc | 151 | add_custom_target(update-po-${langcode} |
0eaa491c | 152 | COMMAND msgmerge -q --previous --update --backup=none ${translation} ${output} |
9a5537fc JAK |
153 | DEPENDS nls-${master_name} |
154 | ) | |
155 | add_dependencies(update-po update-po-${langcode}) | |
156 | endforeach() | |
157 | add_dependencies(update-po nls-${master_name}) | |
158 | endfunction() | |
ddf40a42 | 159 | |
32a32d75 | 160 | function(apt_add_po_statistics excluded) |
ddf40a42 JAK |
161 | add_custom_target(statistics) |
162 | file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po") | |
163 | foreach(translation ${translations}) | |
164 | get_filename_component(langcode ${translation} NAME_WE) | |
32a32d75 JAK |
165 | if ("${langcode}" IN_LIST excluded) |
166 | add_custom_command( | |
167 | TARGET statistics PRE_BUILD | |
168 | COMMAND printf "%-6s " "${langcode}:" | |
169 | COMMAND echo "ignored" | |
170 | VERBATIM | |
171 | ) | |
172 | continue() | |
173 | endif() | |
ddf40a42 JAK |
174 | add_custom_command( |
175 | TARGET statistics PRE_BUILD | |
32a32d75 | 176 | COMMAND printf "%-6s " "${langcode}:" |
ddf40a42 JAK |
177 | COMMAND msgfmt --statistics -o /dev/null ${translation} |
178 | VERBATIM | |
179 | ) | |
180 | endforeach() | |
181 | endfunction() |