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