]> git.saurik.com Git - apt.git/blob - CMake/Translations.cmake
9880e68c79d4a18e56eb9cfe420cc74ec430655a
[apt.git] / CMake / Translations.cmake
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(targets ${NLS_TARGETS})
15 set(domain ${NLS_DOMAIN})
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 )
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()
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})
48 set(abs_files ${abs_files} ${file})
49 endforeach()
50
51 target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}")
52 endforeach()
53
54 if("${scripts}" STREQUAL "")
55 set(sh_pot "/dev/null")
56 else()
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}
63 VERBATIM
64 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
65 )
66 endif()
67
68
69 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.c.pot
70 COMMAND xgettext ${xgettext_params} -k_ -kN_
71 --keyword=P_:1,2
72 -o ${PROJECT_BINARY_DIR}/${domain}.c.pot ${files}
73 DEPENDS ${abs_files}
74 VERBATIM
75 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
76 )
77
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
85 ${sh_pot}
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
89 ${sh_pot}
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
95 DEPENDS ${sh_pot}
96 ${PROJECT_BINARY_DIR}/${domain}.c.pot
97 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
98 )
99
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)
103
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)
109 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
110 continue()
111 endif()
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
119 )
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 )
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
130 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles} nls-${domain}-template)
131 endfunction()
132
133 # Usage: apt_add_update_po(output domain [domain ...])
134 function(apt_add_update_po)
135 set(options)
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)
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)
157 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
158 continue()
159 endif()
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()
168
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)
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()
183 add_custom_command(
184 TARGET statistics PRE_BUILD
185 COMMAND printf "%-6s " "${langcode}:"
186 COMMAND msgfmt --statistics -o /dev/null ${translation}
187 VERBATIM
188 )
189 endforeach()
190 endfunction()