1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef TRANSLATORMESSAGE_H
5#define TRANSLATORMESSAGE_H
6
7#include <QString>
8#include <QStringList>
9#include <QHash>
10
11
12QT_BEGIN_NAMESPACE
13
14enum TranslatorSaveMode { SaveEverything, SaveStripped };
15
16class TranslatorMessage
17{
18public:
19 enum Type { Unfinished, Finished, Vanished, Obsolete };
20 typedef QHash<QString, QString> ExtraData;
21 class Reference
22 {
23 QString m_fileName;
24 int m_lineNumber;
25 public:
26 Reference(const QString &n, int l) : m_fileName(n), m_lineNumber(l) {}
27 bool operator==(const Reference &other) const
28 { return fileName() == other.fileName() && lineNumber() == other.lineNumber(); }
29 QString fileName() const { return m_fileName; }
30 int lineNumber() const { return m_lineNumber; }
31 };
32 typedef QList<Reference> References;
33
34 TranslatorMessage();
35 TranslatorMessage(const QString &context, const QString &sourceText,
36 const QString &comment, const QString &userData,
37 const QString &fileName, int lineNumber,
38 const QStringList &translations = QStringList(),
39 Type type = Unfinished, bool plural = false);
40
41 uint hash() const;
42
43 QString id() const { return m_id; }
44 void setId(const QString &id) { m_id = id; }
45
46 QString context() const { return m_context; }
47 void setContext(const QString &context) { m_context = context; }
48
49 QString sourceText() const { return m_sourcetext; }
50 void setSourceText(const QString &sourcetext) { m_sourcetext = sourcetext; }
51 QString oldSourceText() const { return m_oldsourcetext; }
52 void setOldSourceText(const QString &oldsourcetext) { m_oldsourcetext = oldsourcetext; }
53
54 QString comment() const { return m_comment; }
55 void setComment(const QString &comment) { m_comment = comment; }
56 QString oldComment() const { return m_oldcomment; }
57 void setOldComment(const QString &oldcomment) { m_oldcomment = oldcomment; }
58
59 QStringList translations() const { return m_translations; }
60 void setTranslations(const QStringList &translations) { m_translations = translations; }
61 QString translation() const { return m_translations.value(i: 0); }
62 void setTranslation(const QString &translation) { m_translations = QStringList(translation); }
63 void appendTranslation(const QString &translation) { m_translations.append(t: translation); }
64 bool isTranslated() const
65 {
66 for (const QString &trans : m_translations)
67 if (!trans.isEmpty())
68 return true;
69 return false;
70 }
71
72 QString fileName() const { return m_fileName; }
73 void setFileName(const QString &fileName) { m_fileName = fileName; }
74 int lineNumber() const { return m_lineNumber; }
75 void setLineNumber(int lineNumber) { m_lineNumber = lineNumber; }
76 int tsLineNumber() const { return m_tsLineNumber; }
77 void setTsLineNumber(int lineNumber) { m_tsLineNumber = lineNumber; }
78 void clearReferences();
79 void setReferences(const References &refs);
80 void addReference(const QString &fileName, int lineNumber);
81 void addReference(const Reference &ref) { addReference(fileName: ref.fileName(), lineNumber: ref.lineNumber()); }
82 void addReferenceUniq(const QString &fileName, int lineNumber);
83 References extraReferences() const { return m_extraRefs; }
84 References allReferences() const;
85 QString userData() const { return m_userData; }
86 void setUserData(const QString &userData) { m_userData = userData; }
87 QString extraComment() const { return m_extraComment; }
88 void setExtraComment(const QString &extraComment) { m_extraComment = extraComment; }
89 QString translatorComment() const { return m_translatorComment; }
90 void setTranslatorComment(const QString &translatorComment) { m_translatorComment = translatorComment; }
91 QString warning() const {return m_warning;}
92 void setWarning(const QString &warning) {m_warning = warning;}
93
94
95 bool isNull() const { return m_sourcetext.isNull() && m_lineNumber == -1 && m_translations.isEmpty(); }
96
97 Type type() const { return m_type; }
98 void setType(Type t) { m_type = t; }
99 bool isPlural() const { return m_plural; }
100 void setPlural(bool isplural) { m_plural = isplural; }
101
102 // note: use '<fileformat>:' as prefix for file format specific members,
103 // e.g. "po-msgid_plural"
104 QString extra(const QString &ba) const;
105 void setExtra(const QString &ba, const QString &var);
106 bool hasExtra(const QString &ba) const;
107 const ExtraData &extras() const { return m_extra; }
108 void setExtras(const ExtraData &extras) { m_extra = extras; }
109 void unsetExtra(const QString &key);
110
111 bool warningOnly() const { return m_warningOnly; }
112 void setWarningOnly(bool isWarningOnly) { m_warningOnly = isWarningOnly; }
113
114 void dump() const;
115
116private:
117 QString m_id;
118 QString m_context;
119 QString m_sourcetext;
120 QString m_oldsourcetext;
121 QString m_comment;
122 QString m_oldcomment;
123 QString m_userData;
124 ExtraData m_extra; // PO flags, PO plurals
125 QString m_extraComment;
126 QString m_translatorComment;
127 QString m_warning;
128 QStringList m_translations;
129 QString m_fileName;
130 int m_lineNumber;
131 int m_tsLineNumber = -1;
132 References m_extraRefs;
133 bool m_warningOnly = false;
134
135 Type m_type;
136 bool m_plural;
137};
138
139Q_DECLARE_TYPEINFO(TranslatorMessage, Q_RELOCATABLE_TYPE);
140
141QT_END_NAMESPACE
142
143#endif // TRANSLATORMESSAGE_H
144

source code of qttools/src/linguist/shared/translatormessage.h