Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Qt translation files with CMake

Tags:

c++

cmake

qt

qt5

I'm having a problem when i'm trying to add the process of generating the translations inside the CMake process.

Now i have the following CMakeLists.txt:

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/defines.h.cmake
               ${CMAKE_CURRENT_BINARY_DIR}/defines.h)

file(GLOB_RECURSE UI_FILES *.ui)
file(GLOB_RECURSE CODE_FILES *.cpp)

qt5_wrap_ui(UI_HEADERS ${UI_FILES})

# Qt5LinguistTools
find_package(Qt5LinguistTools)
FILE(GLOB TS_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../resources/langs/*.ts")
QT5_create_translation(QM_FILES ${CODE_FILES} ${TS_FILES})

# Resources
qt5_add_resources(RESOURCE_FILES ../resources/resources.qrc)

# Windows application icon
if (WIN32)
  set(WINDOWS_RES_FILE ${CMAKE_CURRENT_BINARY_DIR}/resources.obj)
  if (MSVC)
    add_custom_command(OUTPUT ${WINDOWS_RES_FILE}
      COMMAND rc.exe /fo ${WINDOWS_RES_FILE} resources.rc
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cmake/win
    )
  else()
    add_custom_command(OUTPUT ${WINDOWS_RES_FILE}
      COMMAND windres.exe resources.rc ${WINDOWS_RES_FILE}
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cmake/win
    )
  endif()
endif()

#Move Qm files to resources langs folder
install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/../resources/langs)

add_executable(${CMAKE_PROJECT_NAME} WIN32
  ${QM_FILES}
  ${UI_HEADERS}
  ${CODE_FILES}
  ${RESOURCE_FILES}
  ${WINDOWS_RES_FILE}
)
target_link_libraries(${CMAKE_PROJECT_NAME}
  Qt5::Widgets
  QtAwesome
)

if (UNIX)
  install(TARGETS ${CMAKE_PROJECT_NAME}
          RUNTIME DESTINATION bin)
elseif (WIN32)
  install(TARGETS ${CMAKE_PROJECT_NAME}
          DESTINATION .)
endif()

It seems that this code generates correctly the .qm files, but not before being read by the resources file. So I'm getting an error: NMAKE : fatal error U1073: don't know how to make '..\..\..\resources\langs\lang_en_US.qm'

Maybe I'm not doing that on the right way or i need to read the language files from other place that's not inside the resources file.

Could you provide me an advice on how to generate the QM files and adding them on the build process.

like image 734
Alejandro Rosas Avatar asked Oct 06 '16 10:10

Alejandro Rosas


2 Answers

From the documentation:

Updating the translations can be done by adding the qm_files to the source list of your library/executable, so they are always updated, or by adding a custom target to control when they get updated/generated.

You can create custom targets and add dependencies:

add_custom_target(translations ALL DEPENDS ${QM_FILES})

add_custom_target(resources ALL DEPENDS ${RESOURCE_FILES})

add_dependencies(resources translations)

add_executable(${CMAKE_PROJECT_NAME} WIN32
  ${UI_HEADERS}
  ${CODE_FILES}
  ${RESOURCE_FILES}
  ${WINDOWS_RES_FILE}
)
add_dependencies(${CMAKE_PROJECT_NAME} resources)
like image 173
rgmt Avatar answered Oct 19 '22 05:10

rgmt


This is what I'm doing for QOwnNotes: https://github.com/pbek/QOwnNotes/blob/develop/src/CMakeLists.txt

# Translation files
SET(QON_TS_FILES
languages/QOwnNotes_en.ts
        languages/QOwnNotes_de.ts
        languages/QOwnNotes_fr.ts
        languages/QOwnNotes_pl.ts
        languages/QOwnNotes_zh.ts
        languages/QOwnNotes_ru.ts
        languages/QOwnNotes_pt.ts
        languages/QOwnNotes_es.ts
        languages/QOwnNotes_nl.ts
        languages/QOwnNotes_hu.ts
        languages/QOwnNotes_ja.ts
        languages/QOwnNotes_it.ts
        languages/QOwnNotes_ar.ts
)

qt5_add_translation(QON_QM_FILES ${QON_TS_FILES})
add_custom_target(translations DEPENDS ${QON_QM_FILES})

if(NOT QT_TRANSLATIONS_DIR)
# If this directory is missing, we are in a Qt5 environment.
# Extract the qmake executable location
get_target_property(QT5_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
# Ask Qt5 where to put the translations
execute_process( COMMAND ${QT5_QMAKE_EXECUTABLE} -query QT_INSTALL_TRANSLATIONS
OUTPUT_VARIABLE qt_translations_dir OUTPUT_STRIP_TRAILING_WHITESPACE )
# make sure we have / and not \ as qmake gives on windows
file( TO_CMAKE_PATH "${qt_translations_dir}" qt_translations_dir)
set( QT_TRANSLATIONS_DIR ${qt_translations_dir} CACHE PATH
"The location of the Qt translations" FORCE)
endif()

install(FILES ${QON_QM_FILES}
DESTINATION ${QT_TRANSLATIONS_DIR})

add_executable(QOwnNotes ${SOURCE_FILES} ${RESOURCE_ADDED} ${QON_QM_FILES})

Does this help you?

like image 36
Patrizio Bekerle Avatar answered Oct 19 '22 05:10

Patrizio Bekerle