Skip to content

Commit

Permalink
添加 语言翻译模块;
Browse files Browse the repository at this point in the history
  • Loading branch information
RealChuan committed Apr 16, 2024
1 parent a96352d commit 131e26d
Show file tree
Hide file tree
Showing 23 changed files with 1,645 additions and 161 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- '.github/workflows/readme.yml'
- '.github/workflows/toolchain.yml'
- 'doc/**'
- 'translation/**'
- '.clang-*'
- '.gitignore'
- 'LICENSE'
Expand All @@ -22,6 +23,7 @@ on:
- '.github/workflows/readme.yml'
- '.github/workflows/toolchain.yml'
- 'doc/**'
- 'translation/**'
- '.clang-*'
- '.gitignore'
- 'LICENSE'
Expand All @@ -40,6 +42,8 @@ jobs:
- ubuntu-latest
qt_ver:
- 6.7.0
qt_modules:
- qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats
build_type:
- "RelWithDebInfo"
generators:
Expand All @@ -51,6 +55,7 @@ jobs:
include:
- os: macos-latest
qt_ver: 6.7.0
qt_modules: qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats
build_type: "RelWithDebInfo"
generators: "Ninja"
arch: arm64
Expand Down Expand Up @@ -112,7 +117,7 @@ jobs:
with:
version: ${{ matrix.qt_ver }}
install-deps: 'true'
modules: 'qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats'
modules: '${{ matrix.qt_modules }}'
cache: 'true'

