Функция для подключения переводов

This commit is contained in:
Andrei Astafev 2019-10-07 18:30:28 +03:00
parent e6a65b884b
commit 424c669eb6
8 changed files with 206 additions and 0 deletions

View File

@ -45,11 +45,13 @@ include(ExternalProject)
add_subdirectory(src/base) add_subdirectory(src/base)
add_subdirectory(src/filesystem) add_subdirectory(src/filesystem)
add_subdirectory(src/log) add_subdirectory(src/log)
add_subdirectory(src/qt)
# Примеры # Примеры
if (BUILD_EXAMPLES) if (BUILD_EXAMPLES)
add_subdirectory(examples/filesystem) add_subdirectory(examples/filesystem)
add_subdirectory(examples/log) add_subdirectory(examples/log)
add_subdirectory(examples/qt)
endif() endif()
ExternalProject_Add(whereami ExternalProject_Add(whereami

View File

@ -0,0 +1,48 @@
# Название основной цели в текущем каталоге
set(current_target example-qt-translators)
# Список файлов исходных текстов
set(current_target_sources
${CMAKE_CURRENT_SOURCE_DIR}/translators.cpp
)
# Путь поиска библиотек внутри проекта
link_directories(${CMAKE_INSTALL_LIBDIR})
link_directories(${CMAKE_BINARY_DIR}/src/qt/lib)
# Цель для создания исполняемого файла
add_executable(${current_target} ${current_target_sources})
common_target_properties(${current_target})
add_clang_tidy_check(${current_target} ${current_target_sources})
add_clang_analyze_check(${current_target} ${current_target_sources})
add_clazy_check(${current_target} ${current_target_sources})
add_uncrustify_format(${current_target} ${current_target_sources})
add_pvs_check(${current_target})
add_dependencies(${current_target} base)
add_dependencies(${current_target} qt)
# Qt5
qt_translation(TARGET ${current_target} TS_DIR ${CMAKE_SOURCE_DIR}/l10n LANGUAGES ru_RU)
target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(${current_target} SYSTEM PUBLIC ${FMT_INCLUDE_DIRS})
target_include_directories(${current_target} SYSTEM PUBLIC ${SPDLOG_INCLUDE_DIRS})
target_include_directories(${current_target} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_compile_options(${current_target} PUBLIC "${Qt5Core_EXECUTABLE_COMPILE_FLAGS}")
target_link_libraries(${current_target} myx-qt)
target_link_libraries(${current_target} Qt5::Core)
# Имя выходного файла для цели
set_target_properties(${current_target}
PROPERTIES
OUTPUT_NAME qt-translators
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}
)
add_sanitizers(${current_target})
# cotire(${current_target})
# Правила для установки
install(TARGETS ${current_target} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@ -0,0 +1,19 @@
#include "qt/translators.hpp"
#include <QCoreApplication>
#include <QDebug>
namespace MQ = myx::qt;
int main( int argc, char** argv )
{
QCoreApplication app( argc, argv );
MQ::QTranslatorsList tl;
qDebug() << QObject::tr( "Yes" );
MQ::append_translators( tl, "example-qt-translators" );
qDebug() << QObject::tr( "Yes" );
return( 0 );
}

View File

@ -0,0 +1,2 @@
add_subdirectory(01_translators)

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="ru_RU">
<context>
<name>QObject</name>
<message>
<location filename="../examples/qt/01_translators/translators.cpp" line="16"/>
<source>Yes</source>
<translation>Да</translation>
</message>
<message>
<location filename="../examples/qt/01_translators/translators.cpp" line="18"/>
<source>Cancel</source>
<translation>Отмена</translation>
</message>
</context>
</TS>

43
src/qt/CMakeLists.txt Normal file
View File

@ -0,0 +1,43 @@
# Название основной цели и имя библиотеки в текущем каталоге
set(current_target qt)
# Список файлов исходных текстов
set(current_target_sources
${CMAKE_CURRENT_SOURCE_DIR}/translators.cpp
)
# Список заголовочных файлов (используется для установки)
set(current_target_headers
${CMAKE_CURRENT_SOURCE_DIR}/translators.hpp
)
add_common_library(TARGET ${current_target} OUTPUT_NAME myx-${current_target}
SOURCES ${current_target_sources})
common_target_properties(${current_target})
add_clang_tidy_check(${current_target} ${current_target_sources})
add_clang_analyze_check(${current_target} ${current_target_sources})
add_clazy_check(${current_target} ${current_target_sources})
add_pvs_check(${current_target})
add_uncrustify_format(${current_target} ${current_target_sources} ${current_target_headers})
target_include_directories(${current_target} SYSTEM PUBLIC ${Qt5Core_INCLUDE_DIRS})
target_include_directories(${current_target} SYSTEM PUBLIC ${FMT_INCLUDE_DIRS})
target_include_directories(${current_target} SYSTEM PUBLIC ${SPDLOG_INCLUDE_DIRS})
target_include_directories(${current_target} PRIVATE ${CMAKE_SOURCE_DIR}/src/base)
# Цель, используемая только для установки заголовочных файлов без компиляции проекта
add_custom_target(${current_target}-install-headers
COMMAND "${CMAKE_COMMAND}"
-DCMAKE_INSTALL_COMPONENT=headers -P "${CMAKE_BINARY_DIR}/cmake_install.cmake"
)
# Правила для установки
install(TARGETS ${current_target}_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(BUILD_SHARED_LIBS)
install(TARGETS ${current_target}_shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
install(FILES ${current_target_headers}
COMPONENT headers
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/${current_target})
install(FILES ${CMAKE_BINARY_DIR}/${current_target}.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)

57
src/qt/translators.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "config.hpp"
#include "translators.hpp"
#include <QCoreApplication>
#include <QLocale>
#include <QLibraryInfo>
namespace myx {
namespace qt {
void append_translators( QTranslatorsList& translators, const QString& appName )
{
auto* translator = new QTranslator( qApp );
if ( translator->load( QLocale(), appName, QStringLiteral( "_" ),
QStringLiteral( ":/qm" ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
translator = new QTranslator( qApp );
if ( translator->load( QLocale::system(),
QStringLiteral( "qt" ), QStringLiteral( "_" ),
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
translator = new QTranslator( qApp );
if ( translator->load( QStringLiteral( "qtbase_" ) + QLocale::system().name(),
QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
{
translators.append( translator );
}
else
{
translator->deleteLater();
}
for ( auto i : qAsConst( translators ) )
{
qApp->installTranslator( i );
}
} // install_translators
} // namespace qt
} // namespace myx

18
src/qt/translators.hpp Normal file
View File

@ -0,0 +1,18 @@
#ifndef MYX_LOG_QT_TRANSLATORS_HPP_
#define MYX_LOG_QT_TRANSLATORS_HPP_
#include <QTranslator>
namespace myx {
namespace qt {
typedef QList< QTranslator* > QTranslatorsList;
void append_translators( QTranslatorsList& translators, const QString& appName );
} // namespace qt
} // namespace myx
#endif // ifndef MYX_LOG_QT_TRANSLATORS_HPP_