Fix crash due to language handling when opening settings.

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
This commit is contained in:
Kārlis Seņko 2021-04-14 21:23:30 +03:00 committed by karliss
parent 585dc961db
commit 8da572d6ec
3 changed files with 12 additions and 11 deletions

View File

@ -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;
}
@ -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;
}
}
}

View File

@ -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";
}
@ -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;

View File

@ -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";
}