Skip to content

Commit 7359b89

Browse files
committed
in addition load the qt base translations
1 parent 500d98f commit 7359b89

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

src/osmin.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QtQuickControls2>
2525
#include <QStandardPaths>
2626
#include <QTranslator>
27+
#include <QLibraryInfo>
2728
#include <QDebug>
2829
#include <QDir>
2930
#include <QProcess>
@@ -112,8 +113,8 @@
112113

113114
int startService(int argc, char* argv[]);
114115
int startGUI(int argc, char* argv[]);
115-
void setupApp(QGuiApplication& app, QTranslator& translator);
116-
void prepareTranslator(QTranslator& translator, const QString& translationPath, const QString& translationPrefix, const QLocale& locale);
116+
void setupApp(QGuiApplication& app);
117+
void prepareTranslator(QGuiApplication& app, const QString& translationPath, const QString& translationPrefix, const QLocale& locale);
117118
void signalCatched(int signal);
118119
void doExit(int code);
119120
bool testServiceUrl(const char *url, int timeout);
@@ -217,8 +218,7 @@ int startGUI(int argc, char* argv[])
217218
QGuiApplication::setApplicationDisplayName(APP_DISPLAY_NAME);
218219
QGuiApplication::setOrganizationName(ORG_NAME);
219220
QGuiApplication app(argc, argv);
220-
QTranslator translator;
221-
setupApp(app, translator);
221+
setupApp(app);
222222

223223
// check installed asset
224224
g_assetDir = QDir(PlatformExtras::getAssetDir(APP_ID));
@@ -600,7 +600,7 @@ int startGUI(int argc, char* argv[])
600600
return ret;
601601
}
602602

603-
void setupApp(QGuiApplication& app, QTranslator& translator)
603+
void setupApp(QGuiApplication& app)
604604
{
605605

606606
SignalHandler *sh = new SignalHandler(&app);
@@ -613,26 +613,45 @@ void setupApp(QGuiApplication& app, QTranslator& translator)
613613
QLocale locale = QLocale::system();
614614
qInfo("User locale setting is %s", std::locale().name().c_str());
615615
// set translators
616-
prepareTranslator(translator, QString(":/i18n"), QString(APP_TR_NAME), locale);
616+
prepareTranslator(app, QString(":/i18n"), QString(APP_TR_NAME), locale);
617617
#ifdef Q_OS_MAC
618618
QDir appDir(app.applicationDirPath());
619619
if (appDir.cdUp() && appDir.cd("Resources/translations"))
620620
prepareTranslator(translator, appDir.absolutePath(), "qt", locale);
621621
#elif defined(Q_OS_ANDROID)
622622
prepareTranslator(translator, "assets:/translations", "qt", locale);
623623
#endif
624-
app.installTranslator(&translator);
625624
app.setWindowIcon(QIcon(QPixmap(":/images/osmin.png")));
626625
}
627626

628-
void prepareTranslator(QTranslator& translator, const QString& translationPath, const QString& translationPrefix, const QLocale& locale)
627+
void prepareTranslator(QGuiApplication& app, const QString& translationPath, const QString& translationPrefix, const QLocale& locale)
629628
{
629+
// load app translations
630630
QString i18Path(translationPath);
631631
i18Path.append("/").append(translationPrefix).append("_").append(locale.name().left(2)).append(".qm");
632-
if (!translator.load(locale, translationPrefix, QString("_"), translationPath))
633-
qWarning("no file found for translations '%s' (using default).", i18Path.toUtf8().constData());
634-
else
632+
QTranslator * translator = new QTranslator(&app);
633+
if (translator->load(locale, translationPrefix, QString("_"), translationPath))
634+
{
635635
qInfo("using file '%s' for translations.", i18Path.toUtf8().constData());
636+
app.installTranslator(translator);
637+
638+
// try to load qt base translations
639+
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
640+
QString qt_translationPath(QLibraryInfo::location(QLibraryInfo::TranslationsPath));
641+
#else
642+
QString qt_translationPath(QLibraryInfo::path(QLibraryInfo::TranslationsPath));
643+
#endif
644+
QTranslator * qt_translator = new QTranslator(&app);
645+
if (qt_translator->load(locale, "qtbase", "_", qt_translationPath))
646+
{
647+
qInfo("using file '%s' for translations.", qt_translationPath.toUtf8().constData());
648+
app.installTranslator(qt_translator);
649+
}
650+
}
651+
else
652+
{
653+
qWarning("no file found for translations '%s' (using default).", i18Path.toUtf8().constData());
654+
}
636655
}
637656

638657
void signalCatched(int signal)

0 commit comments

Comments
 (0)