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.
  • Loading branch information
jcfr committed Jan 16, 2024
1 parent b83ff7a commit a7385c9
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions Libs/Widgets/ctkLanguageComboBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ 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.
QString normalizeLocaleCode(const QString& localeCode);

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

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

// If no local 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 +178,31 @@ 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")

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

QLocale locale(localeCode);

if (this->CountryFlagsVisible)
{
QString countryFlag = locale.name();
Expand Down Expand Up @@ -228,7 +252,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 +272,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 a7385c9

Please sign in to comment.