cppcheck/gui/translationhandler.cpp

112 lines
2.6 KiB
C++
Raw Normal View History

2009-07-02 20:28:05 +02:00
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include "translationhandler.h"
#include <QApplication>
#include <QDebug>
TranslationHandler::TranslationHandler(QObject *parent) :
QObject(parent),
mCurrentLanguage(-1),
mTranslator(new QTranslator(this))
{
//Add our default languages
mNames << QObject::tr("English")
<< QObject::tr("Finnish")
<< QObject::tr("Swedish")
<< QObject::tr("German")
<< QObject::tr("Russian");
mFiles << "cppcheck_en"
<< "cppcheck_fi"
<< "cppcheck_se"
<< "cppcheck_de"
<< "cppcheck_ru";
//Load english as a fallback language
QTranslator *english = new QTranslator();
if (english->load("cppcheck_en"))
{
qApp->installTranslator(english);
}
else
{
qDebug() << "Failed to load english translation!";
delete english;
}
}
TranslationHandler::~TranslationHandler()
{
}
const QStringList TranslationHandler::GetNames()
{
return mNames;
}
const QStringList TranslationHandler::GetFiles()
{
return mFiles;
}
bool TranslationHandler::SetLanguage(const int index, QString &error)
{
//If english is the language
if (index == 0)
{
//Just remove all extra translators
if (mTranslator)
{
qApp->removeTranslator(mTranslator);
}
mCurrentLanguage = index;
return true;
}
//Make sure the translator is otherwise valid
if (index >= mNames.size())
{
error = QObject::tr("Incorrect language specified!");
return false;
}
//Load the new language
if (!mTranslator->load(mFiles[index]))
{
error = QObject::tr("Failed to load language from file %1");
error = error.arg(mFiles[index]);
return false;
}
qApp->installTranslator(mTranslator);
mCurrentLanguage = index;
return true;
}
int TranslationHandler::GetCurrentLanguage() const
{
return mCurrentLanguage;
}