How to update ts file in cmake?
-
At first, I used Qt Creator->New Project->Qt Widgets Application->Cmake build system->Language, so I got a CMakeList.txt file automatically generated by Qt:
cmake_minimum_required(VERSION 3.5) project(CVIPtools VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools) set(TS_FILES CVIPtools_en_US.ts) set(PROJECT_SOURCES main.cpp MainWindow.cpp MainWindow.h MainWindow.ui ${TS_FILES} ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(CVIPtools MANUAL_FINALIZATION ${PROJECT_SOURCES} ) # Define target properties for Android with Qt 6 as: # set_property(TARGET CVIPtools APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR # ${CMAKE_CURRENT_SOURCE_DIR}/android) # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES}) else() if(ANDROID) add_library(CVIPtools SHARED ${PROJECT_SOURCES} ) # Define properties for Android with Qt 5 after find_package() calls as: # set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") else() add_executable(CVIPtools ${PROJECT_SOURCES} ) endif() qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES}) endif() target_link_libraries(CVIPtools PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. if(${QT_VERSION} VERSION_LESS 6.1.0) set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.CVIPtools) endif() set_target_properties(CVIPtools PROPERTIES ${BUNDLE_ID_OPTION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) include(GNUInstallDirs) install(TARGETS CVIPtools BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(CVIPtools) endif()
Then, I built the project, but the
.ts
file remained as an empty default file:<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1" language="en_US"></TS>
The default CMakeLists.txt generated by Qt canot update
.ts
file, and the.qm
file not generated either.Next, I referred to the documentation and changed CmakeList.txt:
........... qt_add_lupdate(CVIPtools TS_FILES CVIPtools_en_US.ts) qt_add_lrelease(CVIPtools TS_FILES CVIPtools_en_US.ts QM_FILES_OUTPUT_VARIABLE QM_FILES) qt_add_resources(CVIPtools "translations" PREFIX "/i18n" BASE "${CMAKE_CURRENT_BINARY_DIR}" FILES "${QM_FILES}") target_link_libraries(CVIPtools PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. ......
or:
qt_add_translations(CVIPtools TS_FILES CVIPtools_en_US.ts)
Below is the log:
[1/7 20.8/sec] Generating D:/Projects/CVIPtools/CVIPtools_en_US.ts Updating '../../CVIPtools_en_US.ts'... Found 0 source text(s) (0 new and 0 already existing) [2/7 24.4/sec] Generating CVIPtools_en_US.qm Updating 'D:/Projects/CVIPtools/build/build-CVIPtools-Desktop_Qt_6_5_1_MSVC2019_64bit-Release/CVIPtools_en_US.qm'... Generated 0 translation(s) (0 finished and 0 unfinished)
The
.ts
files are still not updated, but.qm
file is generated.Qt : 6.5.1
Qt Creator: 11.0.0 -
@Peiqi-Liu
Below yourqt_add_translations
command, try addingadd_dependencies(${CMAKE_PROJECT_NAME} update_translations)
.
See this stackoverflow answer for details. -
Hi @Peiqi-Liu,
Found 0 source text(s) (0 new and 0 already existing)
The problem is that
lupdate
is not finding any source strings to be translated. It could be:qt_add_translations()
is not detecting the target's source files. Try addingSOURCES ...
toqt_add_translations()
, eg
qt_add_translations(CVIPtools TS_FILES CVIPtools_en_US.ts SOURCES ${PROJECT_SOURCES})
If you do something like:
make VERBOSE=1 CVIPtools_lupdate
You should see the output mention a
.../.lupdate/*_project.json
file . Have a look inside, and see which source files are mentioned.- or, it could be that there are no translatable string recoginised in those files. Consider running
lupdate
manually against one or more of your cpp files to see if it recognises any translatable strings (lupdate
can miss strings depending on the code layout). Iflupdate
doesn't recognise any strings, show us one of yourtr(...)
(orCoreApplication::translate()
) examples, or create a really basic one (if you haven't already).
Cheers.
-
Using
cmake -- build. -- target <target name>_lupdate
to updates orcmake -- build. -- target <target name>_lrelease
to release