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
8QT_BEGIN_NAMESPACE
9
10void ProxyTranslator::addEngine(QQmlEngine *engine)
11{
12 m_engines.append(t: engine);
13}
14
15void ProxyTranslator::removeEngine(QQmlEngine *engine)
16{
17 m_engines.removeOne(t: engine);
18}
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
40void ProxyTranslator::setLanguage(const QUrl &context, const QLocale &locale)
41{
42 m_enable = true;
43 m_currentUILanguages = locale.uiLanguages().join(sep: QLatin1Char(' '));
44
45 m_qtTranslator.reset(p: new QTranslator());
46 if (!m_qtTranslator->load(locale, filename: QLatin1String("qt"), prefix: QLatin1String("_"),
47 directory: QLibraryInfo::path(p: QLibraryInfo::TranslationsPath))) {
48 m_qtTranslator.reset();
49 }
50
51 m_qmlTranslator.reset(p: new QTranslator(this));
52 if (!m_qmlTranslator->load(locale, filename: QLatin1String("qml"), prefix: QLatin1String("_"),
53 directory: 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(t&: m_engines))
59 engine->setUiLanguage(locale.bcp47Name());
60
61 // make sure proxy translator is the first used translator
62 QCoreApplication::removeTranslator(messageFile: this);
63 QCoreApplication::installTranslator(messageFile: this);
64
65 for (QQmlEngine *engine : std::as_const(t&: 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 {};
79 QString result;
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
98bool ProxyTranslator::isEmpty() const
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
107QString ProxyTranslator::currentUILanguages() const
108{
109 return m_currentUILanguages;
110}
111
112QT_END_NAMESPACE
113
114#include "moc_proxytranslator.cpp"
115

source code of qtdeclarative/src/plugins/qmltooling/qmldbg_preview/proxytranslator.cpp