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