CalamaresAddTranslations.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. include( CMakeParseArguments )
  2. # Internal macro for adding the C++ / Qt translations to the
  3. # build and install tree. Should be called only once, from
  4. # src/calamares/CMakeLists.txt.
  5. macro(add_calamares_translations language)
  6. list( APPEND CALAMARES_LANGUAGES ${ARGV} )
  7. set( calamares_i18n_qrc_content "<!DOCTYPE RCC><RCC version=\"1.0\">\n" )
  8. # calamares and qt language files
  9. set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}<qresource prefix=\"/lang\">\n" )
  10. foreach( lang ${CALAMARES_LANGUAGES} )
  11. set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}<file>calamares_${lang}.qm</file>\n" )
  12. list( APPEND TS_FILES "${CMAKE_SOURCE_DIR}/lang/calamares_${lang}.ts" )
  13. endforeach()
  14. set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}</qresource>\n" )
  15. set( calamares_i18n_qrc_content "${calamares_i18n_qrc_content}</RCC>\n" )
  16. file( WRITE ${CMAKE_BINARY_DIR}/lang/calamares_i18n.qrc "${calamares_i18n_qrc_content}" )
  17. qt5_add_translation(QM_FILES ${TS_FILES})
  18. ## HACK HACK HACK - around rcc limitations to allow out of source-tree building
  19. set( trans_file calamares_i18n )
  20. set( trans_srcfile ${CMAKE_BINARY_DIR}/lang/${trans_file}.qrc )
  21. set( trans_infile ${CMAKE_CURRENT_BINARY_DIR}/${trans_file}.qrc )
  22. set( trans_outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${trans_file}.cxx )
  23. # Copy the QRC file to the output directory
  24. add_custom_command(
  25. OUTPUT ${trans_infile}
  26. COMMAND ${CMAKE_COMMAND} -E copy ${trans_srcfile} ${trans_infile}
  27. MAIN_DEPENDENCY ${trans_srcfile}
  28. )
  29. # Run the resource compiler (rcc_options should already be set)
  30. add_custom_command(
  31. OUTPUT ${trans_outfile}
  32. COMMAND "${Qt5Core_RCC_EXECUTABLE}"
  33. ARGS ${rcc_options} -name ${trans_file} -o ${trans_outfile} ${trans_infile}
  34. MAIN_DEPENDENCY ${trans_infile}
  35. DEPENDS ${QM_FILES}
  36. )
  37. endmacro()
  38. # Internal macro for Python translations
  39. #
  40. # Translations of the Python modules that don't have their own
  41. # lang/ subdirectories -- these are collected in top-level
  42. # lang/python/<lang>/LC_MESSAGES/python.mo
  43. macro(add_calamares_python_translations language)
  44. set( CALAMARES_LANGUAGES "" )
  45. list( APPEND CALAMARES_LANGUAGES ${ARGV} )
  46. install_calamares_gettext_translations( python
  47. SOURCE_DIR ${CMAKE_SOURCE_DIR}/lang/python
  48. FILENAME python.mo
  49. RENAME calamares-python.mo
  50. )
  51. endmacro()
  52. # Installs a directory containing language-code-labeled subdirectories with
  53. # gettext data into the appropriate system directory. Allows renaming the
  54. # .mo files during install to avoid namespace clashes.
  55. #
  56. # install_calamares_gettext_translations(
  57. # NAME <name of module, for human use>
  58. # SOURCE_DIR path/to/lang
  59. # FILENAME <name of file.mo>
  60. # [RENAME <new-name of.mo>]
  61. # )
  62. #
  63. # For all of the (global) translation languages enabled for Calamares,
  64. # try installing $SOURCE_DIR/$lang/LC_MESSAGES/<filename>.mo into the
  65. # system gettext data directory (e.g. share/locale/), possibly renaming
  66. # filename.mo to renamed.mo in the process.
  67. function( install_calamares_gettext_translations )
  68. # parse arguments ( name needs to be saved before passing ARGN into the macro )
  69. set( NAME ${ARGV0} )
  70. set( oneValueArgs NAME SOURCE_DIR FILENAME RENAME )
  71. cmake_parse_arguments( TRANSLATION "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  72. if( NOT TRANSLATION_NAME )
  73. set( TRANSLATION_NAME ${NAME} )
  74. endif()
  75. if( NOT TRANSLATION_FILENAME )
  76. set( TRANSLATION_FILENAME "${TRANSLATION_NAME}.mo" )
  77. endif()
  78. if( NOT TRANSLATION_RENAME )
  79. set( TRANSLATION_RENAME "${TRANSLATION_FILENAME}" )
  80. endif()
  81. message(STATUS "Installing gettext translations for ${TRANSLATION_NAME}")
  82. message(STATUS " Installing ${TRANSLATION_FILENAME} from ${TRANSLATION_SOURCE_DIR}")
  83. set( TRANSLATION_NAME "${NAME}" )
  84. set( INSTALLED_TRANSLATIONS "" )
  85. foreach( lang ${CALAMARES_TRANSLATION_LANGUAGES} ) # Global
  86. set( lang_mo "${TRANSLATION_SOURCE_DIR}/${lang}/LC_MESSAGES/${TRANSLATION_FILENAME}" )
  87. if( lang STREQUAL "en" )
  88. message( STATUS " Skipping ${TRANSLATION_NAME} translations for en_US" )
  89. else( EXISTS ${lang_mo} )
  90. list( APPEND INSTALLED_LANGUAGES "${lang}" )
  91. install(
  92. FILES ${lang_mo}
  93. DESTINATION ${CMAKE_INSTALL_LOCALEDIR}/${lang}/LC_MESSAGES/
  94. RENAME ${TRANSLATION_RENAME}
  95. )
  96. # TODO: make translations available in build dir too, for
  97. # translation when running calamares -d from builddir.
  98. set(_build_lc ${CMAKE_BINARY_DIR}/lang/${lang}/LC_MESSAGES/)
  99. file(COPY ${lang_mo} DESTINATION ${_build_lc})
  100. if (NOT TRANSLATION_FILENAME STREQUAL TRANSLATION_RENAME)
  101. file(RENAME ${_build_lc}${TRANSLATION_FILENAME} ${_build_lc}${TRANSLATION_RENAME})
  102. endif()
  103. endif()
  104. endforeach()
  105. endfunction()