diff --git a/dp4-login-plugin/CMakeLists.txt b/dp4-login-plugin/CMakeLists.txt index 185a547..51753cf 100644 --- a/dp4-login-plugin/CMakeLists.txt +++ b/dp4-login-plugin/CMakeLists.txt @@ -8,29 +8,36 @@ include(GNUInstallDirs) # 启用 qt moc 的支持 set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) + +file(GLOB_RECURSE SRCS "*.h" "*.cpp") + +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + ADD_DEFINITIONS(-DQM_FILES_DIR="${CMAKE_BINARY_DIR}/${LIB_NAME}") +else() + ADD_DEFINITIONS(-DQM_FILES_DIR="/usr/share/${LIB_NAME}/translations") +endif() find_package(Qt5 COMPONENTS Core Widgets DBus Network LinguistTools REQUIRED) find_package(PkgConfig REQUIRED) find_package(DdeSessionShell REQUIRED) -pkg_check_modules(SSL REQUIRED IMPORTED_TARGET libcrypto libssl openssl) - file(GLOB TS_FILES "translations/*.ts") qt5_add_translation(QM_FILES ${TS_FILES}) -add_custom_target(${BIN_NAME}_language ALL DEPENDS ${QM_FILES}) - -file(GLOB_RECURSE SRCS "*.h" "*.cpp") +add_custom_target(${LIB_NAME}_language ALL DEPENDS ${QM_FILES}) add_library(${LIB_NAME} SHARED ${SRCS}) target_include_directories(${LIB_NAME} PUBLIC - ${Qt5DBus_INCLUDE_DIRS} - ${DDESESSIONSHELL_INCLUDE_DIR} + Qt5::Core + Qt5::DBus + ${DDESESSIONSHELL_INCLUDE_DIR} ) target_link_libraries(${LIB_NAME} PRIVATE - ${Qt5Widgets_LIBRARIES} - ${Qt5DBus_LIBRARIES} + Qt5::Core + Qt5::DBus + ${Qt5Widgets_LIBRARIES} ) install(TARGETS ${LIB_NAME} LIBRARY DESTINATION lib/dde-session-shell/modules)# 安装.qm文件 diff --git a/dp4-login-plugin/dp4_login_module.cpp b/dp4-login-plugin/dp4_login_module.cpp index bb2d8d6..4c04012 100644 --- a/dp4-login-plugin/dp4_login_module.cpp +++ b/dp4-login-plugin/dp4_login_module.cpp @@ -4,6 +4,9 @@ #include "dp4_login_module.h" #include "dp4loginwidget.h" +#include +#include + namespace dss { namespace module @@ -16,7 +19,8 @@ Dp4LoginModule::Dp4LoginModule(QObject *parent) , m_messageCallbackFunc(nullptr) , m_dp4Widget(new Dp4LoginWidget) { - setObjectName(QStringLiteral("AssistLoginModule")); + setObjectName(QStringLiteral("Dp4LoginModule")); + installTranslator(); QString testText = tr("test"); // 以下部分爲測試代碼,具體如何寫請根據實際的業務來處理即可 @@ -47,6 +51,19 @@ void Dp4LoginModule::reset() init(); } +void Dp4LoginModule::installTranslator() +{ + // 加载翻译文件 + QString translationFile = QString("%1/dp4-login-plugin_%2.qm").arg(QM_FILES_DIR).arg(QLocale::system().name()); + QFile file(translationFile); + if (!file.exists()) + return; + + static QTranslator translator(this); + translator.load(translationFile); + QApplication::installTranslator(&translator); +} + void Dp4LoginModule::setCallback(LoginCallBack *callback) { m_callback = callback; diff --git a/dp4-login-plugin/dp4_login_module.h b/dp4-login-plugin/dp4_login_module.h index 2c43a4d..ceeb27f 100644 --- a/dp4-login-plugin/dp4_login_module.h +++ b/dp4-login-plugin/dp4_login_module.h @@ -34,6 +34,9 @@ public: std::string onMessage(const std::string &) override; void reset() override; +private: + void installTranslator(); + private: LoginCallBack *m_callback; AuthCallbackFun m_callbackFun; diff --git a/dp4-login-plugin/translations/dp4-login-plugin_zh_CN.ts b/dp4-login-plugin/translations/dp4-login-plugin_zh_CN.ts new file mode 100644 index 0000000..0b7efac --- /dev/null +++ b/dp4-login-plugin/translations/dp4-login-plugin_zh_CN.ts @@ -0,0 +1,11 @@ + + + + + dss::module::Dp4LoginModule + + test + 测试 + + + diff --git a/dp4-login-plugin/translations/dp4-login-plugin_zh_HK.ts b/dp4-login-plugin/translations/dp4-login-plugin_zh_HK.ts new file mode 100644 index 0000000..0b7efac --- /dev/null +++ b/dp4-login-plugin/translations/dp4-login-plugin_zh_HK.ts @@ -0,0 +1,11 @@ + + + + + dss::module::Dp4LoginModule + + test + 测试 + + + diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 9bed5d3..258a9b6 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -16,11 +16,13 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall") set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules) if (CMAKE_BUILD_TYPE STREQUAL "Debug") + ADD_DEFINITIONS(-DQM_FILES_DIR="${CMAKE_BINARY_DIR}/dp4-login-plugin") ADD_DEFINITIONS(-DSERVICE_CONFIG_DIR="${CMAKE_BINARY_DIR}/share/") ADD_DEFINITIONS(-DSERVICE_LIB_DIR="${CMAKE_BINARY_DIR}/dp4-dbus-service/") else() ADD_DEFINITIONS(-DSERVICE_CONFIG_DIR="${CMAKE_INSTALL_PREFIX}/share/deepin-service-manager/") ADD_DEFINITIONS(-DSERVICE_LIB_DIR="${CMAKE_INSTALL_FULL_LIBDIR}/deepin-service-manager/") + ADD_DEFINITIONS(-DQM_FILES_DIR="/usr/lib/dp4-login-plugin/translations") endif() file(GLOB_RECURSE COMPILEFILES *.h *.cpp ../dp4-login-plugin/*.h ../dp4-login-plugin/*.cpp)