2009-07-02 20:28:05 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 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
|
|
|
*/
|
|
|
|
|
2021-04-03 21:30:50 +02:00
|
|
|
#include "translationhandler.h"
|
|
|
|
|
2022-05-15 12:42:29 +02:00
|
|
|
#include "config.h"
|
2022-02-02 16:17:28 +01:00
|
|
|
#include "common.h"
|
|
|
|
|
2009-07-02 10:32:29 +02:00
|
|
|
#include <QApplication>
|
2009-07-03 13:18:35 +02:00
|
|
|
#include <QFile>
|
2022-02-02 16:17:28 +01:00
|
|
|
#include <QFileInfo>
|
2010-02-28 12:03:34 +01:00
|
|
|
#include <QLocale>
|
2013-03-01 19:11:27 +01:00
|
|
|
#include <QMessageBox>
|
2021-04-03 21:30:50 +02:00
|
|
|
#include <QTranslator>
|
2009-07-02 10:32:29 +02:00
|
|
|
|
2015-12-07 11:24:54 +01:00
|
|
|
|
2013-03-01 20:07:28 +01:00
|
|
|
// Provide own translations for standard buttons. This (garbage) code is needed to enforce them to appear in .ts files even after "lupdate gui.pro"
|
2022-05-15 12:42:29 +02:00
|
|
|
static UNUSED void unused()
|
2013-03-01 20:07:28 +01:00
|
|
|
{
|
2021-12-17 21:49:32 +01:00
|
|
|
Q_UNUSED(QT_TRANSLATE_NOOP("QPlatformTheme", "OK"))
|
|
|
|
Q_UNUSED(QT_TRANSLATE_NOOP("QPlatformTheme", "Cancel"))
|
|
|
|
Q_UNUSED(QT_TRANSLATE_NOOP("QPlatformTheme", "Close"))
|
|
|
|
Q_UNUSED(QT_TRANSLATE_NOOP("QPlatformTheme", "Save"))
|
2013-03-01 20:07:28 +01:00
|
|
|
}
|
|
|
|
|
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"),
|
2019-06-30 21:39:22 +02:00
|
|
|
mTranslator(nullptr)
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
2011-02-06 19:47:12 +01:00
|
|
|
// Add our available languages
|
|
|
|
// Keep this list sorted
|
2017-07-28 13:43:49 +02:00
|
|
|
addTranslation("Chinese (Simplified)", "cppcheck_zh_CN");
|
|
|
|
addTranslation("Dutch", "cppcheck_nl");
|
|
|
|
addTranslation("English", "cppcheck_en");
|
|
|
|
addTranslation("Finnish", "cppcheck_fi");
|
|
|
|
addTranslation("French", "cppcheck_fr");
|
|
|
|
addTranslation("German", "cppcheck_de");
|
|
|
|
addTranslation("Italian", "cppcheck_it");
|
|
|
|
addTranslation("Japanese", "cppcheck_ja");
|
|
|
|
addTranslation("Korean", "cppcheck_ko");
|
|
|
|
addTranslation("Russian", "cppcheck_ru");
|
|
|
|
addTranslation("Serbian", "cppcheck_sr");
|
|
|
|
addTranslation("Spanish", "cppcheck_es");
|
|
|
|
addTranslation("Swedish", "cppcheck_sv");
|
2009-07-02 10:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TranslationHandler::~TranslationHandler()
|
2021-08-07 20:51:18 +02:00
|
|
|
{}
|
2009-07-02 10:32:29 +02:00
|
|
|
|
2017-07-28 13:43:49 +02:00
|
|
|
bool TranslationHandler::setLanguage(const QString &code)
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
2013-03-01 19:11:27 +01:00
|
|
|
bool failure = false;
|
|
|
|
QString error;
|
|
|
|
|
2021-07-23 19:27:53 +02:00
|
|
|
//If English is the language. Code can be e.g. en_US
|
|
|
|
if (code.indexOf("en") == 0) {
|
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;
|
2019-06-30 21:39:22 +02:00
|
|
|
mTranslator = nullptr;
|
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
|
2017-07-28 13:43:49 +02: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!");
|
2013-03-01 19:11:27 +01:00
|
|
|
failure = true;
|
2019-07-14 22:55:35 +02:00
|
|
|
} else {
|
|
|
|
// Make sure there is a translator
|
|
|
|
if (!mTranslator && !failure)
|
|
|
|
mTranslator = new QTranslator(this);
|
|
|
|
|
|
|
|
//Load the new language
|
|
|
|
const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();
|
|
|
|
|
2020-08-29 21:26:49 +02:00
|
|
|
QString datadir = getDataDir();
|
2019-07-14 22:55:35 +02:00
|
|
|
|
|
|
|
QString translationFile;
|
|
|
|
if (QFile::exists(datadir + "/lang/" + mTranslations[index].mFilename + ".qm"))
|
|
|
|
translationFile = datadir + "/lang/" + mTranslations[index].mFilename + ".qm";
|
|
|
|
|
|
|
|
else if (QFile::exists(datadir + "/" + mTranslations[index].mFilename + ".qm"))
|
|
|
|
translationFile = datadir + "/" + mTranslations[index].mFilename + ".qm";
|
|
|
|
|
|
|
|
else
|
|
|
|
translationFile = appPath + "/" + mTranslations[index].mFilename + ".qm";
|
|
|
|
|
|
|
|
if (!mTranslator->load(translationFile) && !failure) {
|
|
|
|
//If it failed, lets check if the default file exists
|
|
|
|
if (!QFile::exists(translationFile)) {
|
|
|
|
error = QObject::tr("Language file %1 not found!");
|
|
|
|
error = error.arg(translationFile);
|
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//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(mTranslations[index].mName);
|
2013-08-03 20:10:28 +02:00
|
|
|
error = error.arg(translationFile);
|
2009-07-04 11:06:31 +02:00
|
|
|
}
|
2013-03-01 19:11:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (failure) {
|
|
|
|
const QString msg(tr("Failed to change the user interface language:"
|
|
|
|
"\n\n%1\n\n"
|
|
|
|
"The user interface language has been reset to English. Open "
|
|
|
|
"the Preferences-dialog to select any of the available "
|
|
|
|
"languages.").arg(error));
|
|
|
|
QMessageBox msgBox(QMessageBox::Warning,
|
|
|
|
tr("Cppcheck"),
|
|
|
|
msg,
|
|
|
|
QMessageBox::Ok);
|
|
|
|
msgBox.exec();
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:43:49 +02:00
|
|
|
QString TranslationHandler::getCurrentLanguage() const
|
2009-07-02 10:32:29 +02:00
|
|
|
{
|
|
|
|
return mCurrentLanguage;
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:43:49 +02:00
|
|
|
QString TranslationHandler::suggestLanguage() const
|
2009-07-02 22:41:37 +02:00
|
|
|
{
|
2014-01-23 06:32:41 +01:00
|
|
|
//Get language from system locale's name ie sv_SE or zh_CN
|
|
|
|
QString language = QLocale::system().name();
|
2009-07-02 22:41:37 +02:00
|
|
|
//qDebug()<<"Your language is"<<language;
|
|
|
|
|
|
|
|
//And see if we can find it from our list of language files
|
2017-07-28 13:43:49 +02: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
|
|
|
|
2017-07-28 13:43:49 +02:00
|
|
|
void TranslationHandler::addTranslation(const char *name, const char *filename)
|
2011-02-06 21:00:16 +01:00
|
|
|
{
|
|
|
|
TranslationInfo info;
|
|
|
|
info.mName = name;
|
|
|
|
info.mFilename = filename;
|
2014-01-23 06:32:41 +01:00
|
|
|
int codeLength = QString(filename).length() - QString(filename).indexOf('_') - 1;
|
|
|
|
info.mCode = QString(filename).right(codeLength);
|
2011-02-06 21:00:16 +01:00
|
|
|
mTranslations.append(info);
|
|
|
|
}
|
2011-02-07 11:30:13 +01:00
|
|
|
|
2017-07-28 13:43:49 +02:00
|
|
|
int TranslationHandler::getLanguageIndexByCode(const QString &code) const
|
2011-02-07 11:30:13 +01:00
|
|
|
{
|
|
|
|
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;
|
2014-01-23 19:40:47 +01:00
|
|
|
} else if (mTranslations[i].mCode == code.left(2)) {
|
2014-01-23 06:32:41 +01:00
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-07 11:30:13 +01:00
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|