How to integrate QT internationalization to CMake?

淺唱寂寞╮ 提交于 2020-04-08 00:16:14

问题


Greetings all,

I am trying to use QT internationalization with CMake. I have configured my cmake file as follows :

#Internalization - this should generate core_jp.ts ?
SET(rinzo_core_TRANSLATIONS
   i18n/core_jp.ts
   )

#these are my source files in the project
SET(FILES_TO_TRANSLATE
   ${rinzo_core_srcs} 
   ${rinzo_core_moh_srcs}
 )

QT4_CREATE_TRANSLATION(QM_FILES ${FILES_TO_TRANSLATE} ${rinzo_core_TRANSLATIONS})
QT4_ADD_TRANSLATION(QM ${rinzo_core_TRANSLATIONS})

But it doesnt genereate any TS nor QM files.

My questions -

1.Does Cmake(by using QT tools) generate TS files automatically extracting "tr()" methods from the source ? (that means I dont have to create any TS file and above i18n/core_jp.ts will be genereated automatically)

2.What exacly are QM files ?

Thanks in advance


回答1:


In CMake documentation see QT4_CREATE_TRANSLATION and QT4_ADD_TRANSLATION macros.

So you should do the followings:

SET(Lang_files
  example.ts
)
...
QT4_CREATE_TRANSLATION(LangSrcs ${Lang_files})
...
ADD_EXECUTABLE(project_name ... others sources ... ${LangSrcs})



回答2:


Translation binary files (*.qm) according to http://itk.org/Wiki/CMake:How_To_Build_Qt4_Software

Also from the bottom of that website

Usage - Updating the .ts files

When you want it to process all your source files (looking for new texts to translate), configure cmake to turn on UPDATE_TRANSLATIONS, and then make your project. CMake will modify your .ts files in your SOURCE folders in addition to generating the .qm files. WARNING: Be aware that CMake will be updating the source .ts files, which means that if > you do a make clean, it will DELETE your source .ts files!!! So it would be a good idea > to switch off UPDATE_TRANSLATIONS as soon as possible.




回答3:


My solution relies on manually invoked lupdate and lrelease tools via add_custom_target, so the generated files are not removed on make clean and put into the source directory.

I defined a function that scans provided directory, generates/updates ts files, and compiles them into qm files in the same directory, so they can be added to the app via .qrc file

cmake_minimum_required(VERSION 3.5)

project(l10n LANGUAGES CXX)
find_package(Qt5 COMPONENTS Core LinguistTools REQUIRED)

# genearats ts and qm file searching recursively SRC_DIR
function(generate_translations CUSTOM_TARGET TS_DIR TS_FILES SRC_DIR)
    set(UPADTE_TS_TARGET_NAME ${CUSTOM_TARGET}_ts)
    set(UPADTE_QM_TARGET_NAME ${CUSTOM_TARGET}_qm)

    add_custom_target(${UPADTE_TS_TARGET_NAME}
        COMMAND ${Qt5_LUPDATE_EXECUTABLE} -recursive ${SRC_DIR} -ts ${TS_FILES}
        WORKING_DIRECTORY ${TS_DIR})

    add_custom_target(${UPADTE_QM_TARGET_NAME}
        COMMAND ${Qt5_LRELEASE_EXECUTABLE} ${TS_FILES}
        WORKING_DIRECTORY ${TS_DIR})

    add_dependencies(${UPADTE_QM_TARGET_NAME} ${UPADTE_TS_TARGET_NAME} )
    add_dependencies(${CUSTOM_TARGET} ${UPADTE_QM_TARGET_NAME})
endfunction()

add_executable(l10n main.cxx)

target_link_libraries(l10n Qt5::Core)

set(MY_TS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/translate)
set(MY_TS_FILES foo_en.ts foo_en.ts)
set(MY_SOURCE_LOOKUP_DIR ${CMAKE_CURRENT_SOURCE_DIR})

generate_translations(l10n "${MY_TS_DIR}" "${MY_TS_FILES}" "${MY_SOURCE_LOOKUP_DIR}")



回答4:


can you use lupdate.exe, linguist.exe and lrelease.exe from qt/[qt_version]/[msvc|mingw|...]/bin/ ?

you can use it like that:

Usage:
    lupdate [options] [project-file]
    lupdate [options] [source-file|path]... -ts ts-files
Options:
    -help  Display this information and exit.
    -noobsolete
           Drop all obsolete strings.
    -extensions [,]...
           Process files with the given extensions only.
           The extension list must be separated with commas, not with whitespace.
           Default: 'ui,c,c++,cc,cpp,cxx,ch,h,h++,hh,hpp,hxx'.
    -pluralonly
           Only include plural form messages.
    -silent
           Do not explain what is being done.
    -version
           Display the version of lupdate and exit 

so, [source-file|path] - is you option, like i think. try to call it with list of source files names.



来源:https://stackoverflow.com/questions/5630155/how-to-integrate-qt-internationalization-to-cmake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!