Skip to content

Instantly share code, notes, and snippets.

@giraldeau
Created November 3, 2017 21:48
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giraldeau/546ba5512a74dfe9d8ea0862d66db412 to your computer and use it in GitHub Desktop.
Save giraldeau/546ba5512a74dfe9d8ea0862d66db412 to your computer and use it in GitHub Desktop.
The missing example to use cmake qt5_create_translation
cmake_minimum_required(VERSION 3.8)
project(tsProject)
# Managing translations require LinguistTools module
# The cmake function is defined in the Qt installation tree:
# i.e. Qt5.9.1/5.9.1/gcc_64/lib/cmake/Qt5LinguistTools/Qt5LinguistToolsMacros.cmake
# Reference: https://doc.qt.io/qt-5/cmake-manual.html#qt5linguisttools-macros
find_package(Qt5 COMPONENTS Widgets LinguistTools)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# auto everything
# Reference: https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
# Only call lrelease on each translation files to create qm files.
# This command assumes that the ts files already exists (manually created).
#
# qt5_add_translation(QM_FILES french.ts english.ts)
# Call lupdate to generate (or update already existing) ts files from source
# code (.cpp and .ui) code AND generate qm files. We can call it with a source
# directory OR individual files to search for strings to translate. The ts files
# are generated in the source directory and the qm files are created in the
# build directory, as it should, because we don't want to pollute our source
# directory with generated binary files.
#
# Recall that the ts files are edited by translators, while qm files are just
# binary representation of the ts file for perfomance and size optimization.
#
# Using a source file list:
# qt5_create_translation(QM_FILES
# translatemainwindow.cpp translatemainwindow.ui
# english.ts french.ts
# )
#
# Using a source directory:
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} english.ts french.ts)
# The qm files are generated in the build tree, but the qrc file is inside the
# source directory and the path to resources are relative to the location of
# the qrc file itself. We use configure_file() to copy the qrc file in the build
# directory such that it can find the qm translations files. The qrc file is
# copied if it doesn't exist in the destination or if it is modified.
configure_file(translations.qrc ${CMAKE_BINARY_DIR} COPYONLY)
# We have to reference the translations.qrc copy in the build directory, not the
# original file in the source directory. We also add qm files to the target to
# create a dependency that will force rebuild in case the translation are
# updated.
add_executable(13-translate_demo
main.cpp
translatemainwindow.cpp
${CMAKE_BINARY_DIR}/translations.qrc
${QM_FILES}
)
target_link_libraries(13-translate_demo Qt5::Widgets)
@petermost
Copy link

Thanks for the snippet!

I'm using it in my C++ AidKit library which I'm using with add_subdirectory() and for that I had to replace:
CMAKE_BINARY_DIR with CMAKE_CURRENT_BINARY_DIR
and
CMAKE_SOURCE_DIR with CMAKE_CURRENT_SOURCE_DIR
i.e.:

qt5_create_translation(QM_FILES ${CMAKE_CURRENT_SOURCE_DIR} english.ts french.ts)
configure_file(translations.qrc ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
add_executable(13-translate_demo
    main.cpp
    translatemainwindow.cpp
    ${CMAKE_CURRENT_BINARY_DIR}/translations.qrc
    ${QM_FILES}
)

I hope that helps others who might have the same use case.

@dewcked
Copy link

dewcked commented Mar 29, 2022

Although this snipet is kinda old, better than latest official documentations [qt5_add_translation] [qt5_create_translation]

@guiyanzhong
Copy link

Thanks for the snippet! My Qt Creator IDE (on Ubuntu Linux) did not create the .qrc file for me automatically, so here is an example for translations.qrc, for your reference:

<!DOCTYPE RCC>
<RCC version="1.0">
    <qresource prefix="/i18n">
        <!-- Add the compiled .qm files here -->
        <file>myexe_zh_CN.qm</file>
        <!-- Add more .qm files if needed -->
    </qresource>
</RCC>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment