diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ kcmodule.cpp khelpclient.cpp klanguagebutton.cpp + klanguagename.cpp kpastetextaction.cpp krecentfilesaction.cpp kstandardaction.cpp @@ -52,6 +53,7 @@ KCModule KHelpClient KLanguageButton + KLanguageName KPasteTextAction KRecentFilesAction KViewStateMaintainer diff --git a/src/klanguagename.h b/src/klanguagename.h new file mode 100644 --- /dev/null +++ b/src/klanguagename.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 1999-2003 Hans Petter Bieker + * (c) 2001 Martijn Klingens + * (c) 2007 David Jarvie + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef KLANGUAGENAME_H +#define KLANGUAGENAME_H + +#include "kconfigwidgets_export.h" + +class QString; + +/** + * @class KLanguageName klanguagename.h KLanguageName + * + * KLanguageName is a helper namespace that returns the name of a given language code. + * + * @since 5.44 + * + */ +namespace KLanguageName +{ + /** + * Returns the name of the given language code in the current locale. + * + * If it can't be found in the current locale it returns the name in English. + * + * It it can't be found in English either returns an empty QString. + * + * @param code code (ISO 639-1) of the language whose name is wanted. + */ + KCONFIGWIDGETS_EXPORT QString nameForCode(const QString &code); + + /** + * Returns the name of the given language code in the other given locale code. + * + * If it can't be found in the given locale it returns the name in English. + * + * It it can't be found in English either returns an empty QString. + * + * @param code code (ISO 639-1) of the language whose name is wanted. + * @param outputLocale code (ISO 639-1) of the language in which we want the name in. + */ + KCONFIGWIDGETS_EXPORT QString nameForCodeInLocale(const QString &code, const QString &outputLocale); +} + +#endif diff --git a/src/klanguagename.cpp b/src/klanguagename.cpp new file mode 100644 --- /dev/null +++ b/src/klanguagename.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 1999-2003 Hans Petter Bieker + * (c) 2007 David Jarvie + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "klanguagename.h" +#include "kconfigwidgets_debug.h" + +#include +#include + +#include + +QString KLanguageName::nameForCode(const QString &code) +{ + const QString realCode = code == QStringLiteral("en") ? QStringLiteral("en_US") : code; + + const QString entryFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("locale") + QLatin1Char('/') + realCode + QStringLiteral("/kf5_entry.desktop")); + if (!entryFile.isEmpty()) { + KConfig entry(entryFile, KConfig::SimpleConfig); + const KConfigGroup group(&entry, "KCM Locale"); + return group.readEntry("Name"); + } + + const QLocale locale(realCode); + if (locale != QLocale::c()) { + return QLocale::languageToString(locale.language()); + } + + return QString(); +} + +QString KLanguageName::nameForCodeInLocale(const QString &code, const QString &outputLocale) +{ + const QString realCode = code == QStringLiteral("en") ? QStringLiteral("en_US") : code; + const QString realOutputLocale = outputLocale == QStringLiteral("en") ? QStringLiteral("en_US") : outputLocale; + + const QString entryFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("locale") + QLatin1Char('/') + realCode + QStringLiteral("/kf5_entry.desktop")); + if (!entryFile.isEmpty()) { + KConfig entry(entryFile, KConfig::SimpleConfig); + entry.setLocale(realOutputLocale); + const KConfigGroup group(&entry, "KCM Locale"); + const QString name = group.readEntry("Name"); + + if (realCode == realOutputLocale) { + // KConfig doesn't have a way to say it didn't find the entry in realOutputLocale and returned the english version + // so we check for the english version if it's equal to the "non-english" version and QLocale::nativeLanguageName() is different + // we give the QLocale version + entry.setLocale("en"); + const QString englisName = group.readEntry("Name"); + if (name != englisName) { + return name; + } else { + const QLocale locale(realCode); + return locale.nativeLanguageName(); + } + } + } + + const QLocale locale(realCode); + if (locale != QLocale::c()) { + QString name; + if (realCode == realOutputLocale) { + name = locale.nativeLanguageName(); + } + return name.isEmpty() ? QLocale::languageToString(locale.language()) : name; + } + + return QString(); +}