keepassxc

Форк
0
/
Translator.cpp 
135 строк · 4.7 Кб
1
/*
2
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
3
 *  Copyright (C) 2014 Felix Geyer <debfx@fobos.de>
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation, either version 2 or (at your option)
8
 *  version 3 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
#include "Translator.h"
20

21
#include <QCoreApplication>
22
#include <QDir>
23
#include <QLibraryInfo>
24
#include <QLocale>
25
#include <QRegularExpression>
26
#include <QTranslator>
27

28
#include "core/Resources.h"
29

30
/**
31
 * Install all KeePassXC and Qt translators.
32
 */
33
void Translator::installTranslators(const QString& uiLanguage)
34
{
35
    QStringList languages;
36
    if (uiLanguage.isEmpty() || uiLanguage == "system") {
37
        // NOTE: this is a workaround for the terrible way Qt loads languages
38
        // using the QLocale::uiLanguages() approach. Instead, we search each
39
        // language and all country variants in order before moving to the next.
40
        QLocale locale;
41
        languages = locale.uiLanguages();
42
    } else {
43
        languages << uiLanguage;
44
    }
45

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

49
    const auto path = resources()->dataPath("translations");
50
    installQtTranslator(languages, path);
51
    if (!installTranslator(languages, path)) {
52
        // couldn't load configured language or fallback
53
        qWarning("Couldn't load translations.");
54
    }
55
}
56

57
/**
58
 * Install KeePassXC translator.
59
 *
60
 * @param languages priority-ordered list of languages
61
 * @param path absolute search path
62
 * @return true on success
63
 */
64
bool Translator::installTranslator(const QStringList& languages, const QString& path)
65
{
66
    for (const auto& language : languages) {
67
        QLocale locale(language);
68
        QScopedPointer<QTranslator> translator(new QTranslator(qApp));
69
        if (translator->load(locale, "keepassxc_", "", path)) {
70
            return QCoreApplication::installTranslator(translator.take());
71
        } else if (translator->load(locale, "keepassxc_", "", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
72
            return QCoreApplication::installTranslator(translator.take());
73
        }
74
    }
75

76
    return false;
77
}
78

79
/**
80
 * Install Qt5 base translator from the specified local search path or the default system path
81
 * if no qtbase_* translations were found at the local path.
82
 *
83
 * @param languages priority-ordered list of languages
84
 * @param path absolute search path
85
 * @return true on success
86
 */
87
bool Translator::installQtTranslator(const QStringList& languages, const QString& path)
88
{
89
    for (const auto& language : languages) {
90
        QLocale locale(language);
91
        QScopedPointer<QTranslator> qtTranslator(new QTranslator(qApp));
92
        if (qtTranslator->load(locale, "qtbase_", "", path)) {
93
            return QCoreApplication::installTranslator(qtTranslator.take());
94
        } else if (qtTranslator->load(locale, "qtbase_", "", QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
95
            return QCoreApplication::installTranslator(qtTranslator.take());
96
        }
97
    }
98
    return false;
99
}
100

101
/**
102
 * @return list of pairs of available language codes and names
103
 */
104
QList<QPair<QString, QString>> Translator::availableLanguages()
105
{
106
    QList<QPair<QString, QString>> languages;
107
    languages.append(QPair<QString, QString>("system", "System default"));
108

109
    QRegularExpression regExp("^keepassxc_([a-zA-Z_]+)\\.qm$", QRegularExpression::CaseInsensitiveOption);
110
    const QStringList fileList = QDir(resources()->dataPath("translations")).entryList();
111
    for (const QString& filename : fileList) {
112
        QRegularExpressionMatch match = regExp.match(filename);
113
        if (match.hasMatch()) {
114
            QString langcode = match.captured(1);
115
            if (langcode == "en") {
116
                continue;
117
            }
118

119
            QLocale locale(langcode);
120
            QString languageStr = QLocale::languageToString(locale.language());
121
            if (langcode == "la") {
122
                // langcode "la" (Latin) is translated into "C" by QLocale::languageToString()
123
                languageStr = "Latin";
124
            }
125
            if (langcode.contains("_")) {
126
                languageStr += QString(" (%1)").arg(QLocale::countryToString(locale.country()));
127
            }
128

129
            QPair<QString, QString> language(langcode, languageStr);
130
            languages.append(language);
131
        }
132
    }
133

134
    return languages;
135
}
136

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.