Wizards: Extend translation support

Don't just create the ts file, but also make sure it is embedded in the
application (if supported by the build system) and make the application
load it if it matches the current locale.

Task-number: QTCREATORBUG-25079
Change-Id: Ie3a0b1975531b12acb008e8c1312dab97f158751
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-01-11 16:50:34 +01:00
parent 0b215ea822
commit f19baeb9a5
10 changed files with 82 additions and 15 deletions

View File

@@ -13,6 +13,8 @@ SOURCES += \\
TRANSLATIONS += \\
%{TsFileName}
CONFIG += lrelease
CONFIG += embed_translations
@endif
# Default rules for deployment.

View File

@@ -16,6 +16,14 @@ QtApplication {
"%{TsFileName}",
@endif
]
@if %{HasTranslation}
Group {
fileTagsFilter: "qm"
Qt.core.resourcePrefix: "/i18n"
fileTags: "qt.core.resource_data"
}
@endif
Group { // Properties for the produced executable
fileTagsFilter: "application"

View File

@@ -1,10 +1,26 @@
%{Cpp:LicenseTemplate}\
%{JS: QtSupport.qtIncludes([ 'QtCore/QCoreApplication' ],
[ 'QtCore/QCoreApplication' ]) }\
@if %{HasTranslation}
#include <QLocale>
#include <QTranslator>
@endif
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
@if %{HasTranslation}
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "%{JS: value('ProjectName') + '_'}" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
@endif
return a.exec();
}

View File

@@ -18,6 +18,8 @@ RESOURCES += qml.qrc
TRANSLATIONS += \\
%{TsFileName}
CONFIG += lrelease
CONFIG += embed_translations
@endif
# Additional import path used to resolve QML modules in Qt Creator's code model

View File

@@ -26,6 +26,14 @@ CppApplication {
"%{TsFileName}",
@endif
]
@if %{HasTranslation}
Group {
fileTagsFilter: "qm"
Qt.core.resourcePrefix: "/i18n"
fileTags: "qt.core.resource_data"
}
@endif
Group { // Properties for the produced executable
fileTagsFilter: "application"

View File

@@ -1,5 +1,10 @@
%{Cpp:LicenseTemplate}\
%{JS: QtSupport.qtIncludes([], ["QtGui/QGuiApplication", "QtQml/QQmlApplicationEngine"])}
@if %{HasTranslation}
#include <QLocale>
#include <QTranslator>
@endif
int main(int argc, char *argv[])
{
@if %{UseVirtualKeyboard}
@@ -22,6 +27,18 @@ int main(int argc, char *argv[])
@endif
QGuiApplication app(argc, argv);
@if %{HasTranslation}
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "%{JS: value('ProjectName') + '_'}" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
app.installTranslator(&translator);
break;
}
}
@endif
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));

View File

@@ -2,10 +2,26 @@
#include "%{HdrFileName}"
%{JS: QtSupport.qtIncludes([ 'QtGui/QApplication' ], [ 'QtWidgets/QApplication' ]) }\
@if %{HasTranslation}
#include <QLocale>
#include <QTranslator>
@endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
@if %{HasTranslation}
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "%{JS: value('ProjectName') + '_'}" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
@endif
%{Class} w;
w.show();
return a.exec();

View File

@@ -23,6 +23,8 @@ FORMS += \\
TRANSLATIONS += \\
%{TsFileName}
CONFIG += lrelease
CONFIG += embed_translations
@endif
# Default rules for deployment.

View File

@@ -20,6 +20,14 @@ QtApplication {
"%{TsFileName}",
@endif
]
@if %{HasTranslation}
Group {
fileTagsFilter: "qm"
Qt.core.resourcePrefix: "/i18n"
fileTags: "qt.core.resource_data"
}
@endif
install: true
installDir: qbs.targetOS.contains("qnx") ? FileInfo.joinPaths("/tmp", name, "bin") : base

View File

@@ -65,9 +65,7 @@ private:
QComboBox m_languageComboBox;
QLineEdit m_fileNameLineEdit;
QLabel m_suffixLabel;
const QString m_enabledExpr;
bool m_lineEditEdited = false;
};
TranslationWizardPageFactory::TranslationWizardPageFactory()
@@ -113,15 +111,10 @@ TranslationWizardPage::TranslationWizardPage(const QString &enabledExpr)
m_languageComboBox.addItem(lp.first, lp.second);
formLayout->addRow(tr("Language:"), &m_languageComboBox);
const auto fileNameLayout = new QHBoxLayout;
m_fileNameLineEdit.setReadOnly(true);
fileNameLayout->addWidget(&m_fileNameLineEdit);
m_suffixLabel.setText(".ts");
fileNameLayout->addWidget(&m_suffixLabel);
fileNameLayout->addStretch(1);
formLayout->addRow(tr("Translation file:"), fileNameLayout);
connect(&m_fileNameLineEdit, &QLineEdit::textEdited, this, [this] {
emit completeChanged();
m_lineEditEdited = true;
});
connect(&m_languageComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &TranslationWizardPage::updateLineEdit);
}
@@ -152,14 +145,9 @@ bool TranslationWizardPage::validatePage()
void TranslationWizardPage::updateLineEdit()
{
m_fileNameLineEdit.setEnabled(m_languageComboBox.currentIndex() != 0);
m_suffixLabel.setEnabled(m_fileNameLineEdit.isEnabled());
if (m_fileNameLineEdit.isEnabled()) {
if (!m_lineEditEdited) {
const QString projectName
= static_cast<JsonWizard *>(wizard())->stringValue("ProjectName");
m_fileNameLineEdit.setText(projectName + '_'
+ m_languageComboBox.currentData().toString());
}
const QString projectName = static_cast<JsonWizard *>(wizard())->stringValue("ProjectName");
m_fileNameLineEdit.setText(projectName + '_' + m_languageComboBox.currentData().toString());
} else {
m_fileNameLineEdit.clear();
m_fileNameLineEdit.setPlaceholderText(tr("<none>"));