diff --git a/autotests/kxmlgui_unittest.h b/autotests/kxmlgui_unittest.h --- a/autotests/kxmlgui_unittest.h +++ b/autotests/kxmlgui_unittest.h @@ -48,6 +48,7 @@ void testMenusNoXmlFile(); void testShortcuts(); void testPopupMenuParent(); + void testSpecificApplicationLanguageQLocale(); }; #endif diff --git a/autotests/kxmlgui_unittest.cpp b/autotests/kxmlgui_unittest.cpp --- a/autotests/kxmlgui_unittest.cpp +++ b/autotests/kxmlgui_unittest.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -1076,3 +1077,23 @@ QVERIFY(popupMenu); QCOMPARE(popupMenu->parent(), &mainWindow); } + +void KXmlGui_UnitTest::testSpecificApplicationLanguageQLocale() +{ + const QLocale originalSystemLocale = QLocale::system(); + + KDEPrivate::setApplicationSpecificLanguage("uk"); + KDEPrivate::initializeLanguages(); + + QCOMPARE(QLocale::system().language(), QLocale::Ukrainian); + + KDEPrivate::setApplicationSpecificLanguage("wa"); + KDEPrivate::initializeLanguages(); + + QCOMPARE(QLocale::system().language(), QLocale::Walloon); + + KDEPrivate::setApplicationSpecificLanguage(QByteArray()); + KDEPrivate::initializeLanguages(); + + QCOMPARE(QLocale::system(), originalSystemLocale); +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -86,6 +86,7 @@ PRIVATE Qt5::Network #QNetworkAccessManager in kaboutapplicationpersonmodel_p Qt5::PrintSupport #QPrinter in kshortcutseditor + Qt5::CorePrivate #QSystemLocale in initializeLanguages KF5::CoreAddons #KAboutData KF5::WidgetsAddons KF5::ItemViews #KWidgetItemDelegate in KAboutApplicationPersonListDelegate diff --git a/src/kswitchlanguagedialog_p.h b/src/kswitchlanguagedialog_p.h --- a/src/kswitchlanguagedialog_p.h +++ b/src/kswitchlanguagedialog_p.h @@ -22,11 +22,16 @@ #ifndef _KSWITCHLANGUAGEDIALOG_H_ #define _KSWITCHLANGUAGEDIALOG_H_ +#include + #include namespace KDEPrivate { +KXMLGUI_EXPORT void setApplicationSpecificLanguage(const QByteArray &languageCode); +KXMLGUI_EXPORT void initializeLanguages(); + class KSwitchLanguageDialogPrivate; /** diff --git a/src/kswitchlanguagedialog_p.cpp b/src/kswitchlanguagedialog_p.cpp --- a/src/kswitchlanguagedialog_p.cpp +++ b/src/kswitchlanguagedialog_p.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "kswitchlanguagedialog_p.h" @@ -61,7 +62,12 @@ return settings->value(qAppName(), defaultCode).toByteArray(); } -static void setApplicationSpecificLanguage(const QByteArray &languageCode) +namespace KDEPrivate +{ + +Q_COREAPP_STARTUP_FUNCTION(initializeLanguages) + +void setApplicationSpecificLanguage(const QByteArray &languageCode) { QSettingsPtr settings = localeOverridesSettings(); settings->beginGroup(QStringLiteral("Language")); @@ -73,7 +79,7 @@ } } -static void initializeLanguages() +void initializeLanguages() { const QByteArray languageCode = getApplicationSpecificLanguage(); @@ -84,14 +90,17 @@ } else { qputenv("LANGUAGE", languageCode + ":" + languages); } + + // Ideally setting the LANGUAGE would change the default QLocale too + // but unfortunately this is too late since the QCoreApplication constructor + // already created a QLocale at this stage so we need to set the reset it + // by triggering the creation and destruction of a QSystemLocale + // this is highly dependant on Qt internals, so may break, but oh well + QSystemLocale *dummy = new QSystemLocale(); + delete dummy; } } -Q_COREAPP_STARTUP_FUNCTION(initializeLanguages) - -namespace KDEPrivate -{ - struct LanguageRowData { LanguageRowData() {