2009-07-02 20:28:05 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2012-01-01 00:05:37 +01:00
|
|
|
* Copyright (C) 2007-2012 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 <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>
|
2011-01-09 23:21:51 +01:00
|
|
|
#include "translationhandler.h"
|
2009-07-02 10:32:29 +02:00
|
|
|
|
|
|
|
TranslationHandler::TranslationHandler(QObject *parent) :
|
2010-04-15 20:08:51 +02:00
|
|
|
QObject(parent),
|
2011-02-08 17:22:44 +01:00
|
|
|
mCurrentLanguage("en"),
|
2012-02-12 02:55:48 +01:00
|
|
|
mTranslator(NULL)
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
2011-02-06 19:47:12 +01:00
|
|
|
// Add our available languages
|
|
|
|
// Keep this list sorted
|
2011-02-06 21:00:16 +01:00
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Dutch"), "cppcheck_nl");
|
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "English"), "cppcheck_en");
|
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Finnish"), "cppcheck_fi");
|
2011-02-07 11:59:46 +01:00
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "French"), "cppcheck_fr");
|
2011-02-06 21:00:16 +01:00
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "German"), "cppcheck_de");
|
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Japanese"), "cppcheck_ja");
|
2012-07-28 15:10:39 +02:00
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Korean"), "cppcheck_ko");
|
2011-02-06 21:00:16 +01:00
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Polish"), "cppcheck_pl");
|
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Russian"), "cppcheck_ru");
|
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Serbian"), "cppcheck_sr");
|
2011-04-06 08:49:17 +02:00
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Spanish"), "cppcheck_es");
|
2011-03-20 15:20:06 +01:00
|
|
|
AddTranslation(QT_TRANSLATE_NOOP("MainWindow", "Swedish"), "cppcheck_sv");
|
2009-07-02 10:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TranslationHandler::~TranslationHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-10 07:26:59 +02:00
|
|
|
const QStringList TranslationHandler::GetNames() const
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
2011-02-06 21:00:16 +01:00
|
|
|
QStringList names;
|
2011-10-13 20:53:06 +02:00
|
|
|
foreach(TranslationInfo translation, mTranslations) {
|
2011-02-06 21:00:16 +01:00
|
|
|
names.append(translation.mName);
|
|
|
|
}
|
|
|
|
return names;
|
2009-07-02 10:32:29 +02:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:30:13 +01:00
|
|
|
bool TranslationHandler::SetLanguage(const QString &code, QString &error)
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
2010-12-15 18:45:53 +01:00
|
|
|
//If English is the language
|
2011-10-13 20:53:06 +02:00
|
|
|
if (code == "en") {
|
2009-07-02 10:32:29 +02:00
|
|
|
//Just remove all extra translators
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mTranslator) {
|
2009-07-02 10:32:29 +02:00
|
|
|
qApp->removeTranslator(mTranslator);
|
2012-02-12 02:55:48 +01:00
|
|
|
delete mTranslator;
|
|
|
|
mTranslator = NULL;
|
2009-07-02 10:32:29 +02:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:30:13 +01:00
|
|
|
mCurrentLanguage = code;
|
2009-07-02 10:32:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Make sure the translator is otherwise valid
|
2011-02-07 11:30:13 +01:00
|
|
|
int index = GetLanguageIndexByCode(code);
|
2011-10-13 20:53:06 +02:00
|
|
|
if (index == -1) {
|
2011-02-08 17:22:44 +01:00
|
|
|
error = QObject::tr("Unknown language specified!");
|
2009-07-02 10:32:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-12 02:55:48 +01:00
|
|
|
// Make sure there is a translator
|
|
|
|
if (!mTranslator)
|
|
|
|
mTranslator = new QTranslator(this);
|
|
|
|
|
2009-07-02 10:32:29 +02:00
|
|
|
//Load the new language
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!mTranslator->load(mTranslations[index].mFilename)) {
|
2009-07-04 11:06:31 +02:00
|
|
|
//If it failed, lets check if the default file exists
|
2011-10-13 20:53:06 +02:00
|
|
|
if (!QFile::exists(mTranslations[index].mFilename + ".qm")) {
|
2009-07-04 11:06:31 +02:00
|
|
|
error = QObject::tr("Language file %1 not found!");
|
2011-02-06 21:00:16 +01:00
|
|
|
error = error.arg(mTranslations[index].mFilename + ".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");
|
2011-02-06 21:00:16 +01:00
|
|
|
error = error.arg(mTranslations[index].mName);
|
|
|
|
error = error.arg(mTranslations[index].mFilename + ".qm");
|
2009-07-02 10:32:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qApp->installTranslator(mTranslator);
|
|
|
|
|
2011-02-07 11:30:13 +01:00
|
|
|
mCurrentLanguage = code;
|
2009-07-02 10:32:29 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-07 11:30:13 +01:00
|
|
|
QString TranslationHandler::GetCurrentLanguage() const
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
return mCurrentLanguage;
|
|
|
|
}
|
|
|
|
|
2011-02-07 11:30:13 +01:00
|
|
|
QString TranslationHandler::SuggestLanguage() const
|
2009-07-02 22:41:37 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
|
|
|
//And see if we can find it from our list of language files
|
2011-02-07 11:30:13 +01:00
|
|
|
int index = GetLanguageIndexByCode(language);
|
2009-07-02 22:41:37 +02:00
|
|
|
|
2010-12-15 18:45:53 +01:00
|
|
|
//If nothing found, return English
|
2011-10-13 20:53:06 +02:00
|
|
|
if (index < 0) {
|
2011-02-07 11:30:13 +01:00
|
|
|
return "en";
|
2009-07-02 22:41:37 +02:00
|
|
|
}
|
|
|
|
|
2011-02-07 11:30:13 +01:00
|
|
|
return language;
|
2009-07-02 22:41:37 +02:00
|
|
|
}
|
2010-12-10 16:59:36 +01:00
|
|
|
|
2011-02-06 21:00:16 +01:00
|
|
|
void TranslationHandler::AddTranslation(const char *name, const char *filename)
|
|
|
|
{
|
|
|
|
TranslationInfo info;
|
|
|
|
info.mName = name;
|
|
|
|
info.mFilename = filename;
|
2011-02-07 11:30:13 +01:00
|
|
|
info.mCode = QString(filename).right(2);
|
2011-02-06 21:00:16 +01:00
|
|
|
mTranslations.append(info);
|
|
|
|
}
|
2011-02-07 11:30:13 +01:00
|
|
|
|
|
|
|
int TranslationHandler::GetLanguageIndexByCode(const QString &code) const
|
|
|
|
{
|
|
|
|
int index = -1;
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < mTranslations.size(); i++) {
|
|
|
|
if (mTranslations[i].mCode == code) {
|
2011-02-07 11:30:13 +01:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|