Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
proxytranslator.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
7#include <QtCore/qlibraryinfo.h>
8
10
11void ProxyTranslator::addEngine(QQmlEngine *engine)
12{
13 m_engines.append(engine);
14}
15
16void ProxyTranslator::removeEngine(QQmlEngine *engine)
17{
18 m_engines.removeOne(engine);
19}
20
21bool ProxyTranslator::hasTranslation(const TranslationBindingInformation &translationBindingInformation) const
22{
23 resetTranslationFound();
24 translationFromInformation(translationBindingInformation);
25 return translationFound();
26}
27
28QString ProxyTranslator::translationFromInformation(const TranslationBindingInformation &info)
29{
30 return info.translation.translate();
31}
32
33QQmlSourceLocation ProxyTranslator::sourceLocationFromInformation(const TranslationBindingInformation &translationBindingInformation)
34{
35 return QQmlSourceLocation(translationBindingInformation.compilationUnit->fileName(),
36 translationBindingInformation.line,
37 translationBindingInformation.column);
38}
39
40
41void ProxyTranslator::setLanguage(const QUrl &context, const QLocale &locale)
42{
43 m_enable = true;
44 m_currentUILanguages = locale.uiLanguages().join(QLatin1Char(' '));
45
46 m_qtTranslator.reset(new QTranslator());
47 const QStringList qtPaths = QLibraryInfo::paths(QLibraryInfo::TranslationsPath);
48 bool ok = false;
49 for (const QString &path : qtPaths) {
50 ok = m_qtTranslator->load(locale, QLatin1String("qt"), QLatin1String("_"), path);
51 if (ok)
52 break;
53 }
54 if (!ok)
55 m_qtTranslator.reset();
56
57 m_qmlTranslator.reset(new QTranslator(this));
58 if (!m_qmlTranslator->load(locale, QLatin1String("qml"), QLatin1String("_"),
59 context.toLocalFile() + QLatin1String("/i18n"))) {
60 m_qmlTranslator.reset();
61 }
62
63 // unfortunately setUiLanguage set new translators, so do this first
64 for (QQmlEngine *engine : std::as_const(m_engines))
65 engine->setUiLanguage(locale.bcp47Name());
66
67 // make sure proxy translator is the first used translator
68 QCoreApplication::removeTranslator(this);
69 QCoreApplication::installTranslator(this);
70
71 for (QQmlEngine *engine : std::as_const(m_engines)) {
72 // have two retranslate runs to get elided warning even the same language was set
73 m_enable = false;
74 engine->retranslate();
75 m_enable = true;
76 engine->retranslate();
77 }
78 emit languageChanged(locale);
79}
80
81QString ProxyTranslator::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const
82{
83 if (!m_enable)
84 return {};
85 QString result;
86 if (result.isNull() && m_qtTranslator)
87 result = m_qtTranslator->translate(context, sourceText, disambiguation, n);
88 if (result.isNull() && m_qmlTranslator)
89 result = m_qmlTranslator->translate(context, sourceText, disambiguation, n);
90 m_translationFound = !(result.isNull() || result.isEmpty() || result == sourceText);
91 return result;
92}
93
94void ProxyTranslator::resetTranslationFound() const
95{
96 m_translationFound = false;
97}
98
99bool ProxyTranslator::translationFound() const
100{
101 return m_translationFound;
102}
103
104bool ProxyTranslator::isEmpty() const
105{
106 if (m_qtTranslator && m_qtTranslator->isEmpty())
107 return false;
108 if (m_qmlTranslator && m_qmlTranslator->isEmpty())
109 return false;
110 return true;
111}
112
113QString ProxyTranslator::currentUILanguages() const
114{
115 return m_currentUILanguages;
116}
117
118QT_END_NAMESPACE
119
120#include "moc_proxytranslator.cpp"
Combined button and popup list for selecting options.