Merge branch 'issue-1308'

FIXES #1308
This commit is contained in:
Adriaan de Groot 2020-01-28 17:38:11 +01:00
commit 5a732a2d7d
2 changed files with 20 additions and 42 deletions

View File

@ -18,7 +18,11 @@
*/ */
#include "LocaleConfiguration.h" #include "LocaleConfiguration.h"
#include "utils/Logger.h"
#include <QLocale> #include <QLocale>
#include <QRegularExpression>
LocaleConfiguration::LocaleConfiguration() LocaleConfiguration::LocaleConfiguration()
: explicit_lang( false ) : explicit_lang( false )
@ -53,14 +57,9 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
{ {
QString language = languageLocale.split( '_' ).first(); QString language = languageLocale.split( '_' ).first();
QStringList linesForLanguage; // Either an exact match, or the whole language part matches
for ( const QString& line : availableLocales ) // (followed by .<encoding> or _<country>
{ QStringList linesForLanguage = availableLocales.filter( QRegularExpression( language + "[._]" ) );
if ( line.startsWith( language ) )
{
linesForLanguage.append( line );
}
}
QString lang; QString lang;
if ( linesForLanguage.length() == 0 || languageLocale.isEmpty() ) if ( linesForLanguage.length() == 0 || languageLocale.isEmpty() )
@ -71,23 +70,6 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
{ {
lang = linesForLanguage.first(); lang = linesForLanguage.first();
} }
else
{
QStringList linesForLanguageUtf;
// FIXME: this might be useless if we already filter out non-UTF8 locales
foreach ( QString line, linesForLanguage )
{
if ( line.contains( "UTF-8", Qt::CaseInsensitive ) || line.contains( "utf8", Qt::CaseInsensitive ) )
{
linesForLanguageUtf.append( line );
}
}
if ( linesForLanguageUtf.length() == 1 )
{
lang = linesForLanguageUtf.first();
}
}
// lang could still be empty if we found multiple locales that satisfy myLanguage // lang could still be empty if we found multiple locales that satisfy myLanguage

View File

@ -227,27 +227,23 @@ LocalePage::init( const QString& initialRegion, const QString& initialZone, cons
// Assuming we have a list of supported locales, we usually only want UTF-8 ones // Assuming we have a list of supported locales, we usually only want UTF-8 ones
// because it's not 1995. // because it's not 1995.
for ( auto it = m_localeGenLines.begin(); it != m_localeGenLines.end(); ) auto notUtf8 = []( const QString& s ) {
{ return !s.contains( "UTF-8", Qt::CaseInsensitive ) && !s.contains( "utf8", Qt::CaseInsensitive );
if ( !it->contains( "UTF-8", Qt::CaseInsensitive ) && !it->contains( "utf8", Qt::CaseInsensitive ) ) };
{ auto it = std::remove_if( m_localeGenLines.begin(), m_localeGenLines.end(), notUtf8 );
it = m_localeGenLines.erase( it ); m_localeGenLines.erase( it, m_localeGenLines.end() );
}
else
{
++it;
}
}
// We strip " UTF-8" from "en_US.UTF-8 UTF-8" because it's redundant redundant. // We strip " UTF-8" from "en_US.UTF-8 UTF-8" because it's redundant redundant.
for ( auto it = m_localeGenLines.begin(); it != m_localeGenLines.end(); ++it ) // Also simplify whitespace.
{ auto unredundant = []( QString& s ) {
if ( it->endsWith( " UTF-8" ) ) if ( s.endsWith( " UTF-8" ) )
{ {
it->chop( 6 ); s.chop( 6 );
} }
*it = it->simplified(); s = s.simplified();
} };
std::for_each( m_localeGenLines.begin(), m_localeGenLines.end(), unredundant );
updateGlobalStorage(); updateGlobalStorage();
} }