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