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