Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqmltranslation.cpp
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#include "private/qqmltranslation_p.h"
5
8
10{
11 return std::visit(
12 [](auto &&arg) -> QString {
13 using T = std::decay_t<decltype(arg)>;
14 if constexpr (!std::is_same_v<T, std::nullptr_t>)
15 return arg.translate();
16 else {
17 Q_ASSERT_X(false, "QQmlTranslation", "Uninitialized Translation");
18 return {};
19 }
20 },
21 data);
22}
23
25{
26 return std::visit(
27 [](auto &&arg) -> QString {
28 using T = std::decay_t<decltype(arg)>;
29 if constexpr (!std::is_same_v<T, std::nullptr_t>)
30 return arg.serializeForQmltc();
31 else {
32 Q_ASSERT_X(false, "QQmlTranslation", "Uninitialized Translation");
33 return {};
34 }
35 },
36 data);
37}
38
40{
41 return std::visit(
42 [](auto &&arg) -> QString {
43 using T = std::decay_t<decltype(arg)>;
44 if constexpr (!std::is_same_v<T, std::nullptr_t>)
45 return arg.idForQmlDebug();
46 else {
47 Q_ASSERT_X(false, "QQmlTranslation", "Uninitialized Translation");
48 return {};
49 }
50 },
51 data);
52}
53
55 const QString &comment, int number)
56 : context(context.toUtf8()), text(text.toUtf8()), comment(comment.toUtf8()), number(number)
57{
58}
59
61{
62 int lastSlash = qmlFilename.lastIndexOf(QLatin1Char('/'));
63 QStringView contextView = (lastSlash > -1)
64 ? QStringView{ qmlFilename }.mid(lastSlash + 1, qmlFilename.size() - lastSlash - 5)
65 : QStringView();
66 return contextView.toString();
67}
68
70{
71#if !QT_CONFIG(translation)
72 return QString();
73#else
75#endif
76}
77
79{
80 return QString::fromUtf8(text);
81}
82
84{
85 QString result = QStringLiteral(R"(QQmlTranslation(QQmlTranslation::QsTrData(
86 QStringLiteral("%1"),
87 QStringLiteral("%2"),
88 QStringLiteral("%3"),
89 %4)))")
91 QString::fromUtf8(comment))
92 .arg(number);
93
94 return result;
95}
96
98 : id(id.toUtf8()), number(number)
99{
100}
101
103{
104#if !QT_CONFIG(translation)
105 return QString();
106#else
107 return qtTrId(id, number);
108#endif
109}
110
112{
113 QString result = QStringLiteral(R"(QQmlTranslation(QQmlTranslation::QsTrIdData(
114 QStringLiteral("%1"),
115 %4)))")
116 .arg(QString::fromUtf8(id))
117 .arg(number);
118
119 return result;
120}
121
123{
124 return QString::fromUtf8(id);
125}
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
QsTrData(const QString &fileNameForContext, const QString &text, const QString &comment, int number)
QString serializeForQmltc() const
QsTrIdData(const QString &id, int number)
QString idForQmlDebug() const
QString serializeForQmltc() const
static QString contextFromQmlFilename(const QString &qmlFilename)
std::variant< std::nullptr_t, QsTrData, QsTrIdData > Data
QString translate() const
\inmodule QtCore
Definition qstringview.h:76
QString toString() const
Returns a deep copy of this string view's data as a QString.
Definition qstring.h:1014
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:279
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5857
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
Definition qstring.cpp:5204
QString text
static void * context
GLenum GLuint id
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint64EXT * result
[6]
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
SSL_CTX int(*) void arg)
#define QStringLiteral(str)
Q_CORE_EXPORT QString qtTrId(const char *id, int n=-1)
QObject::connect nullptr
\inmodule QtCore \reentrant
Definition qchar.h:17