[locale] Factor out the guess-language part
This commit is contained in:
parent
40527ffd4e
commit
6cbf2d7e32
@ -42,16 +42,14 @@ LocaleConfiguration::setLanguage( const QString& localeName )
|
|||||||
m_lang = localeName;
|
m_lang = localeName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @brief Returns the QString from @p availableLocales that best-matches.
|
||||||
LocaleConfiguration
|
*/
|
||||||
LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
|
static LocaleNameParts
|
||||||
const QStringList& availableLocales,
|
identifyBestLanguageMatch( const QString& languageLocale,
|
||||||
const QString& countryCode )
|
const QStringList& availableLocales,
|
||||||
|
const QString& countryCode )
|
||||||
{
|
{
|
||||||
cDebug() << "Mapping" << languageLocale << "in" << countryCode << "to locale.";
|
|
||||||
|
|
||||||
const QString default_lang = QStringLiteral( "en_US.UTF-8" );
|
const QString default_lang = QStringLiteral( "en_US.UTF-8" );
|
||||||
QString lang;
|
|
||||||
|
|
||||||
const LocaleNameParts self = LocaleNameParts::fromName( languageLocale );
|
const LocaleNameParts self = LocaleNameParts::fromName( languageLocale );
|
||||||
if ( self.isValid() && !availableLocales.isEmpty() )
|
if ( self.isValid() && !availableLocales.isEmpty() )
|
||||||
@ -95,17 +93,22 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
|
|||||||
if ( best_match.isValid() )
|
if ( best_match.isValid() )
|
||||||
{
|
{
|
||||||
cDebug() << Logger::SubEntry << "Matched best with" << best_match.name();
|
cDebug() << Logger::SubEntry << "Matched best with" << best_match.name();
|
||||||
lang = best_match.name();
|
return best_match;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Else we have an unrecognized or unsupported locale, all we can do is go with
|
// Else we have an unrecognized or unsupported locale, all we can do is go with
|
||||||
// en_US.UTF-8 UTF-8. This completes all default language setting guesswork.
|
// en_US.UTF-8 UTF-8. This completes all default language setting guesswork.
|
||||||
if ( lang.isEmpty() )
|
return LocaleNameParts::fromName( default_lang );
|
||||||
{
|
}
|
||||||
lang = default_lang;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
LocaleConfiguration
|
||||||
|
LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
|
||||||
|
const QStringList& availableLocales,
|
||||||
|
const QString& countryCode )
|
||||||
|
{
|
||||||
|
cDebug() << "Mapping" << languageLocale << "in" << countryCode << "to locale.";
|
||||||
|
const auto bestLocale = identifyBestLanguageMatch( languageLocale, availableLocales, countryCode );
|
||||||
|
|
||||||
// The following block was inspired by Ubiquity, scripts/localechooser-apply.
|
// The following block was inspired by Ubiquity, scripts/localechooser-apply.
|
||||||
// No copyright statement found in file, assuming GPL v2 or later.
|
// No copyright statement found in file, assuming GPL v2 or later.
|
||||||
@ -153,11 +156,11 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
|
|||||||
// We make a proposed locale based on the UI language and the timezone's country. There is no
|
// We make a proposed locale based on the UI language and the timezone's country. There is no
|
||||||
// guarantee that this will be a valid, supported locale (often it won't).
|
// guarantee that this will be a valid, supported locale (often it won't).
|
||||||
QString lc_formats;
|
QString lc_formats;
|
||||||
const QString combined = QString( "%1_%2" ).arg( self.language ).arg( countryCode );
|
const QString combined = QString( "%1_%2" ).arg( bestLocale.language ).arg( countryCode );
|
||||||
if ( availableLocales.contains( lang ) )
|
if ( availableLocales.contains( bestLocale.language ) )
|
||||||
{
|
{
|
||||||
cDebug() << Logger::SubEntry << "Exact formats match for language tag" << lang;
|
cDebug() << Logger::SubEntry << "Exact formats match for language tag" << bestLocale.language;
|
||||||
lc_formats = lang;
|
lc_formats = bestLocale.language;
|
||||||
}
|
}
|
||||||
else if ( availableLocales.contains( combined ) )
|
else if ( availableLocales.contains( combined ) )
|
||||||
{
|
{
|
||||||
@ -250,12 +253,7 @@ LocaleConfiguration::fromLanguageAndLocation( const QString& languageLocale,
|
|||||||
|
|
||||||
// If we cannot make a good choice for a given country we go with the LANG
|
// If we cannot make a good choice for a given country we go with the LANG
|
||||||
// setting, which defaults to en_US.UTF-8 UTF-8 if all else fails.
|
// setting, which defaults to en_US.UTF-8 UTF-8 if all else fails.
|
||||||
if ( lc_formats.isEmpty() )
|
return LocaleConfiguration( bestLocale.name(), lc_formats.isEmpty() ? bestLocale.name() : lc_formats );
|
||||||
{
|
|
||||||
lc_formats = lang;
|
|
||||||
}
|
|
||||||
|
|
||||||
return LocaleConfiguration( lang, lc_formats );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user