Heart 1.4.984.b4712811
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
translatablestring.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2025-2027 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
9#include "crc32constexper.h"
10
11#include <QCoreApplication>
12namespace QH {
13
15 _data = QString::number(data);
16 _isKey = false;
17}
18
19TranslatableString::TranslatableString(const QString& data, bool isKey) {
20 _data = data;
21 _isKey = isKey;
22}
23
24TranslatableString::TranslatableString(const QList<TranslatableString> &pack) {
25 _pack = pack;
26}
27
29 return _data == other._data && _isKey == other._isKey && _args == other._args && _pack == other._pack;
30}
31
33 return true;
34}
35
37 return _data.isEmpty() && _pack.isEmpty();
38}
39
40QString TranslatableString::text() const {
41 if (_pack.size()) {
42 QString result = _pack.first().text();
43 for (int i = 1; i < _pack.size(); i++) {
44 result += ", " + _pack[i].text();
45 }
46 return result;
47 }
48
49 if (_isKey) {
50 QString result = QCoreApplication::translate("TranslatableString", _data.toLatin1());
51 for (const auto& arg: _args) {
52 result = result.arg(arg.text());
53 }
54
55 return result;
56 }
57
58 return _data;
59}
60
61const QString &TranslatableString::rawText() const {
62 return _data;
63}
64
66 if (_data.isEmpty())
67 return 0;
68 return QuasarAppUtils::calculateCrc32(_data.toLatin1(), _data.size());
69}
70
72 return {source, true};
73}
74
75QDataStream &TranslatableString::fromStream(QDataStream &stream) {
76 versionHeader(0, stream);
77
78 readWrite(stream, _isKey);
79 readWrite(stream, _data);
80 readWrite(stream, _args);
81 readWrite(stream, _pack);
82
83 return stream;
84}
85
86QDataStream &TranslatableString::toStream(QDataStream &stream) const {
87 versionHeader(0, stream);
88
89 readWrite(stream, _isKey);
90 readWrite(stream, _data);
91 readWrite(stream, _args);
92 readWrite(stream, _pack);
93
94 return stream;
95}
96
97TranslatableString &TranslatableString::setArgs(const QList<TranslatableString> &newArgs) {
98 _args = newArgs;
99
100 return *this;
101}
102
104 _args.push_back(newArg);
105
106 return *this;
107}
108
110 _args.push_back({newArg});
111 return *this;
112}
113}
char versionHeader(char version, QDataStream &stream)
versionHeader this is new function that automaticaly choose what shuld be to-do (read/wirte).
void readWrite(QDataStream &stream, const T &data) const
readWrite this method automaticaly choose what we need to-do read or write.
static TranslatableString tr(const QString &source)
tr this method create a string translation wrapper and save translation key for lupdate tool
TranslatableString & setArgs(const QList< TranslatableString > &newArgs)
setArgs this method set args for translation.
QDataStream & toStream(QDataStream &stream) const override
fromStream This method should be write all members of the current object to the stream object.
QString text() const
text this method return translated text.
bool operator==(const TranslatableString &other) const
bool isValid() const override
const QString & rawText() const
rawText return raw key source.
QDataStream & fromStream(QDataStream &stream) override
fromStream This method should be read all bytes from the stream object and full the current object.
uint32_t calcCrc32Hash() const
calcCrc32Hash this method calc hash from raw text.
TranslatableString & arg(const TranslatableString &newArg)
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13