Skip to content

Commit

Permalink
BUG: Fix ctkLanguageComboBox normalizing default language and selection
Browse files Browse the repository at this point in the history
Discovered during the resolution of issues in `ctkLanguageComboBoxTest`, this
commit addresses a bug in the normalization and selection of the default language
in `ctkLanguageComboBox`.

It introduces the function `normalizeLocaleCode` to ensure that the default locale
code is set to its normalized value if valid.

Furthermore, the `updateLanguageItems()` function is updated to select the default
locale code if the user has not already made a selection.

Co-authored-by: Andras Lasso <lasso@queensu.ca>
  • Loading branch information
jcfr and lassoan committed Jan 16, 2024
1 parent b83ff7a commit 9e7f262
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions Libs/Widgets/ctkLanguageComboBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,20 @@ class ctkLanguageComboBoxPrivate
public:
ctkLanguageComboBoxPrivate(ctkLanguageComboBox& object);
void init();

void updateLanguageItems();

/// \brief Normalize Locale Code
///
/// Converts the input locale code to its long form, handling incomplete or
/// extended inputs. For example, "en" -> "en_US", "fr" -> "fr_FR", "fr_" -> "fr_FR",
/// "en_U" -> "en_US", "fr_FRRRR" -> "fr_FR". If the input is invalid, it returns
/// an empty string.
///
/// \note The support for normalizing incomplete or extended inputs aligns with the
/// behavior of `qt.QLocale`.
QString normalizeLocaleCode(const QString& localeCode);

bool languageItem(const QString& localeCode,
QIcon& icon, QString& text,QVariant& data, bool showCountry);

Expand Down Expand Up @@ -87,6 +100,12 @@ void ctkLanguageComboBoxPrivate::updateLanguageItems()
localeCodes.append(this->DefaultLanguage);
}

// If no locale code was selected and a default one was set, select it.
if (selectedLocaleCode.isEmpty() && !this->DefaultLanguage.isEmpty())
{
selectedLocaleCode = this->DefaultLanguage;
}

// Get locale codes from translation files from all the specified directories
foreach(const QString& languageDirectory, this->LanguageDirectories)
{
Expand Down Expand Up @@ -162,23 +181,32 @@ void ctkLanguageComboBoxPrivate::updateLanguageItems()
}
}

// ----------------------------------------------------------------------------
QString ctkLanguageComboBoxPrivate::normalizeLocaleCode(const QString& localeCode)
{
QLocale normalized(localeCode);
return normalized.name() == "C" ? QString() : normalized.name();
}

// ----------------------------------------------------------------------------
bool ctkLanguageComboBoxPrivate::languageItem(const QString& localeCode,
QIcon& icon,
QString& text,
QVariant& data,
bool showCountry)
{
QLocale locale(localeCode);
if (localeCode.isEmpty() ||
locale.name() == "C")

QString normalizedLocaleCode = this->normalizeLocaleCode(localeCode);
if (normalizedLocaleCode.isEmpty())
{
icon = QIcon();
text = QString();
data = QVariant();
return false;
}

QLocale locale(normalizedLocaleCode);

if (this->CountryFlagsVisible)
{
QString countryFlag = locale.name();
Expand Down Expand Up @@ -228,7 +256,7 @@ ctkLanguageComboBox::ctkLanguageComboBox(const QString& defaultLanguage,
, d_ptr(new ctkLanguageComboBoxPrivate(*this))
{
Q_D(ctkLanguageComboBox);
d->DefaultLanguage = defaultLanguage;
d->DefaultLanguage = d->normalizeLocaleCode(defaultLanguage);
d->init();
}

Expand All @@ -248,7 +276,7 @@ QString ctkLanguageComboBox::defaultLanguage() const
void ctkLanguageComboBox::setDefaultLanguage(const QString& localeCode)
{
Q_D(ctkLanguageComboBox);
d->DefaultLanguage = localeCode;
d->DefaultLanguage = d->normalizeLocaleCode(localeCode);
d->updateLanguageItems();
}

Expand Down

0 comments on commit 9e7f262

Please sign in to comment.