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
6QQmlTranslation::QQmlTranslation(const Data &d) : data(d) { }
7QQmlTranslation::QQmlTranslation() : data(nullptr) { }
8
9QString QQmlTranslation::translate() const
10{
11 return std::visit(
12 visitor: [](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 variants: data);
22}
23
24QString QQmlTranslation::serializeForQmltc() const
25{
26 return std::visit(
27 visitor: [](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 variants: data);
37}
38
39QString QQmlTranslation::idForQmlDebug() const
40{
41 return std::visit(
42 visitor: [](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 variants: data);
52}
53
54QQmlTranslation::QsTrData::QsTrData(const QString &context, const QString &text,
55 const QString &comment, int number)
56 : context(context.toUtf8()), text(text.toUtf8()), comment(comment.toUtf8()), number(number)
57{
58}
59
60QString QQmlTranslation::contextFromQmlFilename(const QString &qmlFilename)
61{
62 int lastSlash = qmlFilename.lastIndexOf(c: QLatin1Char('/'));
63 QStringView contextView = (lastSlash > -1)
64 ? QStringView{ qmlFilename }.mid(pos: lastSlash + 1, n: qmlFilename.size() - lastSlash - 5)
65 : QStringView();
66 return contextView.toString();
67}
68
69QString QQmlTranslation::QsTrData::translate() const
70{
71#if !QT_CONFIG(translation)
72 return QString();
73#else
74 return QCoreApplication::translate(context, key: text, disambiguation: comment, n: number);
75#endif
76}
77
78QString QQmlTranslation::QsTrData::idForQmlDebug() const
79{
80 return QString::fromUtf8(ba: text);
81}
82
83QString QQmlTranslation::QsTrData::serializeForQmltc() const
84{
85 QString result = QStringLiteral(R"(QQmlTranslation(QQmlTranslation::QsTrData(
86 QStringLiteral("%1"),
87 QStringLiteral("%2"),
88 QStringLiteral("%3"),
89 %4)))")
90 .arg(args: QString::fromUtf8(ba: context), args: QString::fromUtf8(ba: text),
91 args: QString::fromUtf8(ba: comment))
92 .arg(a: number);
93
94 return result;
95}
96
97QQmlTranslation::QsTrIdData::QsTrIdData(const QString &id, int number)
98 : id(id.toUtf8()), number(number)
99{
100}
101
102QString QQmlTranslation::QsTrIdData::translate() const
103{
104#if !QT_CONFIG(translation)
105 return QString();
106#else
107 return qtTrId(id, n: number);
108#endif
109}
110
111QString QQmlTranslation::QsTrIdData::serializeForQmltc() const
112{
113 QString result = QStringLiteral(R"(QQmlTranslation(QQmlTranslation::QsTrIdData(
114 QStringLiteral("%1"),
115 %4)))")
116 .arg(a: QString::fromUtf8(ba: id))
117 .arg(a: number);
118
119 return result;
120}
121
122QString QQmlTranslation::QsTrIdData::idForQmlDebug() const
123{
124 return QString::fromUtf8(ba: id);
125}
126

source code of qtdeclarative/src/qml/common/qqmltranslation.cpp