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
qqmltranslation_p.h
Go to the documentation of this file.
1// Copyright (C) 2022 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#ifndef QQMLTRANSLATION_P_H
6#define QQMLTRANSLATION_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qtqmlglobal_p.h>
20
21#include <QtCore/qstring.h>
22#include <QtCore/qcoreapplication.h>
23
24QT_BEGIN_NAMESPACE
25
26class QQmlTranslation
27{
28public:
29 class QsTrData
30 {
31 QByteArray m_context;
32 QByteArray m_text;
33 QByteArray m_comment;
34 int m_number = 0;
35
36 public:
37 QsTrData(const QString &fileNameForContext, const QString &text, const QString &comment,
38 int number)
39 : m_context(fileNameForContext.toUtf8())
40 , m_text(text.toUtf8())
41 , m_comment(comment.toUtf8())
42 , m_number(number)
43 {
44 }
45
46 QString translate() const
47 {
48#if QT_CONFIG(translation)
49 return QCoreApplication::translate(m_context.constData(), m_text.constData(), m_comment.constData(), m_number);
50#else
51 return QString();
52#endif
53 }
54
55 QByteArray context() const { return m_context; }
56 QByteArray text() const { return m_text; }
57 QByteArray comment() const { return m_comment; }
58 int number() const { return m_number; }
59 };
60
61 class QsTrIdData
62 {
63 QByteArray m_id;
64 int m_number = 0;
65
66 public:
67 QsTrIdData(const QString &id, int number)
68 : m_id(id.toUtf8()), m_number(number)
69 {
70 }
71
72 QString translate() const
73 {
74#if QT_CONFIG(translation)
75 return qtTrId(m_id.constData(), m_number);
76#else
77 return QString();
78#endif
79 }
80
81 QByteArray id() const { return m_id; }
82 int number() const { return m_number; }
83 };
84
85 // The static analyzer hates std::monostate in std::variant because
86 // that results in various uninitialized memory "problems". Just use
87 // std::nullptr_t to indicate "empty".
88 using Data = std::variant<std::nullptr_t, QsTrData, QsTrIdData>;
89
90 QQmlTranslation(const Data &d) : m_data(d) {}
91 QQmlTranslation() : m_data(nullptr) {}
92
93 template<typename F>
94 auto visit(F &&f) const { return std::visit(std::forward<F>(f), m_data); }
95
96 QString translate() const
97 {
98 return visit(
99 [](auto &&arg) -> QString {
100 using T = std::decay_t<decltype(arg)>;
101 if constexpr (!std::is_same_v<T, std::nullptr_t>)
102 return arg.translate();
103 else {
104 Q_ASSERT_X(false, "QQmlTranslation", "Uninitialized Translation");
105 return {};
106 }
107 });
108 }
109
110 static QString contextFromQmlFilename(const QString &qmlFilename)
111 {
112 int lastSlash = qmlFilename.lastIndexOf(QLatin1Char('/'));
113 QStringView contextView = (lastSlash > -1)
114 ? QStringView{ qmlFilename }.mid(lastSlash + 1, qmlFilename.size() - lastSlash - 5)
115 : QStringView();
116 return contextView.toString();
117 }
118
119private:
120 Data m_data;
121};
122
123QT_END_NAMESPACE
124
125#endif // QQMLTRANSLATION_P_H