Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "proxytranslator.h"
5
6#include <QtCore/qlibraryinfo.h>
7
9
14
19
20bool ProxyTranslator::hasTranslation(const TranslationBindingInformation &translationBindingInformation) const
21{
22 resetTranslationFound();
23 translationFromInformation(translationBindingInformation);
24 return translationFound();
25}
26
27QString ProxyTranslator::translationFromInformation(const TranslationBindingInformation &info)
28{
29 return info.translation.translate();
30}
31
32QQmlSourceLocation ProxyTranslator::sourceLocationFromInformation(const TranslationBindingInformation &translationBindingInformation)
33{
34 return QQmlSourceLocation(translationBindingInformation.compilationUnit->fileName(),
35 translationBindingInformation.line,
36 translationBindingInformation.column);
37}
38
39
41{
42 m_enable = true;
43 m_currentUILanguages = locale.uiLanguages().join(QLatin1Char(' '));
44
45 m_qtTranslator.reset(new QTranslator());
46 if (!m_qtTranslator->load(locale, QLatin1String("qt"), QLatin1String("_"),
48 m_qtTranslator.reset();
49 }
50
51 m_qmlTranslator.reset(new QTranslator(this));
52 if (!m_qmlTranslator->load(locale, QLatin1String("qml"), QLatin1String("_"),
53 context.toLocalFile() + QLatin1String("/i18n"))) {
54 m_qmlTranslator.reset();
55 }
56
57 // unfortunately setUiLanguage set new translators, so do this first
58 for (QQmlEngine *engine : std::as_const(m_engines))
59 engine->setUiLanguage(locale.bcp47Name());
60
61 // make sure proxy translator is the first used translator
64
65 for (QQmlEngine *engine : std::as_const(m_engines)) {
66 // have two retranslate runs to get elided warning even the same language was set
67 m_enable = false;
68 engine->retranslate();
69 m_enable = true;
70 engine->retranslate();
71 }
72 emit languageChanged(locale);
73}
74
75QString ProxyTranslator::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const
76{
77 if (!m_enable)
78 return {};
80 if (result.isNull() && m_qtTranslator)
81 result = m_qtTranslator->translate(context, sourceText, disambiguation, n);
82 if (result.isNull() && m_qmlTranslator)
83 result = m_qmlTranslator->translate(context, sourceText, disambiguation, n);
84 m_translationFound = !(result.isNull() || result.isEmpty() || result == sourceText);
85 return result;
86}
87
88void ProxyTranslator::resetTranslationFound() const
89{
90 m_translationFound = false;
91}
92
93bool ProxyTranslator::translationFound() const
94{
95 return m_translationFound;
96}
97
99{
100 if (m_qtTranslator && m_qtTranslator->isEmpty())
101 return false;
102 if (m_qmlTranslator && m_qmlTranslator->isEmpty())
103 return false;
104 return true;
105}
106
108{
109 return m_currentUILanguages;
110}
111
113
114#include "moc_proxytranslator.cpp"
bool isEmpty() const override
Returns true if this translator is empty, otherwise returns false.
static QString translationFromInformation(const TranslationBindingInformation &translationBindingInformation)
void removeEngine(QQmlEngine *engine)
static QQmlSourceLocation sourceLocationFromInformation(const TranslationBindingInformation &translationBindingInformation)
void languageChanged(const QLocale &locale)
bool hasTranslation(const TranslationBindingInformation &translationBindingInformation) const
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n) const override
Returns the translation for the key (context, sourceText, disambiguation).
void addEngine(QQmlEngine *engine)
void setLanguage(const QUrl &context, const QLocale &locale)
QString currentUILanguages() const
static bool installTranslator(QTranslator *messageFile)
Adds the translation file translationFile to the list of translation files to be used for translation...
static bool removeTranslator(QTranslator *messageFile)
Removes the translation file translationFile from the list of translation files used by this applicat...
void setUiLanguage(const QString &language)
static QString path(LibraryPath p)
bool removeOne(const AT &t)
Definition qlist.h:598
void append(parameter_type t)
Definition qlist.h:458
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QTranslator(QObject *parent=nullptr)
Constructs an empty message file object with parent parent that is not connected to any file.
\inmodule QtCore
Definition qurl.h:94
Combined button and popup list for selecting options.
static void * context
GLfloat n
GLuint64EXT * result
[6]
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define emit
QHostInfo info
[0]
QJSEngine engine
[0]
\inmodule QtCore \reentrant
Definition qchar.h:18