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 | QString KLanguageName::nameForCode(const QString &code) | ||||
28 | { | ||||
29 | const QStringList parts = QLocale().name().split(QChar('_')); | ||||
30 | return nameForCodeInLocale(code, parts.at(0)); | ||||
31 | } | ||||
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 | QString KLanguageName::nameForCodeInLocale(const QString &code, const QString &outputCode) | ||||
34 | { | ||||
35 | const QString realCode = code == QStringLiteral("en") ? QStringLiteral("en_US") : code; | ||||
36 | const QString realOutputCode = outputCode == QStringLiteral("en") ? QStringLiteral("en_US") : outputCode; | ||||
37 | | ||||
38 | const QString entryFile = | ||||
39 | QStandardPaths::locate(QStandardPaths::GenericDataLocation, | ||||
40 | QStringLiteral("locale") + QLatin1Char('/') + realCode + QStringLiteral("/kf5_entry.desktop")); | ||||
41 | if (!entryFile.isEmpty()) { | ||||
42 | KConfig entry(entryFile, KConfig::SimpleConfig); | ||||
43 | entry.setLocale(realOutputCode); | ||||
44 | const KConfigGroup group(&entry, "KCM Locale"); | ||||
45 | const QString name = group.readEntry("Name"); | ||||
46 | | ||||
apol: shouldn't this be equivalent to nameForCodeInLocale(code, QLocale().code())? | |||||
47 | // KConfig doesn't have a way to say it didn't find the entry in | ||||
48 | // realOutputCode and returned the english version so we check for the | ||||
49 | // english version, if it's equal to the "non-english" version we'll use | ||||
50 | // otherwise we defer to QLocale. | ||||
51 | entry.setLocale("en_US"); | ||||
52 | const QString englishName = group.readEntry("Name"); | ||||
53 | if (name != englishName || realOutputCode == QStringLiteral("en_US")) { | ||||
54 | return name; | ||||
55 | } | ||||
56 | } | ||||
57 | | ||||
58 | const QLocale locale(realCode); | ||||
59 | if (locale != QLocale::c()) { | ||||
60 | if (realCode == realOutputCode) { | ||||
61 | return locale.nativeLanguageName(); | ||||
62 | } | ||||
63 | return QLocale::languageToString(locale.language()); | ||||
64 | } | ||||
65 | | ||||
66 | return QString(); | ||||
apol: englishName | |||||
67 | } |
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?