1 # translations.cmake - Translations using APT's translation system.
2 # Copyright (C) 2009, 2016 Julian Andres Klode <jak@debian.org>
4 function(apt_add_translation_domain)
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
14 set(targets ${NLS_TARGETS})
15 set(domain ${NLS_DOMAIN})
19 --package-name=${PROJECT_NAME}
20 --package-version=${PACKAGE_VERSION}
21 --msgid-bugs-address=${PACKAGE_MAIL}
23 foreach(source ${NLS_SCRIPTS})
24 string(SUBSTRING ${source} 0 1 init_char)
25 string(COMPARE EQUAL ${init_char} "/" is_absolute)
29 set(file "${CMAKE_CURRENT_SOURCE_DIR}/${source}")
31 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
32 list(APPEND scripts ${relfile})
33 list(APPEND abs_scripts ${file})
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)
44 set(file "${source_dir}/${source}")
46 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
47 set(files ${files} ${relfile})
48 set(abs_files ${abs_files} ${file})
51 target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}")
54 if("${scripts}" STREQUAL "")
55 set(sh_pot "/dev/null")
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}
64 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
69 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.c.pot
70 COMMAND xgettext ${xgettext_params} -k_ -kN_
72 -o ${PROJECT_BINARY_DIR}/${domain}.c.pot ${files}
75 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
78 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.pot
79 COMMAND msgcomm --more-than=0 --sort-by-file
81 ${PROJECT_BINARY_DIR}/${domain}.c.pot
82 --output=${PROJECT_BINARY_DIR}/${domain}.pot
84 ${PROJECT_BINARY_DIR}/${domain}.c.pot
85 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
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
100 add_custom_command(OUTPUT ${outdir}/${domain}.mo
101 COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po
102 DEPENDS ${outdir}/${domain}.po
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})
110 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles})
113 # Usage: apt_add_update_po(output domain [domain ...])
114 function(apt_add_update_po)
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)
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}
131 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
132 if (NOT TARGET update-po)
133 add_custom_target(update-po)
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}
141 add_dependencies(update-po update-po-${langcode})
143 add_dependencies(update-po nls-${master_name})
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)
152 TARGET statistics PRE_BUILD
153 COMMAND printf "%-7s" "${langcode}:"
154 COMMAND msgfmt --statistics -o /dev/null ${translation}