Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / cmake / modules / FindGettext.cmake
1 # - Find GNU gettext tools
2 # This module looks for the GNU gettext tools. This module defines the
3 # following values:
4 #  GETTEXT_MSGMERGE_EXECUTABLE: the full path to the msgmerge tool.
5 #  GETTEXT_MSGFMT_EXECUTABLE: the full path to the msgfmt tool.
6 #  GETTEXT_FOUND: True if gettext has been found.
7 #
8 # Additionally it provides the following macros:
9 # GETTEXT_CREATE_TRANSLATIONS ( _moBasename [ALL] file1 ... fileN )
10 #    This will create a target "translations" which will convert the
11 #    given input po files into the binary output mo file. If the
12 #    ALL option is used, the translations will also be created when
13 #    building the default target.
14
15 FIND_PROGRAM(GETTEXT_MSGMERGE_EXECUTABLE msgmerge)
16
17 FIND_PROGRAM(GETTEXT_MSGFMT_EXECUTABLE msgfmt)
18
19 #
20 # Macro to be called if .po files are shipped as tar ball.
21 #
22 # _translation_set_basename: Serves two purposes; a) the stem of the
23 # default tarball %{_translation_set_basename}-po.tar.bz2 unless its over
24 # riden by -DUSE_TRANSLATION_SET; b) the basename of the .gmo files.
25 #
26 # We expect a po-file tarball to unpack the .po file to the current
27 # directory!
28 #
29 MACRO( GETTEXT_CREATE_TARBALL_TRANSLATIONS _translation_set_basename )
30
31         IF( NOT USE_TRANSLATION_SET )
32                 SET( USE_TRANSLATION_SET ${_translation_set_basename} )
33         ENDIF( NOT USE_TRANSLATION_SET )
34
35         SET( TRANSLATION_SET "${USE_TRANSLATION_SET}-po.tar.bz2" )
36         MESSAGE( STATUS "Translation set: ${TRANSLATION_SET}" )
37
38         # For those not familiar with 'sed': the tarball might list './' and './*.po'.
39         # We process just the '*.po' lines and strip off any leading './'.
40         EXECUTE_PROCESS(
41                 COMMAND tar tfj ${CMAKE_CURRENT_SOURCE_DIR}/${TRANSLATION_SET}
42                 COMMAND sed -n "/\\.po$/s%.*/%%p"
43                 COMMAND awk "{printf $1\";\"}"
44                 OUTPUT_VARIABLE TRANSLATION_SET_CONTENT
45         )
46         MESSAGE( STATUS "Translations: ${TRANSLATION_SET_CONTENT}" )
47
48         # Create 'LANG.po's from po.tar.bz2
49         ADD_CUSTOM_COMMAND(
50                 OUTPUT ${TRANSLATION_SET_CONTENT}
51                 COMMAND tar xfj ${CMAKE_CURRENT_SOURCE_DIR}/${TRANSLATION_SET}
52                 COMMAND sed -i  '/^msgid/s/do not forbid installation of %s/remove lock to allow installation of %s/' *.po
53                 COMMAND sed -i  '/^msgid/s/do not keep %s installed/remove lock to allow removal of %s/' *.po
54                 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${TRANSLATION_SET}
55         )
56
57         # LANG.po ->msgfmt-> LANG.gmo
58         SET( _gmoFiles )
59         FOREACH( _currentPoFile ${TRANSLATION_SET_CONTENT} )
60
61                 GET_FILENAME_COMPONENT( _lang ${_currentPoFile} NAME_WE )
62                 SET( _gmoFile "${_lang}.gmo" )
63                 SET( _gmoFiles ${_gmoFiles} ${_gmoFile} )
64
65                 ADD_CUSTOM_COMMAND(
66                         OUTPUT ${_gmoFile}
67                         COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${_gmoFile} ${_currentPoFile}
68                         DEPENDS ${_currentPoFile}
69                 )
70
71                 INSTALL(
72                         FILES ${CMAKE_CURRENT_BINARY_DIR}/${_gmoFile}
73                         DESTINATION share/locale/${_lang}/LC_MESSAGES
74                         RENAME ${_translation_set_basename}.mo
75                 )
76
77                 # the docs claim it can handle a list, but
78                 SET_DIRECTORY_PROPERTIES( PROPERTIES
79                         ADDITIONAL_MAKE_CLEAN_FILES ${_currentPoFile}
80                         ADDITIONAL_MAKE_CLEAN_FILES ${_gmoFile}
81                 )
82
83         ENDFOREACH( _currentPoFile )
84
85         # build all .gmo files
86         ADD_CUSTOM_TARGET(
87                 translations ALL
88                 DEPENDS ${_gmoFiles}
89         )
90
91 ENDMACRO( GETTEXT_CREATE_TARBALL_TRANSLATIONS )
92
93 #
94 # Macro to be called if .po files are part of the source tree.
95 #
96 MACRO(GETTEXT_CREATE_TRANSLATIONS _moBasename _firstPoFile)
97
98    SET(_gmoFiles)
99
100    SET(_addToAll)
101    IF(${_firstPoFile} STREQUAL "ALL")
102       SET(_addToAll "ALL")
103       SET(_firstPoFile)
104    ENDIF(${_firstPoFile} STREQUAL "ALL")
105
106    FOREACH (_currentPoFile ${_firstPoFile} ${ARGN})
107       GET_FILENAME_COMPONENT(_absFile ${_currentPoFile} ABSOLUTE)
108       GET_FILENAME_COMPONENT(_abs_PATH ${_absFile} PATH)
109       GET_FILENAME_COMPONENT(_lang ${_absFile} NAME_WE)
110       SET(_gmoFile ${CMAKE_CURRENT_BINARY_DIR}/${_lang}.gmo)
111
112       ADD_CUSTOM_COMMAND(
113          OUTPUT ${_gmoFile}
114          COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${_gmoFile} ${_absFile}
115          DEPENDS ${_absFile}
116       )
117
118       INSTALL(FILES ${_gmoFile} DESTINATION share/locale/${_lang}/LC_MESSAGES RENAME ${_moBasename}.mo)
119       SET(_gmoFiles ${_gmoFiles} ${_gmoFile})
120
121    ENDFOREACH (_currentPoFile )
122
123    ADD_CUSTOM_TARGET(translations ${_addToAll} DEPENDS ${_gmoFiles})
124
125 ENDMACRO(GETTEXT_CREATE_TRANSLATIONS )
126
127 IF (GETTEXT_MSGMERGE_EXECUTABLE AND GETTEXT_MSGFMT_EXECUTABLE )
128    SET(GETTEXT_FOUND TRUE)
129 ELSE (GETTEXT_MSGMERGE_EXECUTABLE AND GETTEXT_MSGFMT_EXECUTABLE )
130    SET(GETTEXT_FOUND FALSE)
131    IF (GetText_REQUIRED)
132       MESSAGE(FATAL_ERROR "GetText not found")
133    ENDIF (GetText_REQUIRED)
134 ENDIF (GETTEXT_MSGMERGE_EXECUTABLE AND GETTEXT_MSGFMT_EXECUTABLE )