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