diff options
author | Martin Gräßlin <mgraesslin@kde.org> | 2014-06-05 14:05:44 +0200 |
---|---|---|
committer | Thomas Braxton <kde.braxton@gmail.com> | 2014-06-12 17:48:21 -0500 |
commit | 69c203156d21385a8c263096cb35600e85a5c604 (patch) | |
tree | 38a69b65bea0a85ef43df011194ce4e0742aec55 | |
parent | ac6703215ba5e152379173ba503f5ba3bd7e8a85 (diff) | |
download | kconfig-69c203156d21385a8c263096cb35600e85a5c604.tar.gz kconfig-69c203156d21385a8c263096cb35600e85a5c604.tar.bz2 |
Fix locale-aware reading in KDesktopFile
The underlying KConfig used QLocale::name() for getting the locale
aware part. But this returns "de_DE" while the desktop files store
"de".
In addition it constructs a QLocale object instead of using the
system locale. This has the advantage that the usage of
QLocale::setDafault() gets honored by KConfig.
REVIEW: 118564
-rw-r--r-- | autotests/helper.h | 43 | ||||
-rw-r--r-- | autotests/kconfigtest.cpp | 3 | ||||
-rw-r--r-- | autotests/kdesktopfiletest.cpp | 30 | ||||
-rw-r--r-- | autotests/kdesktopfiletest.h | 1 | ||||
-rw-r--r-- | src/core/kconfig.cpp | 2 |
5 files changed, 78 insertions, 1 deletions
diff --git a/autotests/helper.h b/autotests/helper.h new file mode 100644 index 00000000..83f1a8bc --- /dev/null +++ b/autotests/helper.h @@ -0,0 +1,43 @@ +/* + * Copyright 2014 Martin Gräßlin <mgraesslin@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. +*/ +#ifndef KCONFIG_AUTOTESTS_HELPER_H +#define KCONFIG_AUTOTESTS_HELPER_H + +#include <QLocale> + +/** + * @brief Helper to store the current default QLocale and resetting to it in the dtor. + **/ +class DefaultLocale +{ +public: + DefaultLocale() + : m_defaultLocale() + { + } + ~DefaultLocale() + { + QLocale::setDefault(m_defaultLocale); + } +private: + QLocale m_defaultLocale; +}; + +#endif diff --git a/autotests/kconfigtest.cpp b/autotests/kconfigtest.cpp index 2b6de0d7..a8482b70 100644 --- a/autotests/kconfigtest.cpp +++ b/autotests/kconfigtest.cpp @@ -21,6 +21,7 @@ #undef QT_NO_CAST_FROM_BYTEARRAY #include "kconfigtest.h" +#include "helper.h" #include <QtTest/QtTest> #include <qtemporarydir.h> @@ -927,6 +928,8 @@ void KConfigTest::testCascadingWithLocale() void KConfigTest::testMerge() { + DefaultLocale defaultLocale; + QLocale::setDefault(QLocale::c()); KConfig config(TEST_SUBDIR "mergetest", KConfig::SimpleConfig); KConfigGroup cg = config.group("some group"); diff --git a/autotests/kdesktopfiletest.cpp b/autotests/kdesktopfiletest.cpp index 494a43c0..6c3af4c2 100644 --- a/autotests/kdesktopfiletest.cpp +++ b/autotests/kdesktopfiletest.cpp @@ -16,6 +16,7 @@ * Boston, MA 02110-1301, USA. */ #include "kdesktopfiletest.h" +#include "helper.h" #include <kconfiggroup.h> #include <qtemporaryfile.h> @@ -49,6 +50,35 @@ void KDesktopFileTest::testRead() QCOMPARE(df.fileName(), QFileInfo(fileName).canonicalFilePath()); } +void KDesktopFileTest::testReadLocalized() +{ + QTemporaryFile file("testReadLocalizedXXXXXX.desktop"); + QVERIFY(file.open()); + const QString fileName = file.fileName(); + QTextStream ts(&file); + ts << + "[Desktop Entry]\n" + "Type=Application\n" + "Name=My Application\n" + "Name[de]=Meine Anwendung\n" + "Icon=foo\n" + "\n"; + file.close(); + QVERIFY(QFile::exists(fileName)); + QVERIFY(KDesktopFile::isDesktopFile(fileName)); + + DefaultLocale defaultLocale; + // set language to German for which we have a translation + QLocale::setDefault(QLocale(QLocale::German)); + KDesktopFile df(fileName); + QCOMPARE(df.readName(), QString::fromLatin1("Meine Anwendung")); + + // set language to French for which we don't have a translation + QLocale::setDefault(QLocale(QLocale::French)); + KDesktopFile df2(fileName); + QCOMPARE(df2.readName(), QString::fromLatin1("My Application")); +} + void KDesktopFileTest::testSuccessfulTryExec() { QTemporaryFile file; diff --git a/autotests/kdesktopfiletest.h b/autotests/kdesktopfiletest.h index d57351fd..f854d6cc 100644 --- a/autotests/kdesktopfiletest.h +++ b/autotests/kdesktopfiletest.h @@ -26,6 +26,7 @@ class KDesktopFileTest : public QObject Q_OBJECT private Q_SLOTS: void testRead(); + void testReadLocalized(); void testUnsuccessfulTryExec(); void testSuccessfulTryExec(); void testActionGroup(); diff --git a/src/core/kconfig.cpp b/src/core/kconfig.cpp index 89e92333..a2598f8e 100644 --- a/src/core/kconfig.cpp +++ b/src/core/kconfig.cpp @@ -95,7 +95,7 @@ KConfigPrivate::KConfigPrivate(KConfig::OpenFlags flags, // mappingsRegistered = true; // } - setLocale(QLocale::system().name()); + setLocale(QLocale().name().split(QStringLiteral("_")).at(0)); } bool KConfigPrivate::lockLocal() |