Skip to content

Commit

Permalink
Fix crash due to language handling when opening settings.
Browse files Browse the repository at this point in the history
Qt doesn't have native language name for some of them. Trying to
capitalize it caused crash.

Use `QLocale(QString)` constructor instead of manually looping and
comparing. The old code incorrectly matched "tr" as "trv".

Don't try to capitalize language name:
* In many cases Qt already returns it capitalized
* capitalization doesn't make sense for some scripts
* in general case splitting first "character" is a hard problem
* in some languages even with latin based scripts name of language isn't
a proper noun which needs to be capitalized
  • Loading branch information
karliss committed Apr 24, 2021
1 parent 585dc96 commit 8da572d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/common/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ bool Configuration::setLocaleByName(const QString &language)
QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);

for (auto &it : allLocales) {
if (QString::compare(it.nativeLanguageName(), language, Qt::CaseInsensitive) == 0) {
if (QString::compare(it.nativeLanguageName(), language, Qt::CaseInsensitive) == 0 ||
it.name() == language) {
setLocale(it);
return true;
}
Expand Down Expand Up @@ -650,14 +651,16 @@ QStringList Configuration::getAvailableTranslations()
QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);

for (auto i : fileNames) {
QString localeName = i.mid(sizeof("cutter_") - 1, 2);
for (auto j : allLocales) {
if (j.name().startsWith(localeName)) {
currLanguageName = j.nativeLanguageName();
currLanguageName = currLanguageName.at(0).toUpper()
+ currLanguageName.right(currLanguageName.length() - 1);
QString localeName = i.mid(sizeof("cutter_") - 1, 2); // TODO:#2321 don't asume 2 characters
// language code is sometimes 3 characters, and there could also be language_COUNTRY. Qt supports that.
QLocale locale(localeName);
if (locale.language() != QLocale::C) {
currLanguageName = locale.nativeLanguageName();
if (currLanguageName.isEmpty()) { // Qt doesn't have native language name for some languages
currLanguageName = QLocale::languageToString(locale.language());
}
if (!currLanguageName.isEmpty()) {
languages << currLanguageName;
break;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/dialogs/WelcomeDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ WelcomeDialog::WelcomeDialog(QWidget *parent) : QDialog(parent), ui(new Ui::Welc
QStringList langs = Config()->getAvailableTranslations();
ui->languageComboBox->addItems(langs);
QString curr = Config()->getCurrLocale().nativeLanguageName();
curr = curr.at(0).toUpper() + curr.right(curr.length() - 1);
if (!langs.contains(curr)) {
curr = "English";
}
Expand Down Expand Up @@ -64,7 +63,7 @@ void WelcomeDialog::on_themeComboBox_currentIndexChanged(int index)
*/
void WelcomeDialog::onLanguageComboBox_currentIndexChanged(int index)
{
QString language = ui->languageComboBox->itemText(index).toLower();
QString language = ui->languageComboBox->itemText(index);
Config()->setLocaleByName(language);

QMessageBox mb;
Expand Down
1 change: 0 additions & 1 deletion src/dialogs/preferences/AppearanceOptionsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog)
ui->languageComboBox->addItems(langs);

QString curr = Config()->getCurrLocale().nativeLanguageName();
curr = curr.at(0).toUpper() + curr.right(curr.length() - 1);
if (!langs.contains(curr)) {
curr = "English";
}
Expand Down

0 comments on commit 8da572d

Please sign in to comment.