[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cmake
Subject:    [CMake] Qt translation handling
From:       Francis Giraldeau <francis.giraldeau () gmail ! com>
Date:       2019-01-17 16:56:19
Message-ID: CAC6yHM7TdD2V5pR-=0D4N=Nf74XzOC12=x42SSs6kHeDE7Zm=Q () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Here is a small snipped I wanted to share about generation of qt translation

files.


The Qt translations are managed using two files: ts and qm. The ts files are
edited by translators and updated from source files. Therefore, they must be
added to the repository. To include the translations in the application, qm
binary file is generated. This file is generated in the build tree and must
not be added to the repository.

Qt5 suggests to use qt5_create_translation() macro. However, this macro
regenerate the ts file at build time. It is very inconvenient when working
on multiple branches, because it forces to either commit the modified
translation file or discard the update. Instead, we use a custom target
"make translations" that update ts files only when requested explicitely.

set(TS_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/app_fr.ts
)
file(GLOB_RECURSE TS_SOURCES "*.cpp" "*.h" "*.ui")
add_custom_target(translations)
foreach(_ts_file ${TS_FILES})

    # generate a sensible name for this translation file
    get_filename_component(_ts_name ${_ts_file} NAME_WE)

    # call lupdate ourselves
    add_custom_command(
        OUTPUT "${_ts_file}"
        DEPENDS ${TS_SOURCES}
        COMMAND ${Qt5_LUPDATE_EXECUTABLE} -recursive
${CMAKE_CURRENT_SOURCE_DIR} -ts ${_ts_file}
    )

    # create a target that depends on the generated files
    add_custom_target(translate_${_ts_name} DEPENDS ${_ts_file})

    # attach the custom target for this ts file to the parent target
    add_dependencies(translations translate_${_ts_name})
endforeach()

# generate qm files
qt5_add_translation(QM_FILES ${TS_FILES})
configure_file(translations.qrc ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)


add_executable(App

    ${QM_FILES}

    ${CMAKE_CURRENT_BINARY_DIR}/translations.qrc

)


Cheers,


Francis

-- 
Francis Giraldeau

[Attachment #5 (text/html)]

<div dir="ltr"><pre style="margin-top:0px;margin-bottom:0px">Here is a small snipped \
I wanted to share about generation of qt translation</pre><pre \
style="margin-top:0px;margin-bottom:0px">files.</pre><pre \
style="margin-top:0px;margin-bottom:0px"><br></pre><pre \
style="margin-top:0px;margin-bottom:0px">The Qt translations are managed using two \
files: ts and qm. The ts files are edited by translators and updated from source \
files. Therefore, they must be added to the repository. To include the translations \
in the application, qm binary file is generated. This file is generated in the build \
tree and must not be added to the repository.

Qt5 suggests to use qt5_create_translation() macro. However, this macro
regenerate the ts file at build time. It is very inconvenient when working
on multiple branches, because it forces to either commit the modified
translation file or discard the update. Instead, we use a custom target
&quot;make translations&quot; that update ts files only when requested explicitely.

set(TS_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/app_fr.ts
)
file(GLOB_RECURSE TS_SOURCES &quot;*.cpp&quot; &quot;*.h&quot; &quot;*.ui&quot;)
add_custom_target(translations)
foreach(_ts_file ${TS_FILES})</pre><pre style="margin-top:0px;margin-bottom:0px">    \
# generate a sensible name for this translation file  get_filename_component(_ts_name \
${_ts_file} NAME_WE)</pre><pre style="margin-top:0px;margin-bottom:0px">    # call \
lupdate ourselves  add_custom_command(
        OUTPUT &quot;${_ts_file}&quot;
        DEPENDS ${TS_SOURCES}
        COMMAND ${Qt5_LUPDATE_EXECUTABLE} -recursive ${CMAKE_CURRENT_SOURCE_DIR} -ts \
${_ts_file}  )</pre><pre style="margin-top:0px;margin-bottom:0px">    # create a \
target that depends on the generated files  add_custom_target(translate_${_ts_name} \
DEPENDS ${_ts_file})</pre><pre style="margin-top:0px;margin-bottom:0px">    # attach \
the custom target for this ts file to the parent target  \
add_dependencies(translations translate_${_ts_name}) endforeach()
<br></pre><pre style="margin-top:0px;margin-bottom:0px"># generate qm files
qt5_add_translation(QM_FILES ${TS_FILES})
configure_file(translations.qrc ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)<br></pre><pre \
style="margin-top:0px;margin-bottom:0px"><br></pre><pre \
style="margin-top:0px;margin-bottom:0px">add_executable(App</pre><pre \
style="margin-top:0px;margin-bottom:0px">    ${QM_FILES}</pre><pre \
style="margin-top:0px;margin-bottom:0px">    \
${CMAKE_CURRENT_BINARY_DIR}/translations.qrc</pre><pre \
style="margin-top:0px;margin-bottom:0px">)</pre><pre \
style="margin-top:0px;margin-bottom:0px"><br></pre><pre \
style="margin-top:0px;margin-bottom:0px">Cheers,</pre><pre \
style="margin-top:0px;margin-bottom:0px"><br></pre><pre \
style="margin-top:0px;margin-bottom:0px">Francis</pre></div>-- <br><div dir="ltr" \
class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Francis \
Giraldeau</div></div>



-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: \
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information \
on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at \
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic