2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2020-08-10 09:43:13 +02:00
|
|
|
*
|
2020-05-30 16:15:03 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
|
|
|
|
* SPDX-FileCopyrightText: 2017-2019 Adriaan de Groot <groot@kde.org>
|
2020-08-10 09:43:13 +02:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-04-19 10:04:49 +02:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2019-04-19 10:04:49 +02:00
|
|
|
*
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
2019-04-19 10:04:49 +02:00
|
|
|
*/
|
|
|
|
|
2019-05-10 17:46:20 +02:00
|
|
|
#include "Label.h"
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2019-05-13 12:23:41 +02:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
namespace Locale
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
|
|
|
|
2019-12-12 16:41:37 +01:00
|
|
|
Label::Label( QObject* parent )
|
2020-02-11 16:39:39 +01:00
|
|
|
: Label( QString(), LabelFormat::IfNeededWithCountry, parent )
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-12-12 16:41:37 +01:00
|
|
|
Label::Label( const QString& locale, LabelFormat format, QObject* parent )
|
2020-02-11 15:36:30 +01:00
|
|
|
: QObject( parent )
|
|
|
|
, m_locale( Label::getLocale( locale ) )
|
2020-02-11 16:39:39 +01:00
|
|
|
, m_localeId( locale.isEmpty() ? m_locale.name() : locale )
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
|
|
|
QString longFormat = QObject::tr( "%1 (%2)" );
|
|
|
|
|
|
|
|
QString languageName = m_locale.nativeLanguageName();
|
|
|
|
QString englishName = m_locale.languageToString( m_locale.language() );
|
|
|
|
QString countryName;
|
|
|
|
|
|
|
|
if ( languageName.isEmpty() )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-04-19 10:04:49 +02:00
|
|
|
languageName = QString( "* %1 (%2)" ).arg( locale, englishName );
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-04-19 10:04:49 +02:00
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry )
|
|
|
|
|| ( locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
|
2019-04-19 10:04:49 +02:00
|
|
|
|
|
|
|
if ( needsCountryName )
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-04-19 10:04:49 +02:00
|
|
|
countryName = m_locale.nativeCountryName();
|
2019-08-04 22:17:12 +02:00
|
|
|
}
|
2019-04-19 13:52:19 +02:00
|
|
|
m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName;
|
2019-08-04 22:17:12 +02:00
|
|
|
m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) )
|
|
|
|
: englishName;
|
2019-04-19 10:04:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
QLocale
|
|
|
|
Label::getLocale( const QString& localeName )
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
2020-02-11 16:37:49 +01:00
|
|
|
if ( localeName.isEmpty() )
|
|
|
|
{
|
|
|
|
return QLocale();
|
|
|
|
}
|
2021-07-25 23:11:46 +02:00
|
|
|
|
|
|
|
// Special cases
|
|
|
|
if ( localeName == QStringLiteral( "sr@latin" ) )
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
|
|
|
QLocale loc( localeName ); // Ignores @latin
|
|
|
|
return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() );
|
|
|
|
}
|
2021-07-25 23:11:46 +02:00
|
|
|
|
|
|
|
return QLocale( localeName );
|
2019-04-19 10:04:49 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
} // namespace Locale
|
|
|
|
} // namespace CalamaresUtils
|