From 9e7f2623fcadfac9c1569a817b850e412d689063 Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Mon, 15 Jan 2024 19:52:49 -0500 Subject: [PATCH] BUG: Fix ctkLanguageComboBox normalizing default language and selection 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 --- Libs/Widgets/ctkLanguageComboBox.cpp | 38 ++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/Libs/Widgets/ctkLanguageComboBox.cpp b/Libs/Widgets/ctkLanguageComboBox.cpp index d817062bcf..344f0dc488 100644 --- a/Libs/Widgets/ctkLanguageComboBox.cpp +++ b/Libs/Widgets/ctkLanguageComboBox.cpp @@ -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); @@ -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) { @@ -162,6 +181,13 @@ 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, @@ -169,9 +195,9 @@ bool ctkLanguageComboBoxPrivate::languageItem(const QString& localeCode, 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(); @@ -179,6 +205,8 @@ bool ctkLanguageComboBoxPrivate::languageItem(const QString& localeCode, return false; } + QLocale locale(normalizedLocaleCode); + if (this->CountryFlagsVisible) { QString countryFlag = locale.name(); @@ -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(); } @@ -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(); }