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(); }