File: translationhandler.cpp

package info (click to toggle)
cppcheck 1.76.1-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 13,608 kB
  • sloc: cpp: 140,390; python: 3,430; ansic: 2,808; xml: 670; makefile: 501; sh: 237
file content (210 lines) | stat: -rw-r--r-- 6,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Cppcheck - A tool for static C/C++ code analysis
 * Copyright (C) 2007-2016 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 <QApplication>
#include <QFile>
#include <QDebug>
#include <QLocale>
#include <QMessageBox>
#include <QSettings>
#include <QFileInfo>
#include "translationhandler.h"


// Provide own translations for standard buttons. This (garbage) code is needed to enforce them to appear in .ts files even after "lupdate gui.pro"
static void unused()
{
#if ((QT_VERSION >= 0x040000)&&(QT_VERSION < 0x050000))
    Q_UNUSED(QT_TRANSLATE_NOOP("QDialogButtonBox", "OK"));
    Q_UNUSED(QT_TRANSLATE_NOOP("QDialogButtonBox", "Cancel"));
    Q_UNUSED(QT_TRANSLATE_NOOP("QDialogButtonBox", "Close"));
    Q_UNUSED(QT_TRANSLATE_NOOP("QDialogButtonBox", "Save"));
#elif ((QT_VERSION >= 0x050000)&&(QT_VERSION < 0x060000))
    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"));
#else
#error Unsupported Qt version.
#endif
}

TranslationHandler::TranslationHandler(QObject *parent) :
    QObject(parent),
    mCurrentLanguage("en"),
    mTranslator(NULL)
{
    // Add our available languages
    // Keep this list sorted
    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");
}

TranslationHandler::~TranslationHandler()
{
}

const QStringList TranslationHandler::GetNames() const
{
    QStringList names;
    foreach (TranslationInfo translation, mTranslations) {
        names.append(translation.mName);
    }
    return names;
}

bool TranslationHandler::SetLanguage(const QString &code)
{
    bool failure = false;
    QString error;

    //If English is the language
    if (code == "en") {
        //Just remove all extra translators
        if (mTranslator) {
            qApp->removeTranslator(mTranslator);
            delete mTranslator;
            mTranslator = NULL;
        }

        mCurrentLanguage = code;
        return true;
    }

    //Make sure the translator is otherwise valid
    int index = GetLanguageIndexByCode(code);
    if (index == -1) {
        error = QObject::tr("Unknown language specified!");
        failure = true;
    }

    // Make sure there is a translator
    if (!mTranslator && !failure)
        mTranslator = new QTranslator(this);

    //Load the new language
    const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();

    QSettings settings;
    QString datadir = settings.value("DATADIR").toString();
    if (datadir.isEmpty())
        datadir = appPath;

    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) {
        translationFile += ".qm";
        //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);
        error = error.arg(translationFile);
    }

    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();
        return false;
    }

    qApp->installTranslator(mTranslator);

    mCurrentLanguage = code;

    return true;
}

QString TranslationHandler::GetCurrentLanguage() const
{
    return mCurrentLanguage;
}

QString TranslationHandler::SuggestLanguage() const
{
    //Get language from system locale's name ie sv_SE or zh_CN
    QString language = QLocale::system().name();
    //qDebug()<<"Your language is"<<language;

    //And see if we can find it from our list of language files
    int index = GetLanguageIndexByCode(language);

    //If nothing found, return English
    if (index < 0) {
        return "en";
    }

    return language;
}

void TranslationHandler::AddTranslation(const char *name, const char *filename)
{
    TranslationInfo info;
    info.mName = name;
    info.mFilename = filename;
    int codeLength = QString(filename).length() - QString(filename).indexOf('_') - 1;
    info.mCode = QString(filename).right(codeLength);
    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;
        } else if (mTranslations[i].mCode == code.left(2)) {
            index = i;
            break;
        }
    }
    return index;
}