2019-04-19 10:04:49 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
2019-05-10 17:46:20 +02:00
|
|
|
* Copyright 2017-2019, Adriaan de Groot <groot@kde.org>
|
2019-04-19 10:04:49 +02:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-05-10 17:46:20 +02:00
|
|
|
#ifndef LOCALE_LABEL_H
|
|
|
|
#define LOCALE_LABEL_H
|
2019-04-19 10:04:49 +02:00
|
|
|
|
|
|
|
#include <QLocale>
|
2019-12-12 16:41:37 +01:00
|
|
|
#include <QObject>
|
2020-02-11 15:25:28 +01:00
|
|
|
#include <QString>
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
namespace CalamaresUtils
|
2019-05-13 12:23:41 +02:00
|
|
|
{
|
|
|
|
namespace Locale
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Consistent locale (language + country) naming.
|
|
|
|
*
|
|
|
|
* Support class to turn locale names (as used by Calamares's
|
|
|
|
* translation system) into QLocales, and also into consistent
|
|
|
|
* human-readable text labels.
|
|
|
|
*/
|
2019-12-12 16:41:37 +01:00
|
|
|
class Label : public QObject
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
2019-12-12 16:41:37 +01:00
|
|
|
Q_OBJECT
|
|
|
|
|
2019-04-19 10:04:49 +02:00
|
|
|
public:
|
|
|
|
/** @brief Formatting option for label -- add (country) to label. */
|
2019-08-04 22:17:12 +02:00
|
|
|
enum class LabelFormat
|
|
|
|
{
|
|
|
|
AlwaysWithCountry,
|
|
|
|
IfNeededWithCountry
|
|
|
|
};
|
2019-04-19 10:04:49 +02:00
|
|
|
|
|
|
|
/** @brief Empty locale. This uses the system-default locale. */
|
2020-02-11 15:25:28 +01:00
|
|
|
Label( QObject* parent = nullptr );
|
2019-04-29 12:12:18 +02:00
|
|
|
|
2019-04-19 10:04:49 +02:00
|
|
|
/** @brief Construct from a locale name.
|
|
|
|
*
|
|
|
|
* The @p localeName should be one that Qt recognizes, e.g. en_US or ar_EY.
|
|
|
|
* The @p format determines whether the country name is always present
|
|
|
|
* in the label (human-readable form) or only if needed for disambiguation.
|
|
|
|
*/
|
2020-02-11 15:25:28 +01:00
|
|
|
Label( const QString& localeName,
|
|
|
|
LabelFormat format = LabelFormat::IfNeededWithCountry,
|
|
|
|
QObject* parent = nullptr );
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2020-03-24 16:04:14 +01:00
|
|
|
|
2019-04-19 10:04:49 +02:00
|
|
|
/** @brief Define a sorting order.
|
|
|
|
*
|
2019-08-05 23:32:13 +02:00
|
|
|
* Locales are sorted by their id, which means the ISO 2-letter code + country.
|
2019-04-19 10:04:49 +02:00
|
|
|
*/
|
2019-08-04 22:17:12 +02:00
|
|
|
bool operator<( const Label& other ) const { return m_localeId < other.m_localeId; }
|
2019-04-19 10:04:49 +02:00
|
|
|
|
|
|
|
/** @brief Is this locale English?
|
|
|
|
*
|
|
|
|
* en_US and en (American English) is defined as English. The Queen's
|
|
|
|
* English -- proper English -- is relegated to non-English status.
|
|
|
|
*/
|
2019-09-08 22:20:13 +02:00
|
|
|
bool isEnglish() const { return m_localeId == QLatin1String( "en_US" ) || m_localeId == QLatin1String( "en" ); }
|
2019-04-19 10:04:49 +02:00
|
|
|
|
|
|
|
/** @brief Get the human-readable name for this locale. */
|
2019-08-04 22:17:12 +02:00
|
|
|
QString label() const { return m_label; }
|
2019-04-19 10:04:49 +02:00
|
|
|
/** @brief Get the *English* human-readable name for this locale. */
|
2019-08-04 22:17:12 +02:00
|
|
|
QString englishLabel() const { return m_englishLabel; }
|
2019-04-19 10:04:49 +02:00
|
|
|
|
|
|
|
/** @brief Get the Qt locale. */
|
2019-08-04 22:17:12 +02:00
|
|
|
QLocale locale() const { return m_locale; }
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
QString name() const { return m_locale.name(); }
|
2019-08-05 23:32:13 +02:00
|
|
|
QString id() const { return m_localeId; }
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2019-05-10 20:44:54 +02:00
|
|
|
/// @brief Convenience accessor to the language part of the locale
|
2019-08-04 22:17:12 +02:00
|
|
|
QLocale::Language language() const { return m_locale.language(); }
|
2019-05-10 20:44:54 +02:00
|
|
|
|
|
|
|
/// @brief Convenience accessor to the country part (if any) of the locale
|
2019-08-04 22:17:12 +02:00
|
|
|
QLocale::Country country() const { return m_locale.country(); }
|
2019-05-10 20:44:54 +02:00
|
|
|
|
2019-04-19 10:04:49 +02:00
|
|
|
/** @brief Get a Qt locale for the given @p localeName
|
|
|
|
*
|
|
|
|
* This special-cases `sr@latin`, which is used as a translation
|
|
|
|
* name in Calamares, while Qt recognizes `sr@latn`.
|
|
|
|
*/
|
|
|
|
static QLocale getLocale( const QString& localeName );
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QLocale m_locale;
|
|
|
|
QString m_localeId; // the locale identifier, e.g. "en_GB"
|
|
|
|
QString m_label; // the native name of the locale
|
|
|
|
QString m_englishLabel;
|
2019-08-04 22:17:12 +02:00
|
|
|
};
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
} // namespace Locale
|
|
|
|
} // namespace CalamaresUtils
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2019-04-29 12:12:18 +02:00
|
|
|
#endif
|