如何使用 cmake 和 QRC 将 Qt4 qm 文件集成为二进制文件?

发布于 2024-09-07 07:57:05 字数 1614 浏览 6 评论 0原文

我有一个 Qt4 CMake 项目,我想将 i18n 的 QM 文件集成到输出二进制文件中。这些是我到目前为止生成 TS 和 QM 文件的规则:

set(myapp_TRANSLATIONS
    i18n/myapp_de.ts
)

set(FILES_TO_TRANSLATE
    ${myapp_SRCS}
    ${myapp_MOC_HDRS}
)

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

我尝试了以下方法将 QM 文件添加到可执行文件:

add_executable(myapp ${myapp_SRCS} ${myapp_MOC_SRCS} ${myapp_RCC_SRCS} ${QM})

这是 main.cpp 的初始化:

QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&qtTranslator);

QTranslator appTranslator;
appTranslator.load("myapp_" + QLocale::system().name());
app.installTranslator(&appTranslator);

但是, strings mypp 显示翻译不会进入二进制文件。

更新:我将每个 qm 文件添加到 i18n/translations.qrc

<!DOCTYPE RCC><RCC version="1.0">
  <qresource prefix="/resources">
    <file>myapp_de.qm</file>
    <file>  ...   .qm</file>
  </qresource>
</RCC>

并使用

QT4_ADD_RESOURCES(myapp_QM_RCC_SRCS i18n/translations.qrc)

myapp_QM_RCC_SRCS 并将其添加到可执行依赖项。

但这在构建时失败了,因为 CMake 进行了影子构建(在源目录之外构建),但解析了 QRC 文件以获取依赖项,期望引用的文件相对于 QRC 文件(很好的功能,但没有 make 规则如何构建)该位置的 QM 文件)。 QM 文件位于 ${CMAKE_CURRENT_BINARY_DIR} 中(它们属于使用影子构建的位置),但期望它位于 ${CMAKE_CURRENT_SOURCE_DIR} 中(非生成的文件应该位于其中 - 所以两者位置将是正确的,具体取决于情况)。

I have a Qt4 CMake project and I'd like to integrate the QM files for i18n into the output binary. These are the rules I have so far for generating the TS and QM files:

set(myapp_TRANSLATIONS
    i18n/myapp_de.ts
)

set(FILES_TO_TRANSLATE
    ${myapp_SRCS}
    ${myapp_MOC_HDRS}
)

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

I tried the following to add the QM files to the executable:

add_executable(myapp ${myapp_SRCS} ${myapp_MOC_SRCS} ${myapp_RCC_SRCS} ${QM})

This is the initialization from main.cpp:

QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&qtTranslator);

QTranslator appTranslator;
appTranslator.load("myapp_" + QLocale::system().name());
app.installTranslator(&appTranslator);

However, strings mypp shows that the translations are not going into the binary.

Update: I added each qm file to a i18n/translations.qrc:

<!DOCTYPE RCC><RCC version="1.0">
  <qresource prefix="/resources">
    <file>myapp_de.qm</file>
    <file>  ...   .qm</file>
  </qresource>
</RCC>

and using

QT4_ADD_RESOURCES(myapp_QM_RCC_SRCS i18n/translations.qrc)

and adding myapp_QM_RCC_SRCS to the executable dependencies.

