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 if (!m_qtTranslator->load(locale, QLatin1String("qt"), QLatin1String("_"),
48 QLibraryInfo::path(QLibraryInfo::TranslationsPath))) {
49 m_qtTranslator.reset();
50 }
51
52 m_qmlTranslator.reset(new QTranslator(this));
53 if (!m_qmlTranslator->load(locale, QLatin1String("qml"), QLatin1String("_"),
54 context.toLocalFile() + QLatin1String("/i18n"))) {
55 m_qmlTranslator.reset();
56 }
57
58 // unfortunately setUiLanguage set new translators, so do this first
59 for (QQmlEngine *engine : std::as_const(m_engines))
60 engine->setUiLanguage(locale.bcp47Name());
61
62 // make sure proxy translator is the first used translator
63 QCoreApplication::removeTranslator(this);
64 QCoreApplication::installTranslator(this);
65
66 for (QQmlEngine *engine : std::as_const(m_engines)) {
67 // have two retranslate runs to get elided warning even the same language was set
68 m_enable = false;
69 engine->retranslate();
70 m_enable = true;
71 engine->retranslate();
72 }
73 emit languageChanged(locale);
74}
75
76QString ProxyTranslator::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const
77{
78 if (!m_enable)
79 return {};
80 QString result;
81 if (result.isNull() && m_qtTranslator)
82 result = m_qtTranslator->translate(context, sourceText, disambiguation, n);
83 if (result.isNull() && m_qmlTranslator)
84 result = m_qmlTranslator->translate(context, sourceText, disambiguation, n);
85 m_translationFound = !(result.isNull() || result.isEmpty() || result == sourceText);
86 return result;
87}
88
89void ProxyTranslator::resetTranslationFound() const
90{
91 m_translationFound = false;
92}
93
94bool ProxyTranslator::translationFound() const
95{
96 return m_translationFound;
97}
98
99bool ProxyTranslator::isEmpty() const
100{
101 if (m_qtTranslator && m_qtTranslator->isEmpty())
102 return false;
103 if (m_qmlTranslator && m_qmlTranslator->isEmpty())
104 return false;
105 return true;
106}
107
108QString ProxyTranslator::currentUILanguages() const
109{
110 return m_currentUILanguages;
111}
112
113QT_END_NAMESPACE
114
115#include "moc_proxytranslator.cpp"
Combined button and popup list for selecting options.