2009-07-02 20:28:05 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2009-07-02 20:28:05 +02:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-07-02 20:28:05 +02:00
|
|
|
*/
|
|
|
|
|
2009-07-02 10:32:29 +02:00
|
|
|
#include "translationhandler.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
2009-07-03 13:18:35 +02:00
|
|
|
#include <QFile>
|
2009-07-02 10:32:29 +02:00
|
|
|
#include <QDebug>
|
2010-02-28 12:03:34 +01:00
|
|
|
#include <QLocale>
|
2009-07-02 10:32:29 +02:00
|
|
|
|
|
|
|
TranslationHandler::TranslationHandler(QObject *parent) :
|
2010-04-15 20:08:51 +02:00
|
|
|
QObject(parent),
|
|
|
|
mCurrentLanguage(-1),
|
|
|
|
mTranslator(new QTranslator(this))
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
//Add our default languages
|
2009-07-04 14:54:28 +02:00
|
|
|
mNames << QT_TRANSLATE_NOOP("MainWindow", "English")
|
2010-04-15 20:08:51 +02:00
|
|
|
<< QT_TRANSLATE_NOOP("MainWindow", "Dutch")
|
|
|
|
<< QT_TRANSLATE_NOOP("MainWindow", "Finnish")
|
|
|
|
<< QT_TRANSLATE_NOOP("MainWindow", "Swedish")
|
|
|
|
<< QT_TRANSLATE_NOOP("MainWindow", "German")
|
|
|
|
<< QT_TRANSLATE_NOOP("MainWindow", "Russian")
|
2010-12-08 10:16:48 +01:00
|
|
|
<< QT_TRANSLATE_NOOP("MainWindow", "Polish")
|
|
|
|
<< QT_TRANSLATE_NOOP("MainWindow", "Japanease");
|
2009-07-02 10:32:29 +02:00
|
|
|
|
|
|
|
mFiles << "cppcheck_en"
|
2010-04-15 20:08:51 +02:00
|
|
|
<< "cppcheck_nl"
|
|
|
|
<< "cppcheck_fi"
|
|
|
|
<< "cppcheck_se"
|
|
|
|
<< "cppcheck_de"
|
|
|
|
<< "cppcheck_ru"
|
2010-12-08 10:16:48 +01:00
|
|
|
<< "cppcheck_pl"
|
|
|
|
<< "cppcheck_ja";
|
2009-07-02 10:32:29 +02:00
|
|
|
|
|
|
|
//Load english as a fallback language
|
|
|
|
QTranslator *english = new QTranslator();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (english->load("cppcheck_en"))
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
qApp->installTranslator(english);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-03 13:18:35 +02:00
|
|
|
qDebug() << "Failed to load English translation!";
|
2009-07-02 10:32:29 +02:00
|
|
|
delete english;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TranslationHandler::~TranslationHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-10 07:26:59 +02:00
|
|
|
const QStringList TranslationHandler::GetNames() const
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
return mNames;
|
|
|
|
}
|
|
|
|
|
2010-08-10 07:26:59 +02:00
|
|
|
const QStringList TranslationHandler::GetFiles() const
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
return mFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TranslationHandler::SetLanguage(const int index, QString &error)
|
|
|
|
{
|
|
|
|
//If english is the language
|
2010-04-02 07:30:58 +02:00
|
|
|
if (index == 0)
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
//Just remove all extra translators
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mTranslator)
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
qApp->removeTranslator(mTranslator);
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentLanguage = index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make sure the translator is otherwise valid
|
2010-04-02 07:30:58 +02:00
|
|
|
if (index >= mNames.size())
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
error = QObject::tr("Incorrect language specified!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Load the new language
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!mTranslator->load(mFiles[index]))
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
2009-07-04 11:06:31 +02:00
|
|
|
//If it failed, lets check if the default file exists
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!QFile::exists(mFiles[index] + ".qm"))
|
2009-07-04 11:06:31 +02:00
|
|
|
{
|
|
|
|
error = QObject::tr("Language file %1 not found!");
|
2009-07-04 14:54:28 +02:00
|
|
|
error = error.arg(mFiles[index] + ".qm");
|
2009-07-04 11:06:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//If file exists, there's something wrong with it
|
|
|
|
error = QObject::tr("Failed to load translation for language %1 from file %2");
|
|
|
|
error = error.arg(mNames[index]);
|
2009-07-04 14:54:28 +02:00
|
|
|
error = error.arg(mFiles[index] + ".qm");
|
2009-07-02 10:32:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qApp->installTranslator(mTranslator);
|
|
|
|
|
|
|
|
mCurrentLanguage = index;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TranslationHandler::GetCurrentLanguage() const
|
|
|
|
{
|
|
|
|
return mCurrentLanguage;
|
|
|
|
}
|
|
|
|
|
2009-07-02 22:41:37 +02:00
|
|
|
int TranslationHandler::SuggestLanguage() const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Get language from system locale's name
|
|
|
|
QLocale::languageToString would return the languages full name and we
|
|
|
|
only want two-letter ISO 639 language code so we'll get it from
|
|
|
|
locale's name.
|
|
|
|
*/
|
|
|
|
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 = mFiles.indexOf(file);
|
|
|
|
|
|
|
|
//If nothing found, return english
|
2010-04-02 07:30:58 +02:00
|
|
|
if (index < 0)
|
2009-07-02 22:41:37 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|