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); QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
for (auto &it : allLocales) { 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); setLocale(it);
return true; return true;
} }
@ -650,14 +651,16 @@ QStringList Configuration::getAvailableTranslations()
QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
for (auto i : fileNames) { for (auto i : fileNames) {
QString localeName = i.mid(sizeof("cutter_") - 1, 2); QString localeName = i.mid(sizeof("cutter_") - 1, 2); // TODO:#2321 don't asume 2 characters
for (auto j : allLocales) { // language code is sometimes 3 characters, and there could also be language_COUNTRY. Qt supports that.
if (j.name().startsWith(localeName)) { QLocale locale(localeName);
currLanguageName = j.nativeLanguageName(); if (locale.language() != QLocale::C) {
currLanguageName = currLanguageName.at(0).toUpper() currLanguageName = locale.nativeLanguageName();
+ currLanguageName.right(currLanguageName.length() - 1); if (currLanguageName.isEmpty()) { // Qt doesn't have native language name for some languages
currLanguageName = QLocale::languageToString(locale.language());
}
if (!currLanguageName.isEmpty()) {
languages << currLanguageName; languages << currLanguageName;
break;
} }
} }
} }

View File

@ -26,7 +26,6 @@ WelcomeDialog::WelcomeDialog(QWidget *parent) : QDialog(parent), ui(new Ui::Welc
QStringList langs = Config()->getAvailableTranslations(); QStringList langs = Config()->getAvailableTranslations();
ui->languageComboBox->addItems(langs); ui->languageComboBox->addItems(langs);
QString curr = Config()->getCurrLocale().nativeLanguageName(); QString curr = Config()->getCurrLocale().nativeLanguageName();
curr = curr.at(0).toUpper() + curr.right(curr.length() - 1);
if (!langs.contains(curr)) { if (!langs.contains(curr)) {
curr = "English"; curr = "English";
} }
@ -64,7 +63,7 @@ void WelcomeDialog::on_themeComboBox_currentIndexChanged(int index)
*/ */
void WelcomeDialog::onLanguageComboBox_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); Config()->setLocaleByName(language);
QMessageBox mb; QMessageBox mb;

View File

@ -33,7 +33,6 @@ AppearanceOptionsWidget::AppearanceOptionsWidget(PreferencesDialog *dialog)
ui->languageComboBox->addItems(langs); ui->languageComboBox->addItems(langs);
QString curr = Config()->getCurrLocale().nativeLanguageName(); QString curr = Config()->getCurrLocale().nativeLanguageName();
curr = curr.at(0).toUpper() + curr.right(curr.length() - 1);
if (!langs.contains(curr)) { if (!langs.contains(curr)) {
curr = "English"; curr = "English";
} }