Changeset View
Changeset View
Standalone View
Standalone View
src/klanguagename.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org> | ||||
3 | * (c) 2007 David Jarvie <software@astrojar.org.uk> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Library General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "klanguagename.h" | ||||
22 | #include "kconfigwidgets_debug.h" | ||||
23 | | ||||
24 | #include <kconfig.h> | ||||
25 | #include <kconfiggroup.h> | ||||
26 | | ||||
27 | #include <QFile> | ||||
28 | | ||||
29 | QString KLanguageName::nameForCode(const QString &code) | ||||
30 | { | ||||
31 | const QString realCode = code == QStringLiteral("en") ? QStringLiteral("en_US") : code; | ||||
aacid: should the second param be code too?
I mean if we read the docs we say both are code (ISO 639… | |||||
Do you mean both being a code or both being the code? a codeparts.at(0) is the code of the current locale. The only way I found to get the 639 code from qlocale is by splitting name http://doc.qt.io/qt-5/qlocale.html#name Perhaps a comment is needed to explain this? the codeBoth params being the variable code wouldn't be right I think. The documentation for nameForCode says it will ideally give you the localized name of code in the system language. LANGUAGE=en nameForCode('en') => 'English' LANGUAGE=fr nameForCode('en') => 'anglaise' which internally is nameForCodeInLocale('en', 'en') nameForCodeInLocale('en', 'fr') sitter: Do you mean both being **a** code or both being **the** code?
# a code
`parts.at(0)` is the… | |||||
aacid: Argggg, ignore me i was reading the code wrong. Sorry for the noise | |||||
32 | | ||||
33 | const QString entryFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("locale") + QLatin1Char('/') + realCode + QStringLiteral("/kf5_entry.desktop")); | ||||
34 | if (!entryFile.isEmpty()) { | ||||
35 | KConfig entry(entryFile, KConfig::SimpleConfig); | ||||
36 | const KConfigGroup group(&entry, "KCM Locale"); | ||||
37 | return group.readEntry("Name"); | ||||
38 | } | ||||
39 | | ||||
40 | const QLocale locale(realCode); | ||||
41 | if (locale != QLocale::c()) { | ||||
42 | return QLocale::languageToString(locale.language()); | ||||
43 | } | ||||
44 | | ||||
45 | return QString(); | ||||
apol: shouldn't this be equivalent to nameForCodeInLocale(code, QLocale().code())? | |||||
46 | } | ||||
47 | | ||||
48 | QString KLanguageName::nameForCodeInLocale(const QString &code, const QString &outputLocale) | ||||
49 | { | ||||
50 | const QString realCode = code == QStringLiteral("en") ? QStringLiteral("en_US") : code; | ||||
51 | const QString realOutputLocale = outputLocale == QStringLiteral("en") ? QStringLiteral("en_US") : outputLocale; | ||||
52 | | ||||
53 | const QString entryFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("locale") + QLatin1Char('/') + realCode + QStringLiteral("/kf5_entry.desktop")); | ||||
54 | if (!entryFile.isEmpty()) { | ||||
55 | KConfig entry(entryFile, KConfig::SimpleConfig); | ||||
56 | entry.setLocale(realOutputLocale); | ||||
57 | const KConfigGroup group(&entry, "KCM Locale"); | ||||
58 | const QString name = group.readEntry("Name"); | ||||
59 | | ||||
60 | if (realCode == realOutputLocale) { | ||||
61 | // KConfig doesn't have a way to say it didn't find the entry in realOutputLocale and returned the english version | ||||
62 | // so we check for the english version if it's equal to the "non-english" version and QLocale::nativeLanguageName() is different | ||||
63 | // we give the QLocale version | ||||
64 | entry.setLocale("en"); | ||||
65 | const QString englisName = group.readEntry("Name"); | ||||
apol: englishName | |||||
66 | if (name != englisName) { | ||||
67 | return name; | ||||
68 | } else { | ||||
69 | const QLocale locale(realCode); | ||||
70 | return locale.nativeLanguageName(); | ||||
71 | } | ||||
72 | } | ||||
73 | } | ||||
74 | | ||||
75 | const QLocale locale(realCode); | ||||
76 | if (locale != QLocale::c()) { | ||||
77 | QString name; | ||||
78 | if (realCode == realOutputLocale) { | ||||
79 | name = locale.nativeLanguageName(); | ||||
80 | } | ||||
81 | return name.isEmpty() ? QLocale::languageToString(locale.language()) : name; | ||||
82 | } | ||||
83 | | ||||
84 | return QString(); | ||||
85 | } |
should the second param be code too?
I mean if we read the docs we say both are code (ISO 639-1), so both should be the same and not only the first part?