File: Translator.cpp

package info (click to toggle)
keepassx 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,396 kB
  • ctags: 2,957
  • sloc: cpp: 26,071; xml: 652; ansic: 600; python: 62; makefile: 10; sh: 7
file content (127 lines) | stat: -rw-r--r-- 3,889 bytes parent folder | download | duplicates (3)
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
/*
 *  Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
 *
 *  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 2 or (at your option)
 *  version 3 of the License.
 *
 *  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 "Translator.h"

#include <QCoreApplication>
#include <QDir>
#include <QLibraryInfo>
#include <QLocale>
#include <QRegExp>
#include <QTranslator>

#include "config-keepassx.h"
#include "core/Config.h"
#include "core/FilePath.h"

void Translator::installTranslator()
{
    QString language = config()->get("GUI/Language").toString();
    if (language == "system" || language.isEmpty()) {
        language = QLocale::system().name();
    }

    if (!installTranslator(language)) {
        // English fallback still needs translations for plurals
        if (!installTranslator("en_plurals")) {
            qWarning("Couldn't load translations.");
        }
    }

    installQtTranslator(language);

    availableLanguages();
}

QList<QPair<QString, QString> > Translator::availableLanguages()
{
    QStringList paths;
#ifdef QT_DEBUG
    paths.append(QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR));
#endif
    paths.append(filePath()->dataPath("translations"));

    QList<QPair<QString, QString> > languages;
    languages.append(QPair<QString, QString>("system", "System default"));

    QRegExp regExp("keepassx_([a-zA-Z_]+)\\.qm", Qt::CaseInsensitive, QRegExp::RegExp2);
    Q_FOREACH (const QString& path, paths) {
        Q_FOREACH (const QString& filename, QDir(path).entryList()) {
            if (regExp.exactMatch(filename)) {
                QString langcode = regExp.cap(1);
                if (langcode == "en_plurals") {
                    langcode = "en";
                }

                QLocale locale(langcode);
                QString languageStr = QLocale::languageToString(locale.language());
                QString countryStr;
                if (langcode.contains("_")) {
                    countryStr = QString(" (%1)").arg(QLocale::countryToString(locale.country()));
                }

                QPair<QString, QString> language(langcode, languageStr + countryStr);
                languages.append(language);
            }
        }
    }

    return languages;
}

bool Translator::installTranslator(const QString& language)
{
    QStringList paths;
#ifdef QT_DEBUG
    paths.append(QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR));
#endif
    paths.append(filePath()->dataPath("translations"));

    Q_FOREACH (const QString& path, paths) {
        if (installTranslator(language, path)) {
            return true;
        }
    }

    return false;
}

bool Translator::installTranslator(const QString& language, const QString& path)
{
    QTranslator* translator = new QTranslator(qApp);
    if (translator->load(QString("keepassx_").append(language), path)) {
        QCoreApplication::installTranslator(translator);
        return true;
    }
    else {
        delete translator;
        return false;
    }
}

bool Translator::installQtTranslator(const QString& language)
{
    QTranslator* qtTranslator = new QTranslator(qApp);
    if (qtTranslator->load(QString("%1/qt_%2").arg(QLibraryInfo::location(QLibraryInfo::TranslationsPath), language))) {
        QCoreApplication::installTranslator(qtTranslator);
        return true;
    }
    else {
        delete qtTranslator;
        return false;
    }
}