Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
translator.h
Go to the documentation of this file.
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 METATRANSLATOR_H
5#define METATRANSLATOR_H
6
8#include "fmt.h"
9
10#include <QCoreApplication>
11#include <QDir>
12#include <QList>
13#include <QLocale>
14#include <QMultiHash>
15#include <QRegularExpression>
16#include <QString>
17#include <QSet>
18#include <QVector>
19
21
22class QIODevice;
23
24// A struct of "interesting" data passed to and from the load and save routines
26{
27public:
29 m_verbose(false),
30 m_ignoreUnfinished(false),
31 m_sortContexts(false),
32 m_noUiLines(false),
33 m_idBased(false),
35 {}
36
37 // tag manipulation
38 const QStringList &dropTags() const { return m_dropTags; }
39 QStringList &dropTags() { return m_dropTags; }
40 const QDir &targetDir() const { return m_targetDir; }
41 bool isVerbose() const { return m_verbose; }
42 bool ignoreUnfinished() const { return m_ignoreUnfinished; }
43 bool sortContexts() const { return m_sortContexts; }
44
45 void appendError(const QString &error) { m_errors.append(error); }
46 QString error() const { return m_errors.isEmpty() ? QString() : m_errors.join(QLatin1Char('\n')) + QLatin1Char('\n'); }
47 QStringList errors() const { return m_errors; }
48 void clearErrors() { m_errors.clear(); }
49
50public:
52 bool m_sourceIsUtf16; // CPP & JAVA specific
53 QString m_unTrPrefix; // QM specific
59 QDir m_targetDir; // FIXME: TS specific
63 QStringList m_dropTags; // tags to be dropped
72};
73
74class TMMKey {
75public:
77 { context = msg.context(); source = msg.sourceText(); comment = msg.comment(); }
78 bool operator==(const TMMKey &o) const
79 { return context == o.context && source == o.source && comment == o.comment; }
81};
83inline size_t qHash(const TMMKey &key)
84{
85 return qHash(key.context) ^ qHash(key.source) ^ qHash(key.comment);
86}
87
89{
90public:
91 Translator();
92
93 bool load(const QString &filename, ConversionData &err, const QString &format /* = "auto" */);
94 bool save(const QString &filename, ConversionData &err, const QString &format /* = "auto" */) const;
95
96 int find(const TranslatorMessage &msg) const;
97 int find(const QString &context,
98 const QString &comment, const TranslatorMessage::References &refs) const;
99
100 int find(const QString &context) const;
101
102 void replaceSorted(const TranslatorMessage &msg);
103 void extend(const TranslatorMessage &msg, ConversionData &cd); // Only for single-location messages
104 void append(const TranslatorMessage &msg);
105 void appendSorted(const TranslatorMessage &msg);
106
110 void stripEmptyContexts();
111 void stripNonPluralForms();
113 void dropTranslations();
114 void dropUiLines();
115 void makeFileNamesAbsolute(const QDir &originalPath);
116 bool translationsExist() const;
117
118 using DuplicateEntries = QHash<int, QVector<int>>;
124 void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose);
126 const DuplicateEntries::value_type &dups) const;
127
128 QString languageCode() const { return m_language; }
129 QString sourceLanguageCode() const { return m_sourceLanguage; }
130
132 void setLocationsType(LocationsType lt) { m_locationsType = lt; }
133 LocationsType locationsType() const { return m_locationsType; }
134
135 static QString makeLanguageCode(QLocale::Language language, QLocale::Territory territory);
136 static void languageAndTerritory(QStringView languageCode, QLocale::Language *langPtr,
137 QLocale::Territory *territoryPtr);
138 void setLanguageCode(const QString &languageCode) { m_language = languageCode; }
139 void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; }
140 static QString guessLanguageCodeFromFileName(const QString &fileName);
141 const QList<TranslatorMessage> &messages() const;
142 static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals);
145
146 int messageCount() const { return m_messages.size(); }
147 TranslatorMessage &message(int i) { return m_messages[i]; }
148 const TranslatorMessage &message(int i) const { return m_messages.at(i); }
149 const TranslatorMessage &constMessage(int i) const { return m_messages.at(i); }
150 void dump() const;
151
152 void setDependencies(const QStringList &dependencies) { m_dependencies = dependencies; }
153 QStringList dependencies() const { return m_dependencies; }
154
155 // additional file format specific data
156 // note: use '<fileformat>:' as prefix for file format specific members,
157 // e.g. "po-flags", "po-msgid_plural"
158 typedef TranslatorMessage::ExtraData ExtraData;
159 QString extra(const QString &ba) const;
160 void setExtra(const QString &ba, const QString &var);
161 bool hasExtra(const QString &ba) const;
162 const ExtraData &extras() const { return m_extra; }
163 void setExtras(const ExtraData &extras) { m_extra = extras; }
164
165 // registration of file formats
166 typedef bool (*SaveFunction)(const Translator &, QIODevice &out, ConversionData &data);
167 typedef bool (*LoadFunction)(Translator &, QIODevice &in, ConversionData &data);
168 struct FileFormat {
170 QString extension; // such as "ts", "xlf", ...
172 // human-readable description
173 QString description() const { return FMT::tr(untranslatedDescription); }
177 int priority; // 0 = highest, -1 = invisible
178 };
179 static void registerFileFormat(const FileFormat &format);
181
182 static constexpr QChar TextVariantSeparator{0x2762}; // some weird character nobody ever heard of :-D
183 static constexpr QChar BinaryVariantSeparator{0x9c}; // unicode "STRING TERMINATOR"
184
185private:
186 void insert(int idx, const TranslatorMessage &msg);
187 void addIndex(int idx, const TranslatorMessage &msg) const;
188 void delIndex(int idx) const;
189 void ensureIndexed() const;
190
191 typedef QList<TranslatorMessage> TMM; // int stores the sequence position.
192
193 TMM m_messages;
194 LocationsType m_locationsType;
195
196 // A string beginning with a 2 or 3 letter language code (ISO 639-1
197 // or ISO-639-2), followed by the optional territory variant to distinguish
198 // between territory-specific variations of the language. The language code
199 // and territory code are always separated by '_'
200 // Note that the language part can also be a 3-letter ISO 639-2 code.
201 // Legal examples:
202 // 'pt' portuguese, assumes portuguese from portugal
203 // 'pt_BR' Brazilian portuguese (ISO 639-1 language code)
204 // 'por_BR' Brazilian portuguese (ISO 639-2 language code)
205 QString m_language;
206 QString m_sourceLanguage;
207 QStringList m_dependencies;
208 ExtraData m_extra;
209
210 mutable bool m_indexOk;
211 mutable QHash<QString, int> m_ctxCmtIdx;
212 mutable QHash<QString, int> m_idMsgIdx;
213 mutable QHash<TMMKey, int> m_msgIdx;
214};
215
216bool getNumerusInfo(QLocale::Language language, QLocale::Territory territory, QByteArray *rules,
217 QStringList *forms, const char **gettextRules);
218
220
221bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd);
222
223/*
224 This is a quick hack. The proper way to handle this would be
225 to extend Translator's interface.
226*/
227#define ContextComment "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT"
228
229QT_END_NAMESPACE
230
231#endif
int main(int argc, char *argv[])
[2]
Definition buffer.cpp:77
QString m_defaultContext
Definition translator.h:51
void clearErrors()
Definition translator.h:48
QString m_unTrPrefix
Definition translator.h:53
QString m_targetFileName
Definition translator.h:55
bool sortContexts() const
Definition translator.h:43
QVector< QRegularExpression > m_excludes
Definition translator.h:57
const QDir & targetDir() const
Definition translator.h:40
QStringList m_dropTags
Definition translator.h:63
QString m_sourceFileName
Definition translator.h:54
QString error() const
Definition translator.h:46
const QStringList & dropTags() const
Definition translator.h:38
QStringList m_rootDirs
Definition translator.h:71
bool isVerbose() const
Definition translator.h:41
QSet< QString > m_projectRoots
Definition translator.h:60
QStringList & dropTags()
Definition translator.h:39
TranslatorSaveMode m_saveMode
Definition translator.h:70
QMultiHash< QString, QString > m_allCSources
Definition translator.h:61
bool m_sortContexts
Definition translator.h:67
QStringList errors() const
Definition translator.h:47
bool ignoreUnfinished() const
Definition translator.h:42
void appendError(const QString &error)
Definition translator.h:45
bool m_ignoreUnfinished
Definition translator.h:66
QStringList m_includePath
Definition translator.h:62
bool m_sourceIsUtf16
Definition translator.h:52
QString m_compilationDatabaseDir
Definition translator.h:56
QStringList m_errors
Definition translator.h:64
QString source
Definition translator.h:80
bool operator==(const TMMKey &o) const
Definition translator.h:78
TMMKey(const TranslatorMessage &msg)
Definition translator.h:76
QString comment
Definition translator.h:80
QString context
Definition translator.h:80
QList< Reference > References
QHash< QString, QString > ExtraData
void setDependencies(const QStringList &dependencies)
Definition translator.h:152
bool(*) LoadFunction(Translator &, QIODevice &in, ConversionData &data)
Definition translator.h:167
static QString makeLanguageCode(QLocale::Language language, QLocale::Territory territory)
void stripObsoleteMessages()
QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const
void dropTranslations()
bool save(const QString &filename, ConversionData &err, const QString &format) const
bool load(const QString &filename, ConversionData &err, const QString &format)
static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals)
void dropUiLines()
void dump() const
void stripEmptyContexts()
void stripUntranslatedMessages()
void stripFinishedMessages()
void replaceSorted(const TranslatorMessage &msg)
static void languageAndTerritory(QStringView languageCode, QLocale::Language *langPtr, QLocale::Territory *territoryPtr)
void stripNonPluralForms()
void setExtra(const QString &ba, const QString &var)
LocationsType locationsType() const
Definition translator.h:133
void setExtras(const ExtraData &extras)
Definition translator.h:163
bool(*) SaveFunction(const Translator &, QIODevice &out, ConversionData &data)
Definition translator.h:166
const TranslatorMessage & message(int i) const
Definition translator.h:148
int find(const QString &context) const
QString extra(const QString &ba) const
QString languageCode() const
Definition translator.h:128
static void registerFileFormat(const FileFormat &format)
void append(const TranslatorMessage &msg)
static constexpr QChar TextVariantSeparator
Definition translator.h:182
static QString guessLanguageCodeFromFileName(const QString &fileName)
void setLocationsType(LocationsType lt)
Definition translator.h:132
@ RelativeLocations
Definition translator.h:131
@ AbsoluteLocations
Definition translator.h:131
void stripIdenticalSourceTranslations()
static QList< FileFormat > & registeredFileFormats()
int messageCount() const
Definition translator.h:146
void makeFileNamesAbsolute(const QDir &originalPath)
QStringList dependencies() const
Definition translator.h:153
bool translationsExist() const
void setLanguageCode(const QString &languageCode)
Definition translator.h:138
const TranslatorMessage & constMessage(int i) const
Definition translator.h:149
Duplicates resolveDuplicates()
bool hasExtra(const QString &ba) const
void setSourceLanguageCode(const QString &languageCode)
Definition translator.h:139
int find(const QString &context, const QString &comment, const TranslatorMessage::References &refs) const
void normalizeTranslations(ConversionData &cd)
TranslatorMessage & message(int i)
Definition translator.h:147
void appendSorted(const TranslatorMessage &msg)
void reportDuplicatesLines(const TranslatorMessage &msg, const DuplicateEntries::value_type &dups) const
int find(const TranslatorMessage &msg) const
void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose)
const ExtraData & extras() const
Definition translator.h:162
const QList< TranslatorMessage > & messages() const
void extend(const TranslatorMessage &msg, ConversionData &cd)
QString sourceLanguageCode() const
Definition translator.h:129
static constexpr QChar BinaryVariantSeparator
Definition translator.h:183
Combined button and popup list for selecting options.
static QT_USE_NAMESPACE int usage(const QStringList &args)
Definition main.cpp:17
Definition main.cpp:91
QString format
Definition main.cpp:93
QString name
Definition main.cpp:92
DuplicateEntries byContents
Definition translator.h:121
DuplicateEntries byId
Definition translator.h:121
const char * untranslatedDescription
Definition translator.h:171
QString description() const
Definition translator.h:173
size_t qHash(const TMMKey &key)
Definition translator.h:83
bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd)
Definition qm.cpp:619
QString getNumerusInfoString()
Definition numerus.cpp:361
Q_DECLARE_TYPEINFO(TMMKey, Q_RELOCATABLE_TYPE)
bool getNumerusInfo(QLocale::Language language, QLocale::Territory territory, QByteArray *rules, QStringList *forms, const char **gettextRules)
Definition numerus.cpp:328
TranslatorSaveMode
@ SaveEverything