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 | |
7 | #include "translatormessage.h" |
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 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | class QIODevice; |
23 | |
24 | // A struct of "interesting" data passed to and from the load and save routines |
25 | class ConversionData |
26 | { |
27 | public: |
28 | ConversionData() : |
29 | m_verbose(false), |
30 | m_ignoreUnfinished(false), |
31 | m_sortContexts(false), |
32 | m_noUiLines(false), |
33 | m_idBased(false), |
34 | m_saveMode(SaveEverything) |
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(t: error); } |
46 | QString error() const { return m_errors.isEmpty() ? QString() : m_errors.join(sep: QLatin1Char('\n')) + QLatin1Char('\n'); } |
47 | QStringList errors() const { return m_errors; } |
48 | void clearErrors() { m_errors.clear(); } |
49 | |
50 | public: |
51 | QString m_defaultContext; |
52 | bool m_sourceIsUtf16; // CPP & JAVA specific |
53 | QString m_unTrPrefix; // QM specific |
54 | QString m_sourceFileName; |
55 | QString m_targetFileName; |
56 | QString m_compilationDatabaseDir; |
57 | QVector<QRegularExpression> m_excludes; |
58 | QDir m_sourceDir; |
59 | QDir m_targetDir; // FIXME: TS specific |
60 | QSet<QString> m_projectRoots; |
61 | QMultiHash<QString, QString> m_allCSources; |
62 | QStringList m_includePath; |
63 | QStringList m_dropTags; // tags to be dropped |
64 | QStringList m_errors; |
65 | bool m_verbose; |
66 | bool m_ignoreUnfinished; |
67 | bool m_sortContexts; |
68 | bool m_noUiLines; |
69 | bool m_idBased; |
70 | TranslatorSaveMode m_saveMode; |
71 | QStringList m_rootDirs; |
72 | }; |
73 | |
74 | class TMMKey { |
75 | public: |
76 | TMMKey(const TranslatorMessage &msg) |
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; } |
80 | QString context, source, comment; |
81 | }; |
82 | Q_DECLARE_TYPEINFO(TMMKey, Q_RELOCATABLE_TYPE); |
83 | inline size_t qHash(const TMMKey &key) |
84 | { |
85 | return qHash(key: key.context) ^ qHash(key: key.source) ^ qHash(key: key.comment); |
86 | } |
87 | |
88 | class Translator |
89 | { |
90 | public: |
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 | |
107 | void stripObsoleteMessages(); |
108 | void stripFinishedMessages(); |
109 | void stripUntranslatedMessages(); |
110 | void stripEmptyContexts(); |
111 | void stripNonPluralForms(); |
112 | void stripIdenticalSourceTranslations(); |
113 | void dropTranslations(); |
114 | void dropUiLines(); |
115 | void makeFileNamesAbsolute(const QDir &originalPath); |
116 | bool translationsExist() const; |
117 | |
118 | using DuplicateEntries = QHash<int, QVector<int>>; |
119 | struct Duplicates |
120 | { |
121 | DuplicateEntries byId, byContents; |
122 | }; |
123 | Duplicates resolveDuplicates(); |
124 | void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose); |
125 | void reportDuplicatesLines(const TranslatorMessage &msg, |
126 | const DuplicateEntries::value_type &dups) const; |
127 | |
128 | QString languageCode() const { return m_language; } |
129 | QString sourceLanguageCode() const { return m_sourceLanguage; } |
130 | |
131 | enum LocationsType { DefaultLocations, NoLocations, RelativeLocations, AbsoluteLocations }; |
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); |
143 | void normalizeTranslations(ConversionData &cd); |
144 | QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const; |
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 { |
169 | FileFormat() : untranslatedDescription(nullptr), loader(0), saver(0), priority(-1) {} |
170 | QString extension; // such as "ts", "xlf", ... |
171 | const char *untranslatedDescription; |
172 | // human-readable description |
173 | QString description() const { return FMT::tr(sourceText: untranslatedDescription); } |
174 | LoadFunction loader; |
175 | SaveFunction saver; |
176 | enum FileType { TranslationSource, TranslationBinary } fileType; |
177 | int priority; // 0 = highest, -1 = invisible |
178 | }; |
179 | static void registerFileFormat(const FileFormat &format); |
180 | static QList<FileFormat> ®isteredFileFormats(); |
181 | |
182 | enum { |
183 | TextVariantSeparator = 0x2762, // some weird character nobody ever heard of :-D |
184 | BinaryVariantSeparator = 0x9c // unicode "STRING TERMINATOR" |
185 | }; |
186 | |
187 | private: |
188 | void insert(int idx, const TranslatorMessage &msg); |
189 | void addIndex(int idx, const TranslatorMessage &msg) const; |
190 | void delIndex(int idx) const; |
191 | void ensureIndexed() const; |
192 | |
193 | typedef QList<TranslatorMessage> TMM; // int stores the sequence position. |
194 | |
195 | TMM m_messages; |
196 | LocationsType m_locationsType; |
197 | |
198 | // A string beginning with a 2 or 3 letter language code (ISO 639-1 |
199 | // or ISO-639-2), followed by the optional territory variant to distinguish |
200 | // between territory-specific variations of the language. The language code |
201 | // and territory code are always separated by '_' |
202 | // Note that the language part can also be a 3-letter ISO 639-2 code. |
203 | // Legal examples: |
204 | // 'pt' portuguese, assumes portuguese from portugal |
205 | // 'pt_BR' Brazilian portuguese (ISO 639-1 language code) |
206 | // 'por_BR' Brazilian portuguese (ISO 639-2 language code) |
207 | QString m_language; |
208 | QString m_sourceLanguage; |
209 | QStringList m_dependencies; |
210 | ExtraData m_extra; |
211 | |
212 | mutable bool m_indexOk; |
213 | mutable QHash<QString, int> m_ctxCmtIdx; |
214 | mutable QHash<QString, int> m_idMsgIdx; |
215 | mutable QHash<TMMKey, int> m_msgIdx; |
216 | }; |
217 | |
218 | bool getNumerusInfo(QLocale::Language language, QLocale::Territory territory, QByteArray *rules, |
219 | QStringList *forms, const char **gettextRules); |
220 | |
221 | QString getNumerusInfoString(); |
222 | |
223 | bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd); |
224 | |
225 | /* |
226 | This is a quick hack. The proper way to handle this would be |
227 | to extend Translator's interface. |
228 | */ |
229 | #define ContextComment "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT" |
230 | |
231 | QT_END_NAMESPACE |
232 | |
233 | #endif |
234 |
Definitions
- ConversionData
- ConversionData
- dropTags
- dropTags
- targetDir
- isVerbose
- ignoreUnfinished
- sortContexts
- appendError
- error
- errors
- clearErrors
- TMMKey
- TMMKey
- operator==
- qHash
- Translator
- Duplicates
- languageCode
- sourceLanguageCode
- LocationsType
- setLocationsType
- locationsType
- setLanguageCode
- setSourceLanguageCode
- messageCount
- message
- message
- constMessage
- setDependencies
- dependencies
- extras
- setExtras
- FileFormat
- FileFormat
- description
Learn to use CMake with our Intro Training
Find out more