Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Translator.cpp « core « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e3f568cbe2dd950f8cb066ab1172edc011a8b44 (plain)
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
/*
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
 *  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 <QRegularExpression>
#include <QTranslator>

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

/**
 * Install all KeePassXC and Qt translators.
 */
void Translator::installTranslators()
{
    QStringList languages;
    QString languageSetting = config()->get("GUI/Language").toString();
    if (languageSetting.isEmpty() || languageSetting == "system") {
        // NOTE: this is a workaround for the terrible way Qt loads languages
        // using the QLocale::uiLanguages() approach. Instead, we search each
        // language and all country variants in order before moving to the next.
        QLocale locale;
        languages = locale.uiLanguages();
    } else {
        languages << languageSetting;
    }

    // Always try to load english last
    languages << "en_US";

    const QStringList paths = {
#ifdef QT_DEBUG
        QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR),
#endif
        filePath()->dataPath("translations")};

    bool translationsLoaded = false;
    for (const QString& path : paths) {
        installQtTranslator(languages, path);
        if (installTranslator(languages, path)) {
            translationsLoaded = true;
            break;
        }
    }

    if (!translationsLoaded) {
        // couldn't load configured language or fallback
        qWarning("Couldn't load translations.");
    }
}

/**
 * Install KeePassXC translator.
 *
 * @param languages priority-ordered list of languages
 * @param path absolute search path
 * @return true on success
 */
bool Translator::installTranslator(const QStringList& languages, const QString& path)
{
    for (const auto& language : languages) {
        QLocale locale(language);
        QScopedPointer<QTranslator> translator(new QTranslator(qApp));
        if (translator->load(locale, "keepassx_", "", path)) {
            return QCoreApplication::installTranslator(translator.take());
        }
    }

    return false;
}

/**
 * Install Qt5 base translator from the specified local search path or the default system path
 * if no qtbase_* translations were found at the local path.
 *
 * @param languages priority-ordered list of languages
 * @param path absolute search path
 * @return true on success
 */
bool Translator::installQtTranslator(const QStringList& languages, const QString& path)
{
    for (const auto& language : languages) {
        QLocale locale(language);
        QScopedPointer<QTranslator> qtTranslator(new QTranslator(qApp));
        if (qtTranslator->load(locale, "qtbase_", "", path)) {
            return QCoreApplication::installTranslator(qtTranslator.take());
        } else if (qtTranslator->load(locale, "qtbase_", "", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
            return QCoreApplication::installTranslator(qtTranslator.take());
        }
    }
    return false;
}

/**
 * @return list of pairs of available language codes and names
 */
QList<QPair<QString, QString>> Translator::availableLanguages()
{
    const QStringList paths = {
#ifdef QT_DEBUG
        QString("%1/share/translations").arg(KEEPASSX_BINARY_DIR),
#endif
        filePath()->dataPath("translations")};

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

    QRegularExpression regExp("^keepassx_([a-zA-Z_]+)\\.qm$", QRegularExpression::CaseInsensitiveOption);
    for (const QString& path : paths) {
        const QStringList fileList = QDir(path).entryList();
        for (const QString& filename : fileList) {
            QRegularExpressionMatch match = regExp.match(filename);
            if (match.hasMatch()) {
                QString langcode = match.captured(1);
                if (langcode == "en") {
                    continue;
                }

                QLocale locale(langcode);
                QString languageStr = QLocale::languageToString(locale.language());
                if (langcode == "la") {
                    // langcode "la" (Latin) is translated into "C" by QLocale::languageToString()
                    languageStr = "Latin";
                }
                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;
}