Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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