]> git.saurik.com Git - apt.git/blob - CMake/Translations.cmake
CMake: Translations: Add a statistics target
[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)
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 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.pot
79 COMMAND msgcomm --more-than=0 --sort-by-file
80 ${sh_pot}
81 ${PROJECT_BINARY_DIR}/${domain}.c.pot
82 --output=${PROJECT_BINARY_DIR}/${domain}.pot
83 DEPENDS ${sh_pot}
84 ${PROJECT_BINARY_DIR}/${domain}.c.pot
85 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
86 )
87
88 # Build .mo files
89 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
90 list(SORT translations)
91 foreach(file ${translations})
92 get_filename_component(langcode ${file} NAME_WE)
93 set(outdir ${PROJECT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES)
94 file(MAKE_DIRECTORY ${outdir})
95 # Command to merge and compile the messages
96 add_custom_command(OUTPUT ${outdir}/${domain}.po
97 COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${PROJECT_BINARY_DIR}/${domain}.pot
98 DEPENDS ${file} ${PROJECT_BINARY_DIR}/${domain}.pot
99 )
100 add_custom_command(OUTPUT ${outdir}/${domain}.mo
101 COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po
102 DEPENDS ${outdir}/${domain}.po
103 )
104
105 set(mofiles ${mofiles} ${outdir}/${domain}.mo)
106 install(FILES ${outdir}/${domain}.mo
107 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${langcode}/LC_MESSAGES")
108 endforeach(file ${translations})
109
110 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles})
111 endfunction()
112
113 # Usage: apt_add_update_po(output domain [domain ...])
114 function(apt_add_update_po)
115 set(options)
116 set(oneValueArgs TEMPLATE)
117 set(multiValueArgs DOMAINS)
118 cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
119 set(output ${CMAKE_CURRENT_SOURCE_DIR}/${NLS_TEMPLATE}.pot)
120 foreach(domain ${NLS_DOMAINS})
121 list(APPEND potfiles ${PROJECT_BINARY_DIR}/${domain}.pot)
122 endforeach()
123
124 get_filename_component(master_name ${output} NAME_WE)
125 add_custom_target(nls-${master_name}
126 COMMAND msgcomm --sort-by-file --add-location=file
127 --more-than=0 --output=${output}
128 ${potfiles}
129 DEPENDS ${potfiles})
130
131 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
132 if (NOT TARGET update-po)
133 add_custom_target(update-po)
134 endif()
135 foreach(translation ${translations})
136 get_filename_component(langcode ${translation} NAME_WE)
137 add_custom_target(update-po-${langcode}
138 COMMAND msgmerge -q --update --backup=none ${translation} ${output}
139 DEPENDS nls-${master_name}
140 )
141 add_dependencies(update-po update-po-${langcode})
142 endforeach()
143 add_dependencies(update-po nls-${master_name})
144 endfunction()
145
146 function(apt_add_po_statistics)
147 add_custom_target(statistics)
148 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
149 foreach(translation ${translations})
150 get_filename_component(langcode ${translation} NAME_WE)
151 add_custom_command(
152 TARGET statistics PRE_BUILD
153 COMMAND printf "%-7s" "${langcode}:"
154 COMMAND msgfmt --statistics -o /dev/null ${translation}
155 VERBATIM
156 )
157 endforeach()
158 endfunction()