1 /*
2   translator.cpp
3 
4   This file is part of GammaRay, the Qt application inspection and
5   manipulation tool.
6 
7   Copyright (C) 2016-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8   Author: Volker Krause <volker.krause@kdab.com>
9 
10   Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11   accordance with GammaRay Commercial License Agreement provided with the Software.
12 
13   Contact info@kdab.com if any conditions of this licensing are not clear to you.
14 
15   This program is free software; you can redistribute it and/or modify
16   it under the terms of the GNU General Public License as published by
17   the Free Software Foundation, either version 2 of the License, or
18   (at your option) any later version.
19 
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24 
25   You should have received a copy of the GNU General Public License
26   along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #include <config-gammaray.h>
30 
31 #include "translator.h"
32 #include "paths.h"
33 
34 #include <compat/qasconst.h>
35 
36 #include <QCoreApplication>
37 #include <QDebug>
38 #include <QLibraryInfo>
39 #include <QLocale>
40 #include <QDir>
41 #include <QTranslator>
42 
43 using namespace GammaRay;
44 
rootTranslationsPath()45 static QString rootTranslationsPath() {
46     return Paths::rootPath() + QLatin1Char('/') + GAMMARAY_TRANSLATION_INSTALL_DIR;
47 }
48 
qtTranslationsPath()49 static QString qtTranslationsPath() {
50     return QLibraryInfo::location(QLibraryInfo::TranslationsPath);
51 }
52 
loadTranslations(const QString & catalog,const QString & path,const QString & overrideLanguage)53 void Translator::loadTranslations(const QString &catalog, const QString &path, const QString &overrideLanguage)
54 {
55     const QDir dir(path);
56     const QLocale locale;
57     QStringList names = locale.uiLanguages();
58     if (!overrideLanguage.isEmpty())
59         names.prepend(overrideLanguage);
60 
61     for (const QString &name : qAsConst(names)) {
62         const QLocale uiLocale(name);
63         auto translator = new QTranslator(QCoreApplication::instance());
64         if (translator->load(uiLocale, catalog, QStringLiteral("_"), path)) {
65             QCoreApplication::instance()->installTranslator(translator);
66             return;
67         } else {
68             delete translator;
69 
70             foreach (const QString &name, uiLocale.uiLanguages()) {
71                 const QString fileName = QString("%1_%2.qm").arg(catalog, name);
72                 const QString filePath = dir.filePath(fileName);
73                 if (QFile::exists(filePath)) {
74                     return;
75                 }
76             }
77         }
78     }
79 
80     if (locale.language() != QLocale::C && locale.language() != QLocale::English)
81         qDebug() << "did not find a translation for" << catalog << "in" << path
82                  << "for language" << locale.name();
83 }
84 
loadGammaRayTranslations(const QString & overrideLanguage)85 void Translator::loadGammaRayTranslations(const QString &overrideLanguage)
86 {
87     loadTranslations(QStringLiteral("gammaray"), rootTranslationsPath(), overrideLanguage);
88 }
89 
loadStandAloneTranslations(const QString & overrideLanguage)90 void Translator::loadStandAloneTranslations(const QString &overrideLanguage)
91 {
92     loadGammaRayTranslations(overrideLanguage);
93     loadTranslations(QStringLiteral("qt"), qtTranslationsPath(), overrideLanguage);
94 }
95 
96 
97