- uses: actions/checkout@v4
Expand Down Expand Up @@ -171,7 +176,7 @@ jobs:
for file in $files;
do
if [[ $file == *.exe || $file == *.dll ]]; then
windeployqt --force --no-translations --compiler-runtime $file
windeployqt --force --compiler-runtime $file
fi
done
rm -rf *plugin.dll
Expand Down Expand Up @@ -207,16 +212,14 @@ jobs:
./linuxdeployqt-continuous-x86_64.AppImage \
./bin-64/${{ matrix.build_type }}/$so_file \
-qmake=${{ env.Qt6_DIR }}/bin/qmake \
-always-overwrite -no-translations \
-unsupported-allow-new-glibc
-always-overwrite -unsupported-allow-new-glibc
done
rm -f ./bin-64/${{ matrix.build_type }}/AppRun
./linuxdeployqt-continuous-x86_64.AppImage \
./bin-64/${{ matrix.build_type }}/Qt-App \
-executable=./bin-64/${{ matrix.build_type }}/CrashReport \
-qmake=${{ env.Qt6_DIR }}/bin/qmake \
-always-overwrite -no-translations \
-unsupported-allow-new-glibc \
-always-overwrite -unsupported-allow-new-glibc \
-appimage
rm linuxdeployqt-continuous-x86_64.AppImage
rm -f ./bin-64/${{ matrix.build_type }}/*.so
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/qmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- '.github/workflows/readme.yml'
- '.github/workflows/toolchain.yml'
- 'doc/**'
- 'translation/**'
- '.clang-*'
- '.gitignore'
- 'LICENSE'
Expand All @@ -23,6 +24,7 @@ on:
- '.github/workflows/readme.yml'
- '.github/workflows/toolchain.yml'
- 'doc/**'
- 'translation/**'
- '.clang-*'
- '.gitignore'
- 'LICENSE'
Expand All @@ -42,13 +44,16 @@ jobs:
- ubuntu-latest
qt_ver:
- 6.7.0
qt_modules:
- qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats
arch:
- x86_64
vcpkg_libs:
- breakpad
include:
- os: macos-latest
qt_ver: 6.7.0
qt_modules: qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats
arch: arm64
vcpkg_libs: breakpad

Expand Down Expand Up @@ -108,7 +113,7 @@ jobs:
with:
version: ${{ matrix.qt_ver }}
install-deps: 'true'
modules: 'qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats'
modules: '${{ matrix.qt_modules }}'
cache: 'true'

- uses: actions/checkout@v4
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
- ubuntu-latest
qt_ver:
- 6.7.0
qt_modules:
- qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats
vcpkg_libs:
- breakpad

Expand Down Expand Up @@ -73,5 +75,5 @@ jobs:
with:
version: ${{ matrix.qt_ver }}
install-deps: 'true'
modules: 'qt5compat qtserialport qtnetworkauth qtmultimedia qtimageformats'
modules: '${{ matrix.qt_modules }}'
cache: 'true'
37 changes: 25 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,11 @@ project(
VERSION 0.0.1
LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
# qt_standard_project_setup() 这个识别不了

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CURRENT_SOURCE_DIR ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_DEBUG_POSTFIX d)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Expand Down Expand Up @@ -82,8 +74,13 @@ find_package(
Multimedia
OpenGLWidgets
SerialPort
# LinguistTools
)
LinguistTools)

qt_standard_project_setup(I18N_SOURCE_LANGUAGE en I18N_TRANSLATED_LANGUAGES
zh_CN)
# qt_standard_project_setup will set CMAKE_RUNTIME_OUTPUT_DIRECTORY, we need to
# set it back, and use EXECUTABLE_OUTPUT_PATH
unset(CMAKE_RUNTIME_OUTPUT_DIRECTORY)

find_package(unofficial-breakpad CONFIG REQUIRED)
if(unofficial-breakpad_FOUND)
Expand All @@ -92,3 +89,19 @@ endif()

include_directories(src)
add_subdirectory(src)
add_subdirectory(examples)

string(TOLOWER ${PROJECT_NAME} Lower_PROJECT_NAME)

qt_add_translations(
${PROJECT_NAME}
TS_FILE_BASE
${Lower_PROJECT_NAME}
TS_FILE_DIR
translations
INCLUDE_DIRECTORIES
directory
${PROJECT_SOURCE_DIR}/src)

file(GLOB_RECURSE TS_FILES "${PROJECT_SOURCE_DIR}/translations/*.ts")
set_qt_translations_output_location(${TS_FILES})
8 changes: 7 additions & 1 deletion Qt-App.pro
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
TEMPLATE = subdirs
CONFIG += ordered

SUBDIRS += src
SUBDIRS += \
src \
examples

TRANSLATIONS += \
translations/qt-app_en.ts \
translations/qt-app_zh_CN.ts

DISTFILES += \
doc/** \
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,24 @@
3. 在使用[qmake](.github/workflows/qmake.yml)时,需要指定`QMAKE_APPLE_DEVICE_ARCHS=x86_64`或者`QMAKE_APPLE_DEVICE_ARCHS=arm64`

- 为什么不使用google crashpad作为崩溃捕捉模块?
1. vcpkg对crashpad在macos和linux下支持不好;
2. 如果想要使用crashpad,[参考代码](https://github.com/RealChuan/Cpp-Examples/tree/master/Crashpad);
1. [vcpkg](https://github.com/microsoft/vcpkg)[crashpad](https://github.com/chromium/crashpad)`macos``linux`下支持不好;
2. 如果想要使用[crashpad](https://github.com/chromium/crashpad)[参考代码](https://github.com/RealChuan/Cpp-Examples/tree/master/Crashpad);

- 国际化实时翻译,当前更改完翻译设置,需要重启程序才能生效;
1. 懒得改代码了;
2. [具体参考:QT实用小技巧(想到就更新)](https://realchuan.github.io/2021/10/12/QT%E5%AE%9E%E7%94%A8%E5%B0%8F%E6%8A%80%E5%B7%A7%EF%BC%88%E6%83%B3%E5%88%B0%E5%B0%B1%E6%9B%B4%E6%96%B0%EF%BC%89/),核心代码;

```cpp
void Widget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
comboBox->setItemText(0, tr("Hello"));
label->setText(tr("Hello")); // 代码添加的文字
ui->retranslateUi(this); // 有UI文件情况下
break;
default: break;
}
}
```
18 changes: 18 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ function(add_rpath target_name)
PROPERTIES LINK_FLAGS "-Wl,-rpath,./:./lib:./../lib")
endif()
endfunction()

function(set_qt_translations_output_location)
set(options "")
set(oneValueArgs "")
set(multiValueArgs TS_FILES)

if(CMAKE_HOST_APPLE)
set(output_location
"${EXECUTABLE_OUTPUT_PATH}/${PROJECT_NAME}.app/Contents/Resources/translations"
)
else()
set(output_location "${EXECUTABLE_OUTPUT_PATH}/translations")
endif()
foreach(ts_file ${TS_FILES})
set_source_files_properties("${ts_file}" PROPERTIES OUTPUT_LOCATION
"${output_location}")
endforeach(ts_file)
endfunction()
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(i18n)
5 changes: 5 additions & 0 deletions examples/examples.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TEMPLATE = subdirs
CONFIG += ordered

SUBDIRS += \
i18n
8 changes: 8 additions & 0 deletions examples/i18n/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set(PROJECT_SOURCES main.cc)
qt_add_executable(I18N MANUAL_FINALIZATION ${PROJECT_SOURCES})
set_target_properties(I18N PROPERTIES MACOSX_BUNDLE FALSE WIN32_EXECUTABLE TRUE)
set_target_properties(I18N PROPERTIES RUNTIME_OUTPUT_DIRECTORY
"${EXECUTABLE_OUTPUT_PATH}/examples")

target_link_libraries(I18N PRIVATE Qt6::Core)
qt_finalize_executable(I18N)
14 changes: 14 additions & 0 deletions examples/i18n/i18n.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include(../../common.pri)

QT = core

CONFIG += cmdline

TEMPLATE = app

TARGET = I18N

SOURCES += \
main.cc

DESTDIR = $$APP_OUTPUT_PATH/examples
10 changes: 10 additions & 0 deletions examples/i18n/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <QLocale>
#include <QMetaEnum>

auto main(int argc, char *argv[]) -> int
{
auto metaEnums = QMetaEnum::fromType<QLocale::Language>();
for (int i = 0; i < metaEnums.keyCount(); ++i) {
qDebug() << metaEnums.key(i) << metaEnums.value(i);
}
}
52 changes: 22 additions & 30 deletions src/apps/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
set(PROJECT_SOURCES main.cc)

if(CMAKE_HOST_WIN32)
qt_add_executable(Qt-App MANUAL_FINALIZATION ${PROJECT_SOURCES} app.rc)
qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_SOURCES}
app.rc)
elseif(CMAKE_HOST_APPLE)
qt_add_executable(Qt-App MACOSX_BUNDLE MANUAL_FINALIZATION ${PROJECT_SOURCES}
app.icns PkgInfo)
qt_add_executable(${PROJECT_NAME} MACOSX_BUNDLE MANUAL_FINALIZATION
${PROJECT_SOURCES} app.icns PkgInfo)
set_target_properties(
Qt-App
PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Qt-App"
MACOSX_BUNDLE_EXECUTABLE_NAME "Qt-App"
${PROJECT_NAME}
PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "${PROJECT_NAME}"
MACOSX_BUNDLE_EXECUTABLE_NAME "${PROJECT_NAME}"
MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION}"
MACOSX_BUNDLE_COPYRIGHT "(C) 2023 Youth"
MACOSX_BUNDLE_GUI_IDENTIFIER "com.Youth.Qt-App"
MACOSX_BUNDLE_GUI_IDENTIFIER "com.Youth.${PROJECT_NAME}"
MACOSX_BUNDLE_ICON_FILE app.icns
MACOSX_BUNDLE_INFO_STRING "APPL???")
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/app.icns
PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/PkgInfo
PROPERTIES MACOSX_PACKAGE_LOCATION .)
add_dependencies(Qt-App coreplugin serialplugin tcpplugin CrashReport)
add_rpath(Qt-App)
add_dependencies(${PROJECT_NAME} coreplugin serialplugin tcpplugin
CrashReport)
add_rpath(${PROJECT_NAME})
else()
qt_add_executable(Qt-App MANUAL_FINALIZATION ${PROJECT_SOURCES})
add_rpath(Qt-App)
qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_SOURCES})
add_rpath(${PROJECT_NAME})
endif()

set_target_properties(Qt-App PROPERTIES MACOSX_BUNDLE TRUE WIN32_EXECUTABLE
TRUE)
set_target_properties(${PROJECT_NAME} PROPERTIES MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE)

target_link_libraries(
Qt-App
${PROJECT_NAME}
PRIVATE extensionsystem
thirdparty
gui
Expand All @@ -42,28 +44,18 @@ target_link_libraries(
unofficial::breakpad::libbreakpad
unofficial::breakpad::libbreakpad_client)

# qt translations file(MAKE_DIRECTORY ${PROJECT_SOURCE_DIR}/translations)
# set(TS_FILES ${PROJECT_SOURCE_DIR}/translations/Qt-App_zh_CN.ts
# ${PROJECT_SOURCE_DIR}/translations/Qt-App_en.ts)
# qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES} OPTIONS
# -no-obsolete) target_sources(Qt-App PRIVATE ${QM_FILES})
# add_custom_target(translations DEPENDS ${QM_FILES}) add_custom_command( TARGET
# Qt-App POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory
# ${EXECUTABLE_OUTPUT_PATH}/translations/ COMMAND ${CMAKE_COMMAND} -E
# copy_if_different ${QM_FILES} ${EXECUTABLE_OUTPUT_PATH}/translations/ COMMENT
# "Copying translations to build dir")

qt_finalize_executable(Qt-App)
qt_finalize_executable(${PROJECT_NAME})

if(CMAKE_HOST_APPLE)
add_custom_command(
TARGET Qt-App
TARGET ${PROJECT_NAME}
POST_BUILD
# copy plugins to bundle
COMMAND
${CMAKE_COMMAND} -E copy_directory ${EXECUTABLE_OUTPUT_PATH}/plugins
${EXECUTABLE_OUTPUT_PATH}/Qt-App.app/Contents/MacOS/plugins
${EXECUTABLE_OUTPUT_PATH}/${PROJECT_NAME}.app/Contents/MacOS/plugins
# copy crashrepoter to bundle
COMMAND ${CMAKE_COMMAND} -E copy ${EXECUTABLE_OUTPUT_PATH}/CrashReport
${EXECUTABLE_OUTPUT_PATH}/Qt-App.app/Contents/MacOS/CrashReport)
COMMAND
${CMAKE_COMMAND} -E copy ${EXECUTABLE_OUTPUT_PATH}/CrashReport
${EXECUTABLE_OUTPUT_PATH}/${PROJECT_NAME}.app/Contents/MacOS/CrashReport)
endif()
Loading

0 comments on commit 131e26d

Please sign in to comment.