But this fails during build time thanks to the fact that CMake does a shadow build (building outside the source dir) but parses the QRC files for dependencies expecting the referenced files relative to the QRC file (nice feature but there's no make rule how to build the QM file at that location). The QM files are in ${CMAKE_CURRENT_BINARY_DIR} (where they belong using shadow building) but expects it in ${CMAKE_CURRENT_SOURCE_DIR} (where non-generated files should be - so both locations would be correct, depending on situation).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云醉月微眠 2024-09-14 07:57:05

我遇到了完全相同的问题。我想出了以下解决方案:

创建一个仅包含预期 QM 文件的 QRC 文件,并为其指定不同的前缀,这样它就不会与您的其他资源冲突:

<RCC>
    <qresource prefix="/translators">
    <file>myapp_en.qm</file>
    </qresource>
</RCC>

添加 CMake 规则以将 QRC 文件复制到输出目录然后运行资源编译器的另一条规则:

# Change 'myapp_en' to be the base file name of the qrc file.
SET( trans_file myapp_en )
SET( trans_srcfile ${CMAKE_CURRENT_SOURCE_DIR}/${trans_file}.qrc)
SET( trans_infile ${CMAKE_CURRENT_BINARY_DIR}/${trans_file}.qrc)
SET( trans_outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${trans_file}.cxx)

# Copy the QRC file to the output directory, because the files listed in the 
# qrc file are relative to that directory.
ADD_CUSTOM_COMMAND(
    OUTPUT ${trans_infile}
    COMMAND ${CMAKE_COMMAND} -E copy ${trans_srcfile} ${trans_infile}
    MAIN_DEPENDENCY ${trans_srcfile}
    )

# Run the resource compiler (rcc_options should already be set). We can't
# use QT4_ADD_RESOURCES because the qrc file may not exist yet.
ADD_CUSTOM_COMMAND(
    OUTPUT ${trans_outfile}
    COMMAND ${QT_RCC_EXECUTABLE}
    ARGS ${rcc_options} -name ${trans_file} -o ${trans_outfile} ${trans_infile}
    MAIN_DEPENDENCY ${trans_infile}
    DEPENDS ${qm_files}
    )

# Add compiled resources to executable dependency list
ADD_EXECUTABLE( ${APP_NAME} ... ${trans_outfile} )

如果使用 Qt 5,请使用 ${Qt5Core_RCC_EXECUTABLE} 而不是 ${QT_RCC_EXECUTABLE}

I had the exact same problem. I came up with the following solution:

Create a QRC file that contains only the expected QM files, and give it a different prefix so it won't conflict with your other resources:

<RCC>
    <qresource prefix="/translators">
    <file>myapp_en.qm</file>
    </qresource>
</RCC>

Add a CMake rule to copy the QRC file to the output directory and then another rule to run the resource compiler:

# Change 'myapp_en' to be the base file name of the qrc file.
SET( trans_file myapp_en )
SET( trans_srcfile ${CMAKE_CURRENT_SOURCE_DIR}/${trans_file}.qrc)
SET( trans_infile ${CMAKE_CURRENT_BINARY_DIR}/${trans_file}.qrc)
SET( trans_outfile ${CMAKE_CURRENT_BINARY_DIR}/qrc_${trans_file}.cxx)

# Copy the QRC file to the output directory, because the files listed in the 
# qrc file are relative to that directory.
ADD_CUSTOM_COMMAND(
    OUTPUT ${trans_infile}
    COMMAND ${CMAKE_COMMAND} -E copy ${trans_srcfile} ${trans_infile}
    MAIN_DEPENDENCY ${trans_srcfile}
    )

# Run the resource compiler (rcc_options should already be set). We can't
# use QT4_ADD_RESOURCES because the qrc file may not exist yet.
ADD_CUSTOM_COMMAND(
    OUTPUT ${trans_outfile}
    COMMAND ${QT_RCC_EXECUTABLE}
    ARGS ${rcc_options} -name ${trans_file} -o ${trans_outfile} ${trans_infile}
    MAIN_DEPENDENCY ${trans_infile}
    DEPENDS ${qm_files}
    )

# Add compiled resources to executable dependency list
ADD_EXECUTABLE( ${APP_NAME} ... ${trans_outfile} )

Use ${Qt5Core_RCC_EXECUTABLE} instead of ${QT_RCC_EXECUTABLE} if you use Qt 5.

想你的星星会说话 2024-09-14 07:57:05

我在 CMake 3.0(也许更早)中找到了一种非常简单的方法,没有 ADD_CUSTOM_COMMAND 和其他复杂性。

首先,您应该创建一个 QRC 文件,其中列出了所有 .qm 文件(谢谢,the_fly_123< /a> ):

<RCC>
  <qresource prefix="/translators">
    <file>myapp_en.qm</file>
  </qresource>
</RCC>

然后您可以使用 configure_file 将此 QRC 文件复制到输出目录中,并使用标准 Qt 例程来构建和添加它:

# Change lang.qrc to the name of QRC file, created on the previous step
set(lang_qrc "lang.qrc")
configure_file(${lang_qrc} ${lang_qrc} COPYONLY)
qt5_add_translation(myapp_QM ${myapp_TRANSLATIONS})
qt5_add_resources(myapp_QM_RC ${CMAKE_CURRENT_BINARY_DIR}/${lang_qrc})

然后将 ${myapp_QM_RC} 包含在add_executable 以及其他来源。
注意:对于 Qt4,请将所有 qt5_ 前缀替换为 qt4_

I have found a very simple way to do it in CMake 3.0 (and, maybe, earlier) without ADD_CUSTOM_COMMAND and other complications.

First, you should create a QRC file with all .qm files listed ( thanks, the_fly_123 ):

<RCC>
  <qresource prefix="/translators">
    <file>myapp_en.qm</file>
  </qresource>
</RCC>

Then you can copy this QRC file into the output directory using configure_file and use standard Qt routines to build and add it:

# Change lang.qrc to the name of QRC file, created on the previous step
set(lang_qrc "lang.qrc")
configure_file(${lang_qrc} ${lang_qrc} COPYONLY)
qt5_add_translation(myapp_QM ${myapp_TRANSLATIONS})
qt5_add_resources(myapp_QM_RC ${CMAKE_CURRENT_BINARY_DIR}/${lang_qrc})

Then include ${myapp_QM_RC} in add_executable along with other sources.
Note: For Qt4 replace all qt5_ prefixes with qt4_

丶视觉 2024-09-14 07:57:05

我的解决方案是从头开始生成带有编译翻译的 ts.qrc XML 文件,然后将其与 app 进行编译。

以下是示例:

file(GLOB QRC_FILES *.qrc)
file(GLOB TS_FILES ts/*.ts)
...

# Option for updating translations
option(UPDATE_TRANSLATIONS "Update source translation ts/*.ts files (WARNING: make clean will delete the source *.ts files. Danger!)" OFF)
if(UPDATE_TRANSLATIONS)
qt4_create_translation(QM_FILES ${TS_FILES})
endif()
...
# Compiling translations *.ts -> *.qm
qt4_add_translation(QM_FILES ${TS_FILES})
...
# Create translations QRC file - ts.qrc
set(TRANSLATIONS_QRC "${CMAKE_CURRENT_BINARY_DIR}/ts.qrc")
file(WRITE ${TRANSLATIONS_QRC} "<RCC>\n\t<qresource prefix=\"/ts\">")
foreach(QM_FILE ${QM_FILES})
    get_filename_component(QM_FILE_NAME ${QM_FILE} NAME)
    file(APPEND ${TRANSLATIONS_QRC} "\n\t\t<file alias=\"${QM_FILE_NAME}\">${QM_FILE_NAME}</file>")
endforeach()
file(APPEND ${TRANSLATIONS_QRC} "\n\t</qresource>\n</RCC>")
list(APPEND QRC_FILES ${TRANSLATIONS_QRC})
...
# Compiling *.qrc files
qt4_add_resources(QRC_SRCS ${QRC_FILES})
...
# Add compiled resources to executable dependency list
add_executable(${APP_NAME} ... ${QRC_SRCS})

文件树:

/ - source code root
/rc.qrc - contains app icons etc.
/ts/appname_*.ts - application translations
...
/build - build root
/build/appname_*.qm - compiled translations
/build/ts.qrc - translations rescources
/build/Release/qrc_rc.cxx - compiled icon etc. resources
/build/Release/qrc_ts.cxx - compiled translation resources

ts 目录中的文件最初由 lupdate 工具生成。

My solution is to generate ts.qrc XML file with compiled translations from scratch and then complie it with app.

Here is example:

file(GLOB QRC_FILES *.qrc)
file(GLOB TS_FILES ts/*.ts)
...

# Option for updating translations
option(UPDATE_TRANSLATIONS "Update source translation ts/*.ts files (WARNING: make clean will delete the source *.ts files. Danger!)" OFF)
if(UPDATE_TRANSLATIONS)
qt4_create_translation(QM_FILES ${TS_FILES})
endif()
...
# Compiling translations *.ts -> *.qm
qt4_add_translation(QM_FILES ${TS_FILES})
...
# Create translations QRC file - ts.qrc
set(TRANSLATIONS_QRC "${CMAKE_CURRENT_BINARY_DIR}/ts.qrc")
file(WRITE ${TRANSLATIONS_QRC} "<RCC>\n\t<qresource prefix=\"/ts\">")
foreach(QM_FILE ${QM_FILES})
    get_filename_component(QM_FILE_NAME ${QM_FILE} NAME)
    file(APPEND ${TRANSLATIONS_QRC} "\n\t\t<file alias=\"${QM_FILE_NAME}\">${QM_FILE_NAME}</file>")
endforeach()
file(APPEND ${TRANSLATIONS_QRC} "\n\t</qresource>\n</RCC>")
list(APPEND QRC_FILES ${TRANSLATIONS_QRC})
...
# Compiling *.qrc files
qt4_add_resources(QRC_SRCS ${QRC_FILES})
...
# Add compiled resources to executable dependency list
add_executable(${APP_NAME} ... ${QRC_SRCS})

File tree:

/ - source code root
/rc.qrc - contains app icons etc.
/ts/appname_*.ts - application translations
...
/build - build root
/build/appname_*.qm - compiled translations
/build/ts.qrc - translations rescources
/build/Release/qrc_rc.cxx - compiled icon etc. resources
/build/Release/qrc_ts.cxx - compiled translation resources

Files in ts dir initially generated by lupdate tool.

冰葑 2024-09-14 07:57:05

您需要使用 Qt 资源系统将翻译直接包含到应用程序二进制文件中。使用 QT4_ADD_RESOURCES 宏来执行此操作。有一些如何使用它的示例: http://www.qtcentre.org /wiki/index.php?title=Compiling_Qt4_apps_with_CMake

You need to use Qt resources system to include your translation directly into your application binary. Use QT4_ADD_RESOURCES macro to do this. There is some example how to use it: http://www.qtcentre.org/wiki/index.php?title=Compiling_Qt4_apps_with_CMake

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文