source: calamares/trunk/fuentes/CMakeModules/CalamaresAddTranslations.cmake @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 6.9 KB
Line 
1# === This file is part of Calamares - <https://github.com/calamares> ===
2#
3#   Calamares is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7#
8#   Calamares is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11#   GNU General Public License for more details.
12#
13#   You should have received a copy of the GNU General Public License
14#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
15#
16#   SPDX-License-Identifier: GPL-3.0+
17#   License-Filename: LICENSE
18#
19###
20#
21# This file has not yet been documented for use outside of Calamares itself.
22
23include( CMakeParseArguments )
24
25if( NOT _rcc_version_support_checked )
26    set( _rcc_version_support_checked TRUE )
27
28    # Extract the executable name
29    get_property( _rcc_executable
30        TARGET ${Qt5Core_RCC_EXECUTABLE}
31        PROPERTY IMPORTED_LOCATION
32    )
33    if( NOT _rcc_executable )
34        # Weird, probably now uses Qt5::rcc which is wrong too
35        set( _rcc_executable ${Qt5Core_RCC_EXECUTABLE} )
36    endif()
37
38    # Try an empty RCC file with explicit format-version
39    execute_process(
40        COMMAND echo "<RCC version='1.0'></RCC>"
41        COMMAND ${Qt5Core_RCC_EXECUTABLE} --format-version 1 --list -
42        RESULT_VARIABLE _rcc_version_rv
43        ERROR_VARIABLE _rcc_version_dump
44    )
45    if ( _rcc_version_rv EQUAL 0 )
46        # Supported: force to the reproducible version
47        set( _rcc_version_support --format-version 1 )
48    else()
49        # Older Qt versions (5.7, 5.8) don't support setting the
50        # rcc format-version, so won't be reproducible if they
51        # default to version 2.
52        set( _rcc_version_support "" )
53    endif()
54    unset( _rcc_version_rv )
55    unset( _rcc_version_dump )
56endif()
57
58
59# Internal macro for adding the C++ / Qt translations to the
60# build and install tree. Should be called only once, from
61# src/calamares/CMakeLists.txt.
62macro(add_calamares_translations language)
63    list( APPEND CALAMARES_LANGUAGES ${ARGV} )
64
65    set( calamares_i18n_qrc_content "<!DOCTYPE RCC><RCC version=\"1.0\">\n" )
66
67    # calamares and qt language files
68    set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}<qresource prefix=\"/lang\">\n" )
69    foreach( lang ${CALAMARES_LANGUAGES} )
70        set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}<file>calamares_${lang}.qm</file>\n" )
71        list( APPEND TS_FILES "${CMAKE_SOURCE_DIR}/lang/calamares_${lang}.ts" )
72    endforeach()
73
74    set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}</qresource>\n" )
75    set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}</RCC>\n" )
76
77    file( WRITE ${CMAKE_BINARY_DIR}/lang/calamares_i18n.qrc "${calamares_i18n_qrc_content}" )
78
79    qt5_add_translation(QM_FILES ${TS_FILES})
80
81    ## HACK HACK HACK - around rcc limitations to allow out of source-tree building
82    set( trans_file calamares_i18n )
83    set( trans_srcfile ${CMAKE_BINARY_DIR}/lang/${trans_file}.qrc )
84    set( trans_infile ${CMAKE_CURRENT_BINARY_DIR}/${trans_file}.qrc )
85    set( trans_outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${trans_file}.cxx )
86
87    # Copy the QRC file to the output directory
88    add_custom_command(
89        OUTPUT ${trans_infile}
90        COMMAND ${CMAKE_COMMAND} -E copy ${trans_srcfile} ${trans_infile}
91        MAIN_DEPENDENCY ${trans_srcfile}
92    )
93
94    # Run the resource compiler (rcc_options should already be set)
95    add_custom_command(
96        OUTPUT ${trans_outfile}
97        COMMAND "${Qt5Core_RCC_EXECUTABLE}"
98        ARGS ${rcc_options} ${_rcc_version_support} -name ${trans_file} -o ${trans_outfile} ${trans_infile}
99        MAIN_DEPENDENCY ${trans_infile}
100        DEPENDS ${QM_FILES}
101    )
102endmacro()
103
104# Internal macro for Python translations
105#
106# Translations of the Python modules that don't have their own
107# lang/ subdirectories -- these are collected in top-level
108# lang/python/<lang>/LC_MESSAGES/python.mo
109macro(add_calamares_python_translations language)
110    set( CALAMARES_LANGUAGES "" )
111    list( APPEND CALAMARES_LANGUAGES ${ARGV} )
112
113    install_calamares_gettext_translations( python
114        SOURCE_DIR ${CMAKE_SOURCE_DIR}/lang/python
115        FILENAME python.mo
116        RENAME calamares-python.mo
117    )
118endmacro()
119
120# Installs a directory containing language-code-labeled subdirectories with
121# gettext data into the appropriate system directory. Allows renaming the
122# .mo files during install to avoid namespace clashes.
123#
124# install_calamares_gettext_translations(
125#   NAME <name of module, for human use>
126#   SOURCE_DIR path/to/lang
127#   FILENAME <name of file.mo>
128#   [RENAME <new-name of.mo>]
129# )
130#
131# For all of the (global) translation languages enabled for Calamares,
132# try installing $SOURCE_DIR/$lang/LC_MESSAGES/<filename>.mo into the
133# system gettext data directory (e.g. share/locale/), possibly renaming
134# filename.mo to renamed.mo in the process.
135function( install_calamares_gettext_translations )
136    # parse arguments ( name needs to be saved before passing ARGN into the macro )
137    set( NAME ${ARGV0} )
138    set( oneValueArgs NAME SOURCE_DIR FILENAME RENAME )
139    cmake_parse_arguments( TRANSLATION "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
140
141    if( NOT TRANSLATION_NAME )
142        set( TRANSLATION_NAME ${NAME} )
143    endif()
144    if( NOT TRANSLATION_FILENAME )
145        set( TRANSLATION_FILENAME "${TRANSLATION_NAME}.mo" )
146    endif()
147    if( NOT TRANSLATION_RENAME )
148        set( TRANSLATION_RENAME "${TRANSLATION_FILENAME}" )
149    endif()
150
151    message(STATUS "Installing gettext translations for ${TRANSLATION_NAME}")
152    message(STATUS "  Installing ${TRANSLATION_FILENAME} from ${TRANSLATION_SOURCE_DIR}")
153
154    set( TRANSLATION_NAME "${NAME}" )
155    set( INSTALLED_TRANSLATIONS "" )
156    foreach( lang ${CALAMARES_TRANSLATION_LANGUAGES} )  # Global
157        set( lang_mo "${TRANSLATION_SOURCE_DIR}/${lang}/LC_MESSAGES/${TRANSLATION_FILENAME}" )
158        if( lang STREQUAL "en" )
159            message( STATUS "  Skipping ${TRANSLATION_NAME} translations for en_US" )
160        else( EXISTS ${lang_mo} )
161            list( APPEND INSTALLED_LANGUAGES "${lang}" )
162            install(
163                FILES ${lang_mo}
164                DESTINATION ${CMAKE_INSTALL_LOCALEDIR}/${lang}/LC_MESSAGES/
165                RENAME ${TRANSLATION_RENAME}
166            )
167            # TODO: make translations available in build dir too, for
168            #       translation when running calamares -d from builddir.
169            set(_build_lc ${CMAKE_BINARY_DIR}/lang/${lang}/LC_MESSAGES/)
170            file(COPY ${lang_mo} DESTINATION ${_build_lc})
171            if (NOT TRANSLATION_FILENAME STREQUAL TRANSLATION_RENAME)
172                file(RENAME ${_build_lc}${TRANSLATION_FILENAME} ${_build_lc}${TRANSLATION_RENAME})
173            endif()
174
175        endif()
176    endforeach()
177endfunction()
Note: See TracBrowser for help on using the repository browser.