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
|
|
|
#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 )
|
|
|
|
: QObject( parent )
|
|
|
|
, m_locale( QLocale() )
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
|
|
|
m_localeId = m_locale.name();
|
2019-04-19 13:52:19 +02:00
|
|
|
|
2019-04-19 10:04:49 +02:00
|
|
|
setLabels( QString(), LabelFormat::IfNeededWithCountry );
|
|
|
|
}
|
|
|
|
|
2019-12-12 16:41:37 +01:00
|
|
|
Label::Label( const QString& locale, LabelFormat format, QObject* parent )
|
2019-05-10 17:46:20 +02:00
|
|
|
: m_locale( Label::getLocale( locale ) )
|
2019-04-19 10:04:49 +02:00
|
|
|
, m_localeId( locale )
|
2019-12-12 16:41:37 +01:00
|
|
|
, QObject( parent )
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
|
|
|
setLabels( locale, format );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-10 17:46:20 +02:00
|
|
|
Label::setLabels( const QString& locale, LabelFormat format )
|
2019-04-19 10:04:49 +02:00
|
|
|
{
|
2019-12-12 16:41:37 +01:00
|
|
|
emit localeIdChanged(m_localeId);
|
2019-04-19 10:04:49 +02:00
|
|
|
//: language[name] (country[name])
|
|
|
|
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-12-12 16:41:37 +01:00
|
|
|
emit labelChanged(m_label);
|
2019-08-04 22:17:12 +02:00
|
|
|
m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) )
|
|
|
|
: englishName;
|
2019-12-12 16:41:37 +01:00
|
|
|
emit englishLabelChanged(m_englishLabel);
|
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
|
|
|
{
|
|
|
|
if ( localeName.contains( "@latin" ) )
|
|
|
|
{
|
|
|
|
QLocale loc( localeName ); // Ignores @latin
|
|
|
|
return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() );
|
|
|
|
}
|
|
|
|
else
|
2019-08-04 22:17:12 +02:00
|
|
|
{
|
2019-04-19 10:04:49 +02:00
|
|
|
return QLocale( localeName );
|
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
|