Merge branch 'fix-language-display'

Merge in the bits of @apachelogger's fixes that I forgot previously,
and then refactor the whole thing to be consistent more easily.

FIXES: #712
This commit is contained in:
Adriaan de Groot 2018-12-14 13:51:08 +01:00
commit c9930788f7
6 changed files with 126 additions and 97 deletions

View File

@ -172,10 +172,6 @@ createRoundedImage( const QPixmap& pixmap, const QSize& size, float frameWidthPc
painter.setPen( pen );
painter.drawRoundedRect( outerRect, qreal( frameWidthPct ) * 100.0, qreal( frameWidthPct ) * 100.0, Qt::RelativeSize );
/* painter.setBrush( Qt::transparent );
painter.setPen( Qt::white );
painter.drawRoundedRect( outerRect, frameWidthPct, frameWidthPct, Qt::RelativeSize ); */
return frame;
}
@ -266,5 +262,44 @@ clearLayout( QLayout* layout )
}
}
LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format )
: m_locale( LocaleLabel::getLocale( locale ) )
, m_localeId( locale )
{
QString longFormat = QObject::tr( "%1 (%2)", "Language (Country)" );
QString sortKey = QLocale::languageToString( m_locale.language() );
QString languageName = m_locale.nativeLanguageName();
QString countryName;
if ( languageName.isEmpty() )
languageName = QString( QLatin1Literal( "* %1 (%2)" ) ).arg( locale, sortKey );
bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry ) ||
(locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
if ( needsCountryName )
{
sortKey.append( '+' );
sortKey.append( QLocale::countryToString( m_locale.country() ) );
countryName = m_locale.nativeCountryName();
}
m_sortKey = sortKey;
m_label = needsCountryName ? longFormat.arg( languageName ).arg( countryName ) : languageName;
}
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 );
}
}

View File

@ -23,6 +23,7 @@
#include "utils/CalamaresUtils.h"
#include "UiDllMacro.h"
#include <QObject>
#include <QPixmap>
#include <QSize>
@ -125,6 +126,77 @@ 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:
/** @brief Formatting option for label -- add (country) to label. */
enum class LabelFormat { AlwaysWithCountry, IfNeededWithCountry } ;
/** @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.
*/
LocaleLabel( const QString& localeName, LabelFormat format = LabelFormat::IfNeededWithCountry );
/** @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

View File

@ -369,6 +369,8 @@ KeyboardPage::onActivate()
{
const auto langParts = lang.split( '_', QString::SkipEmptyParts );
// Note that this his string is not fit for display purposes!
// It doesn't come from QLocale::nativeCountryName.
QString country = QLocale::countryToString( QLocale( lang ).country() );
cDebug() << " .. extracted country" << country << "::" << langParts;

View File

@ -21,6 +21,7 @@
#include "timezonewidget/timezonewidget.h"
#include "SetTimezoneJob.h"
#include "utils/CalamaresUtilsGui.h"
#include "utils/Logger.h"
#include "utils/Retranslator.h"
#include "GlobalStorage.h"
@ -383,12 +384,14 @@ LocalePage::init( const QString& initialRegion,
std::pair< QString, QString > LocalePage::prettyLocaleStatus( const LocaleConfiguration& lc ) const
{
using CalamaresUtils::LocaleLabel;
LocaleLabel lang( lc.lang, LocaleLabel::LabelFormat::AlwaysWithCountry );
LocaleLabel num( lc.lc_numeric, LocaleLabel::LabelFormat::AlwaysWithCountry );
return std::make_pair< QString, QString >(
tr( "The system language will be set to %1." )
.arg( prettyLCLocale( lc.lang ) ),
tr( "The numbers and dates locale will be set to %1." )
.arg( prettyLCLocale( lc.lc_numeric ) )
);
tr( "The system language will be set to %1." ).arg( lang.label() ),
tr( "The numbers and dates locale will be set to %1." ).arg( num.label() ) );
}
QString
@ -468,19 +471,6 @@ LocalePage::guessLocaleConfiguration() const
}
QString
LocalePage::prettyLCLocale( const QString& lcLocale ) const
{
QString localeString = lcLocale;
if ( localeString.endsWith( " UTF-8" ) )
localeString.remove( " UTF-8" );
QLocale locale( localeString );
//: Language (Country)
return tr( "%1 (%2)" ).arg( QLocale::languageToString( locale.language() ) )
.arg( QLocale::countryToString( locale.country() ) );
}
void
LocalePage::updateGlobalStorage()
{

View File

@ -51,7 +51,6 @@ public:
private:
LocaleConfiguration guessLocaleConfiguration() const;
QString prettyLCLocale( const QString& localesMap ) const;
// For the given locale config, return two strings describing
// the settings for language and numbers.

View File

@ -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() );
}
}
@ -345,4 +277,3 @@ WelcomePage::focusInEvent( QFocusEvent* e )
ui->languageWidget->setFocus();
e->accept();
}