KConfigWidgets

klanguagename.cpp
1/*
2 SPDX-FileCopyrightText: 1999-2003 Hans Petter Bieker <bieker@kde.org>
3 SPDX-FileCopyrightText: 2007 David Jarvie <software@astrojar.org.uk>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "klanguagename.h"
9
10#include <KConfig>
11#include <KConfigGroup>
12
13#include <QDir>
14#include <QLocale>
15
16QString KLanguageName::nameForCode(const QString &code)
17{
18 const QStringList parts = QLocale().name().split(QLatin1Char('_'));
19 return nameForCodeInLocale(code, parts.at(0));
20}
21
22static std::tuple<QString, QString> namesFromEntryFile(const QString &realCode, const QString &realOutputCode)
23{
25 QStringLiteral("locale") + QLatin1Char('/') + realCode + QStringLiteral("/kf6_entry.desktop"));
26
27 if (!entryFile.isEmpty()) {
28 KConfig entry(entryFile, KConfig::SimpleConfig);
29 entry.setLocale(realOutputCode);
30 const KConfigGroup group(&entry, QStringLiteral("KCM Locale"));
31 const QString name = group.readEntry("Name");
32
33 entry.setLocale(QStringLiteral("en_US"));
34 const QString englishName = group.readEntry("Name");
35 return std::make_tuple(name, englishName);
36 }
37 return {};
38}
39
40QString KLanguageName::nameForCodeInLocale(const QString &code, const QString &outputCode)
41{
42 const QString realCode = code == QLatin1String("en") ? QStringLiteral("en_US") : code;
43 const QString realOutputCode = outputCode == QLatin1String("en") ? QStringLiteral("en_US") : outputCode;
44
45 const std::tuple<QString, QString> nameAndEnglishName = namesFromEntryFile(realCode, realOutputCode);
46 const QString name = std::get<0>(nameAndEnglishName);
47 const QString englishName = std::get<1>(nameAndEnglishName);
48
49 if (!name.isEmpty()) {
50 // KConfig doesn't have a way to say it didn't find the entry in
51 // realOutputCode. When it doesn't find it in the locale you ask for, it just returns the english version
52 // so we compare the returned name against the english version, if they are different we return it, if they
53 // are equal we defer to QLocale (with a final fallback to name/englishName if QLocale doesn't know about it)
54 if (name != englishName || realOutputCode == QLatin1String("en_US")) {
55 return name;
56 }
57 }
58
59 const QLocale locale(realCode);
60 if (locale != QLocale::c()) {
61 if (realCode == realOutputCode) {
62 return locale.nativeLanguageName();
63 }
64 return QLocale::languageToString(locale.language());
65 }
66
67 // We get here if QLocale doesn't know about realCode (at the time of writing this happens for crh, csb, hne, mai) and name and englishName are the same.
68 // So what we do here is return name, which can be either empty if the KDE side doesn't know about the code, or otherwise will be the name/englishName
69 return name;
70}
71
72QStringList KLanguageName::allLanguageCodes()
73{
74 QStringList systemLangList;
76 for (const QString &localeDir : localeDirs) {
77 const QStringList entries = QDir(localeDir).entryList(QDir::Dirs);
78 auto languageExists = [&localeDir](const QString &language) {
79 return QFile::exists(localeDir + QLatin1Char('/') + language + QLatin1String("/kf6_entry.desktop"));
80 };
81 std::copy_if(entries.begin(), entries.end(), std::back_inserter(systemLangList), languageExists);
82 }
83 if (localeDirs.count() > 1) {
84 systemLangList.removeDuplicates();
85 }
86 return systemLangList;
87}
QString name(StandardAction id)
QStringList entryList(Filters filters, SortFlags sort) const const
bool exists() const const
const_reference at(qsizetype i) const const
iterator begin()
iterator end()
QLocale c()
QString languageToString(Language language)
QString name() const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
qsizetype removeDuplicates()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.