]> git.saurik.com Git - apt.git/blob - CMake/Translations.cmake
1b9781d697a8c1097e6c9c550ede19942eff93d4
[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 foreach(source ${NLS_SCRIPTS})
17 string(SUBSTRING ${source} 0 1 init_char)
18 string(COMPARE EQUAL ${init_char} "/" is_absolute)
19 if (${is_absolute})
20 set(file "${source}")
21 else()
22 set(file "${CMAKE_CURRENT_SOURCE_DIR}/${source}")
23 endif()
24 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
25 list(APPEND scripts ${relfile})
26 list(APPEND abs_scripts ${file})
27 endforeach()
28 foreach(target ${targets})
29 get_target_property(source_dir ${target} SOURCE_DIR)
30 get_target_property(sources ${target} SOURCES)
31 foreach(source ${sources})
32 string(SUBSTRING ${source} 0 1 init_char)
33 string(COMPARE EQUAL ${init_char} "/" is_absolute)
34 if (${is_absolute})
35 set(file "${source}")
36 else()
37 set(file "${source_dir}/${source}")
38 endif()
39 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
40 set(files ${files} ${relfile})
41 set(abs_files ${abs_files} ${file})
42 endforeach()
43
44 target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}")
45 endforeach()
46
47 if("${scripts}" STREQUAL "")
48 set(sh_pot "/dev/null")
49 else()
50 set(sh_pot ${PROJECT_BINARY_DIR}/${domain}.sh.pot)
51 # Create the template for this specific sub-domain
52 add_custom_command (OUTPUT ${sh_pot}
53 COMMAND xgettext --add-comments --foreign -L Shell
54 -o ${sh_pot} ${scripts}
55 DEPENDS ${abs_scripts}
56 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
57 )
58 endif()
59
60
61 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.c.pot
62 COMMAND xgettext --add-comments --foreign -k_ -kN_
63 --keyword=P_:1,2
64 -o ${PROJECT_BINARY_DIR}/${domain}.c.pot ${files}
65 DEPENDS ${abs_files}
66 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
67 )
68
69 add_custom_command (OUTPUT ${PROJECT_BINARY_DIR}/${domain}.pot
70 COMMAND msgcomm --more-than=0 --sort-by-file
71 ${sh_pot}
72 ${PROJECT_BINARY_DIR}/${domain}.c.pot
73 --output=${PROJECT_BINARY_DIR}/${domain}.pot
74 DEPENDS ${sh_pot}
75 ${PROJECT_BINARY_DIR}/${domain}.c.pot
76 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
77 )
78
79 # Build .mo files
80 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
81 list(SORT translations)
82 foreach(file ${translations})
83 get_filename_component(langcode ${file} NAME_WE)
84 set(outdir ${PROJECT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES)
85 file(MAKE_DIRECTORY ${outdir})
86 # Command to merge and compile the messages
87 add_custom_command(OUTPUT ${outdir}/${domain}.po
88 COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${PROJECT_BINARY_DIR}/${domain}.pot
89 DEPENDS ${file} ${PROJECT_BINARY_DIR}/${domain}.pot
90 )
91 add_custom_command(OUTPUT ${outdir}/${domain}.mo
92 COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po
93 DEPENDS ${outdir}/${domain}.po
94 )
95
96 set(mofiles ${mofiles} ${outdir}/${domain}.mo)
97 install(FILES ${outdir}/${domain}.mo
98 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${langcode}/LC_MESSAGES")
99 endforeach(file ${translations})
100
101 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles})
102 endfunction()