GUI: Store selected language as language code.

GUI was storing selected language as index to the languages list.
This is fragile since the order and count of items can change.
This commit changes to using ISO language code (e.g. "en" for
"English").

Fixes ticket #2446 (GUI: Don't use index number for language selection)
This commit is contained in:
Kimmo Varis 2011-02-07 12:30:13 +02:00
parent 64a70fa51c
commit fd112fc16e
6 changed files with 58 additions and 42 deletions

View File

@ -154,7 +154,7 @@ void MainWindow::LoadSettings()
mApplications->LoadSettings(mSettings);
SetLanguage(mSettings->value(SETTINGS_LANGUAGE, mTranslation->SuggestLanguage()).toInt());
SetLanguage(mSettings->value(SETTINGS_LANGUAGE, mTranslation->SuggestLanguage()).toString());
}
void MainWindow::SaveSettings()
@ -410,8 +410,8 @@ void MainWindow::ProgramSettings()
dialog.SaveFullPath(),
dialog.SaveAllErrors(),
dialog.ShowNoErrorsMessage());
const int currentLang = mTranslation->GetCurrentLanguage();
const int newLang = mSettings->value(SETTINGS_LANGUAGE, 0).toInt();
const QString currentLang = mTranslation->GetCurrentLanguage();
const QString newLang = mSettings->value(SETTINGS_LANGUAGE, "en").toString();
if (currentLang != newLang)
SetLanguage(newLang);
}
@ -651,15 +651,15 @@ void MainWindow::FormatAndSetTitle(const QString &text)
}
void MainWindow::SetLanguage(int index)
void MainWindow::SetLanguage(const QString &code)
{
if (mTranslation->GetCurrentLanguage() == index)
if (mTranslation->GetCurrentLanguage() == code)
{
return;
}
QString error;
if (!mTranslation->SetLanguage(index, error))
if (!mTranslation->SetLanguage(code, error))
{
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),

View File

@ -255,9 +255,9 @@ protected:
/**
* @brief Set current language
* @param index Index of the language to set
* @param code Language code of the language to set (e.g. "en").
*/
void SetLanguage(const int index);
void SetLanguage(const QString &code);
/**
* @brief Event coming when application is about to close.

View File

@ -91,13 +91,17 @@ SettingsDialog::~SettingsDialog()
void SettingsDialog::InitTranslationsList()
{
QStringList languages = mTranslator->GetNames();
foreach(const QString lang, languages)
const QString current = mTranslator->GetCurrentLanguage();
QList<TranslationInfo> translations = mTranslator->GetTranslations();
foreach(TranslationInfo translation, translations)
{
mUI.mListLanguages->addItem(lang);
QListWidgetItem *item = new QListWidgetItem;
item->setText(translation.mName);
item->setData(LangCodeRole, QVariant(translation.mCode));
mUI.mListLanguages->addItem(item);
if (translation.mCode == current)
mUI.mListLanguages->setCurrentItem(item);
}
const int current = mTranslator->GetCurrentLanguage();
mUI.mListLanguages->setCurrentRow(current);
}
Qt::CheckState SettingsDialog::BoolToCheckState(bool yes) const
@ -148,7 +152,10 @@ void SettingsDialog::SaveSettingValues()
SaveCheckboxValue(mUI.mShowDebugWarnings, SETTINGS_SHOW_DEBUG_WARNINGS);
SaveCheckboxValue(mUI.mInlineSuppressions, SETTINGS_INLINE_SUPPRESSIONS);
mSettings->setValue(SETTINGS_GLOBAL_INCLUDE_PATHS, mUI.mEditIncludePaths->text());
mSettings->setValue(SETTINGS_LANGUAGE, mUI.mListLanguages->currentRow());
QListWidgetItem *currentLang = mUI.mListLanguages->currentItem();
const QString langcode = currentLang->data(LangCodeRole).toString();
mSettings->setValue(SETTINGS_LANGUAGE, langcode);
}
void SettingsDialog::SaveCheckboxValue(QCheckBox *box, const QString &name)

View File

@ -201,6 +201,7 @@ protected:
*/
Ui::Settings mUI;
private:
static const int LangCodeRole = Qt::UserRole;
};
/// @}
#endif // SETTINGSDIALOG_H

