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