[libcalamaresui] Refactor: move LocaleLabel to UI library
- This is prep-work for making locale labels consistent everywhere. - While here, improve code documentation.
This commit is contained in:
parent
311af6de5d
commit
084f4d2445
@ -266,5 +266,42 @@ clearLayout( QLayout* layout )
|
||||
}
|
||||
}
|
||||
|
||||
LocaleLabel::LocaleLabel( const QString& locale )
|
||||
: m_locale( LocaleLabel::getLocale( locale ) )
|
||||
, m_localeId( locale )
|
||||
{
|
||||
QString sortKey = QLocale::languageToString( m_locale.language() );
|
||||
QString label = m_locale.nativeLanguageName();
|
||||
|
||||
if ( label.isEmpty() )
|
||||
label = QString( QLatin1Literal( "* %1 (%2)" ) ).arg( locale, sortKey );
|
||||
|
||||
if ( locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 2 )
|
||||
{
|
||||
QLatin1Literal countrySuffix( " (%1)" );
|
||||
|
||||
sortKey.append( QString( countrySuffix ).arg( QLocale::countryToString( m_locale.country() ) ) );
|
||||
|
||||
// If the language name is RTL, make this parenthetical addition RTL as well.
|
||||
QString countryFormat = label.isRightToLeft() ? QString( QChar( 0x202B ) ) : QString();
|
||||
countryFormat.append( countrySuffix );
|
||||
label.append( countryFormat.arg( m_locale.nativeCountryName() ) );
|
||||
}
|
||||
|
||||
m_sortKey = sortKey;
|
||||
m_label = label;
|
||||
}
|
||||
|
||||
QLocale LocaleLabel::getLocale( const QString& localeName )
|
||||
{
|
||||
if ( localeName.contains( "@latin" ) )
|
||||
{
|
||||
QLocale loc( localeName ); // Ignores @latin
|
||||
return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() );
|
||||
}
|
||||
else
|
||||
return QLocale( localeName );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "utils/CalamaresUtils.h"
|
||||
#include "UiDllMacro.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QPixmap>
|
||||
#include <QSize>
|
||||
|
||||
@ -125,6 +126,68 @@ constexpr int windowMinimumWidth = 800;
|
||||
constexpr int windowMinimumHeight = 520;
|
||||
constexpr int windowPreferredWidth = 1024;
|
||||
constexpr int windowPreferredHeight = 520;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
class LocaleLabel : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/** @brief Construct from a locale name.
|
||||
*
|
||||
* The locale name should be one that Qt recognizes, e.g. en_US or ar_EY.
|
||||
*/
|
||||
LocaleLabel( const QString& localeName );
|
||||
|
||||
/** @brief Define a sorting order.
|
||||
*
|
||||
* English (@see isEnglish() -- it means en_US) is sorted at the top.
|
||||
*/
|
||||
bool operator <(const LocaleLabel& other) const
|
||||
{
|
||||
if ( isEnglish() )
|
||||
return !other.isEnglish();
|
||||
if ( other.isEnglish() )
|
||||
return false;
|
||||
return m_sortKey < other.m_sortKey;
|
||||
}
|
||||
|
||||
/** @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.
|
||||
*/
|
||||
bool isEnglish() const
|
||||
{
|
||||
return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" );
|
||||
}
|
||||
|
||||
/** @brief Get the human-readable name for this locale. */
|
||||
QString label() const { return m_label; }
|
||||
/** @brief Get the Qt locale. */
|
||||
QLocale locale() const { return m_locale; }
|
||||
|
||||
/** @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_sortKey; // the English name of the locale
|
||||
QString m_label; // the native name of the locale
|
||||
} ;
|
||||
|
||||
|
||||
} // namespace CalamaresUtils
|
||||
|
||||
#endif // CALAMARESUTILSGUI_H
|
||||
|
@ -132,74 +132,6 @@ bool matchLocale( QComboBox& list, QLocale& matchFound, std::function<bool(const
|
||||
return false;
|
||||
}
|
||||
|
||||
struct LocaleLabel
|
||||
{
|
||||
LocaleLabel( const QString& locale )
|
||||
: m_locale( LocaleLabel::getLocale( locale ) )
|
||||
, m_localeId( locale )
|
||||
{
|
||||
QString sortKey = QLocale::languageToString( m_locale.language() );
|
||||
QString label = m_locale.nativeLanguageName();
|
||||
|
||||
if ( label.isEmpty() )
|
||||
label = QString( QLatin1Literal( "* %1 (%2)" ) ).arg( locale, sortKey );
|
||||
|
||||
if ( locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 2 )
|
||||
{
|
||||
QLatin1Literal countrySuffix( " (%1)" );
|
||||
|
||||
sortKey.append( QString( countrySuffix ).arg( QLocale::countryToString( m_locale.country() ) ) );
|
||||
|
||||
// If the language name is RTL, make this parenthetical addition RTL as well.
|
||||
QString countryFormat = label.isRightToLeft() ? QString( QChar( 0x202B ) ) : QString();
|
||||
countryFormat.append( countrySuffix );
|
||||
label.append( countryFormat.arg( m_locale.nativeCountryName() ) );
|
||||
}
|
||||
|
||||
m_sortKey = sortKey;
|
||||
m_label = label;
|
||||
}
|
||||
|
||||
QLocale m_locale;
|
||||
QString m_localeId; // the locale identifier, e.g. "en_GB"
|
||||
QString m_sortKey; // the English name of the locale
|
||||
QString m_label; // the native name of the locale
|
||||
|
||||
/** @brief Define a sorting order.
|
||||
*
|
||||
* English (@see isEnglish() -- it means en_US) is sorted at the top.
|
||||
*/
|
||||
bool operator <(const LocaleLabel& other) const
|
||||
{
|
||||
if ( isEnglish() )
|
||||
return !other.isEnglish();
|
||||
if ( other.isEnglish() )
|
||||
return false;
|
||||
return m_sortKey < other.m_sortKey;
|
||||
}
|
||||
|
||||
/** @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.
|
||||
*/
|
||||
bool isEnglish() const
|
||||
{
|
||||
return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" );
|
||||
}
|
||||
|
||||
static QLocale getLocale( const QString& localeName )
|
||||
{
|
||||
if ( localeName.contains( "@latin" ) )
|
||||
{
|
||||
QLocale loc( localeName );
|
||||
return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() );
|
||||
}
|
||||
else
|
||||
return QLocale( localeName );
|
||||
}
|
||||
} ;
|
||||
|
||||
void
|
||||
WelcomePage::initLanguages()
|
||||
{
|
||||
@ -208,7 +140,7 @@ WelcomePage::initLanguages()
|
||||
ui->languageWidget->setInsertPolicy( QComboBox::InsertAtBottom );
|
||||
|
||||
{
|
||||
std::list< LocaleLabel > localeList;
|
||||
std::list< CalamaresUtils::LocaleLabel > localeList;
|
||||
const auto locales = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';');
|
||||
for ( const QString& locale : locales )
|
||||
{
|
||||
@ -219,7 +151,7 @@ WelcomePage::initLanguages()
|
||||
|
||||
for ( const auto& locale : localeList )
|
||||
{
|
||||
ui->languageWidget->addItem( locale.m_label, locale.m_locale );
|
||||
ui->languageWidget->addItem( locale.label(), locale.locale() );
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user