View File

@ -24,7 +24,6 @@
TranslationHandler::TranslationHandler(QObject *parent) :
QObject(parent),
mCurrentLanguage(-1),
mTranslator(new QTranslator(this))
{
// Add our available languages
@ -66,10 +65,10 @@ const QStringList TranslationHandler::GetNames() const
return names;
}
bool TranslationHandler::SetLanguage(const int index, QString &error)
bool TranslationHandler::SetLanguage(const QString &code, QString &error)
{
//If English is the language
if (index == 2)
if (code == "en")
{
//Just remove all extra translators
if (mTranslator)
@ -77,12 +76,13 @@ bool TranslationHandler::SetLanguage(const int index, QString &error)
qApp->removeTranslator(mTranslator);
}
mCurrentLanguage = index;
mCurrentLanguage = code;
return true;
}
//Make sure the translator is otherwise valid
if (index >= mTranslations.size())
int index = GetLanguageIndexByCode(code);
if (index == -1)
{
error = QObject::tr("Incorrect language specified!");
return false;
@ -108,17 +108,17 @@ bool TranslationHandler::SetLanguage(const int index, QString &error)
qApp->installTranslator(mTranslator);
mCurrentLanguage = index;
mCurrentLanguage = code;
return true;
}
int TranslationHandler::GetCurrentLanguage() const
QString TranslationHandler::GetCurrentLanguage() const
{
return mCurrentLanguage;
}
int TranslationHandler::SuggestLanguage() const
QString TranslationHandler::SuggestLanguage() const
{
/*
Get language from system locale's name
@ -129,29 +129,16 @@ int TranslationHandler::SuggestLanguage() const
QString language = QLocale::system().name().left(2);
//qDebug()<<"Your language is"<<language;
//catenate that to the default language filename
QString file = QString("cppcheck_%1").arg(language);
//qDebug()<<"Language file could be"<<file;
//And see if we can find it from our list of language files
int index = 0;
for (int i = 0; i < mTranslations.size(); i++)
{
if (mTranslations[i].mFilename == file)
{
index = i;
break;
}
}
int index = GetLanguageIndexByCode(language);
//If nothing found, return English
if (index < 0)
{
return 0;
return "en";
}
return index;
return language;
}
void TranslationHandler::AddTranslation(const char *name, const char *filename)
@ -159,5 +146,20 @@ void TranslationHandler::AddTranslation(const char *name, const char *filename)
TranslationInfo info;
info.mName = name;
info.mFilename = filename;
info.mCode = QString(filename).right(2);
mTranslations.append(info);
}
int TranslationHandler::GetLanguageIndexByCode(const QString &code) const
{
int index = -1;
for (int i = 0; i < mTranslations.size(); i++)
{
if (mTranslations[i].mCode == code)
{
index = i;
break;
}
}
return index;
}

View File

@ -31,6 +31,7 @@ struct TranslationInfo
{
QString mName;
QString mFilename;
QString mCode;
};
@ -41,13 +42,18 @@ public:
TranslationHandler(QObject *parent);
virtual ~TranslationHandler();
const QStringList GetNames() const;
bool SetLanguage(const int index, QString &error);
int GetCurrentLanguage() const;
int SuggestLanguage() const;
QList<TranslationInfo> GetTranslations() const
{
return mTranslations;
}
bool SetLanguage(const QString &code, QString &error);
QString GetCurrentLanguage() const;
QString SuggestLanguage() const;
protected:
void AddTranslation(const char *name, const char *filename);
int GetLanguageIndexByCode(const QString &code) const;
int mCurrentLanguage;
QString mCurrentLanguage;
QList<TranslationInfo> mTranslations;
QTranslator *mTranslator;
private: