LINUX.ORG.RU

История изменений

Исправление Dr64h, (текущая версия) :

cmake_minimum_required(VERSION 3.24)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)

project("hello_tr")

add_executable(${PROJECT_NAME} "src/main.cpp")

find_package(Qt6 REQUIRED COMPONENTS Core LinguistTools CONFIG)

target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core)

qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES ru)

qt6_add_translations(${PROJECT_NAME}
    TS_OUTPUT_DIRECTORY "translations"
    QM_OUTPUT_DIRECTORY "translations")
#include <QDebug>

#include <QCoreApplication>
#include <QTranslator>
#include <QString>

int main(int argc, char* argv[])
{
    QCoreApplication app{argc, argv};
    QTranslator translator{};
    QString locale_str{app.arguments().isEmpty() ? "en" : app.arguments().at(1)};
    bool res{};

    res = translator.load(
        qApp->applicationName() + "_" + locale_str,
        qApp->applicationDirPath() + "/translations"
    );

    qDebug() << "LOAD_RESULT:" << res;

    res = qApp->installTranslator(&translator);

    qDebug() << "INSTALL_RESULT:" << res;

    QString test_str{QObject::tr("Hello, World!")};

    qDebug() << "LOCALE:" << locale_str;
    qDebug() << "TEST_STRING:" << test_str;

    return app.exec();
}
cmake -B build

cmake --build ./build --target update_translations

sed -i -e 's/<translation type="unfinished"><\/translation>/<translation>Привет, Мир!<\/translation>/g' ./translations/hello_tr_ru.ts

cmake --build ./build

./build/hello_tr en
LOAD_RESULT: true
INSTALL_RESULT: true
LOCALE: "en"
TEST_STRING: "Hello, World!"

./build/hello_tr ru
LOAD_RESULT: true
INSTALL_RESULT: true
LOCALE: "ru"
TEST_STRING: "Привет, Мир!"

Исходная версия Dr64h, :

cmake_minimum_required(VERSION 3.24)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)

project("hello_tr")

add_executable(${PROJECT_NAME} "src/main.cpp")

find_package(Qt6 REQUIRED COMPONENTS Core LinguistTools CONFIG)

target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core)

find_package(Qt6 REQUIRED COMPONENTS LinguistTools)

qt_standard_project_setup(I18N_TRANSLATED_LANGUAGES ru)

qt6_add_translations(${PROJECT_NAME}
    TS_OUTPUT_DIRECTORY "translations"
    QM_OUTPUT_DIRECTORY "translations")
#include <QDebug>

#include <QCoreApplication>
#include <QTranslator>
#include <QString>

int main(int argc, char* argv[])
{
    QCoreApplication app{argc, argv};
    QTranslator translator{};
    QString locale_str{app.arguments().isEmpty() ? "en" : app.arguments().at(1)};
    bool res{};

    res = translator.load(
        qApp->applicationName() + "_" + locale_str,
        qApp->applicationDirPath() + "/translations"
    );

    qDebug() << "LOAD_RESULT:" << res;

    res = qApp->installTranslator(&translator);

    qDebug() << "INSTALL_RESULT:" << res;

    QString test_str{QObject::tr("Hello, World!")};

    qDebug() << "LOCALE:" << locale_str;
    qDebug() << "TEST_STRING:" << test_str;

    return app.exec();
}
cmake -B build

cmake --build ./build --target update_translations

sed -i -e 's/<translation type="unfinished"><\/translation>/<translation>Привет, Мир!<\/translation>/g' ./translations/hello_tr_ru.ts

cmake --build ./build

./build/hello_tr en
LOAD_RESULT: true
INSTALL_RESULT: true
LOCALE: "en"
TEST_STRING: "Hello, World!"

./build/hello_tr ru
LOAD_RESULT: true
INSTALL_RESULT: true
LOCALE: "ru"
TEST_STRING: "Привет, Мир!"