1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Linguist of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef METATRANSLATOR_H
43 #define METATRANSLATOR_H
44 
45 #include "translatormessage.h"
46 
47 #include <QDir>
48 #include <QList>
49 #include <QLocale>
50 #include <QMultiHash>
51 #include <QString>
52 #include <QSet>
53 
54 
55 QT_BEGIN_NAMESPACE
56 
57 #ifdef QT_BOOTSTRAPPED
58 class QObject {
59 public:
60     static QString tr(const char *sourceText, const char * = 0, int n = -1);
61 };
62 class QCoreApplication : public QObject {
63 public:
64     enum Encoding { CodecForTr };
65     static QString translate(const char *, const char *sourceText, const char * = 0,
66                              Encoding = CodecForTr, int n = -1)
67         { return tr(sourceText, 0, n); }
68 };
69 #endif
70 
71 class QIODevice;
72 
73 // A struct of "interesting" data passed to and from the load and save routines
74 class ConversionData
75 {
76 public:
ConversionData()77     ConversionData() :
78         m_verbose(false),
79         m_ignoreUnfinished(false),
80         m_sortContexts(false),
81         m_noUiLines(false),
82         m_idBased(false),
83         m_saveMode(SaveEverything)
84     {}
85 
86     // tag manipulation
dropTags()87     const QStringList &dropTags() const { return m_dropTags; }
dropTags()88     QStringList &dropTags() { return m_dropTags; }
targetDir()89     const QDir &targetDir() const { return m_targetDir; }
isVerbose()90     bool isVerbose() const { return m_verbose; }
ignoreUnfinished()91     bool ignoreUnfinished() const { return m_ignoreUnfinished; }
sortContexts()92     bool sortContexts() const { return m_sortContexts; }
93 
appendError(const QString & error)94     void appendError(const QString &error) { m_errors.append(error); }
error()95     QString error() const { return m_errors.isEmpty() ? QString() : m_errors.join(QLatin1String("\n")) + QLatin1Char('\n'); }
errors()96     QStringList errors() const { return  m_errors; }
clearErrors()97     void clearErrors() { m_errors.clear(); }
98 
99 public:
100     QString m_defaultContext;
101     QByteArray m_codecForSource; // CPP, PO & QM specific
102     QByteArray m_outputCodec; // CPP & PO specific
103     QString m_unTrPrefix; // QM specific
104     QString m_sourceFileName;
105     QString m_targetFileName;
106     QDir m_sourceDir;
107     QDir m_targetDir; // FIXME: TS specific
108     QSet<QString> m_projectRoots;
109     QMultiHash<QString, QString> m_allCSources;
110     QStringList m_includePath;
111     QStringList m_dropTags;  // tags to be dropped
112     QStringList m_errors;
113     bool m_verbose;
114     bool m_ignoreUnfinished;
115     bool m_sortContexts;
116     bool m_noUiLines;
117     bool m_idBased;
118     TranslatorSaveMode m_saveMode;
119 };
120 
121 class TMMKey {
122 public:
TMMKey(const TranslatorMessage & msg)123     TMMKey(const TranslatorMessage &msg)
124         { context = msg.context(); source = msg.sourceText(); comment = msg.comment(); }
125     bool operator==(const TMMKey &o) const
126         { return context == o.context && source == o.source && comment == o.comment; }
127     QString context, source, comment;
128 };
129 Q_DECLARE_TYPEINFO(TMMKey, Q_MOVABLE_TYPE);
qHash(const TMMKey & key)130 inline uint qHash(const TMMKey &key) { return qHash(key.context) ^ qHash(key.source) ^ qHash(key.comment); }
131 
132 class Translator
133 {
134 public:
135     Translator();
136 
137     bool load(const QString &filename, ConversionData &err, const QString &format /* = "auto" */);
138     bool save(const QString &filename, ConversionData &err, const QString &format /* = "auto" */) const;
139 
140     int find(const TranslatorMessage &msg) const;
141     int find(const QString &context,
142         const QString &comment, const TranslatorMessage::References &refs) const;
143 
144     int find(const QString &context) const;
145 
146     void replaceSorted(const TranslatorMessage &msg);
147     void extend(const TranslatorMessage &msg); // Only for single-location messages
148     void append(const TranslatorMessage &msg);
149     void appendSorted(const TranslatorMessage &msg);
150 
151     void stripObsoleteMessages();
152     void stripFinishedMessages();
153     void stripEmptyContexts();
154     void stripNonPluralForms();
155     void stripIdenticalSourceTranslations();
156     void dropTranslations();
157     void dropUiLines();
158     void makeFileNamesAbsolute(const QDir &originalPath);
159 
160     struct Duplicates { QSet<int> byId, byContents; };
161     Duplicates resolveDuplicates();
162     void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose);
163 
164     void setCodecName(const QByteArray &name);
setCodec(QTextCodec * codec)165     void setCodec(QTextCodec *codec) { m_codec = codec; }
166     QByteArray codecName() const;
codec()167     QTextCodec *codec() const { return m_codec; }
168 
languageCode()169     QString languageCode() const { return m_language; }
sourceLanguageCode()170     QString sourceLanguageCode() const { return m_sourceLanguage; }
171 
172     enum LocationsType { DefaultLocations, NoLocations, RelativeLocations, AbsoluteLocations };
setLocationsType(LocationsType lt)173     void setLocationsType(LocationsType lt) { m_locationsType = lt; }
locationsType()174     LocationsType locationsType() const { return m_locationsType; }
175 
176     static QString makeLanguageCode(QLocale::Language language, QLocale::Country country);
177     static void languageAndCountry(const QString &languageCode,
178         QLocale::Language *lang, QLocale::Country *country);
setLanguageCode(const QString & languageCode)179     void setLanguageCode(const QString &languageCode) { m_language = languageCode; }
setSourceLanguageCode(const QString & languageCode)180     void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; }
181     static QString guessLanguageCodeFromFileName(const QString &fileName);
182     QList<TranslatorMessage> messages() const;
183     static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals);
184     void normalizeTranslations(ConversionData &cd);
185     QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const;
186 
messageCount()187     int messageCount() const { return m_messages.size(); }
message(int i)188     TranslatorMessage &message(int i) { return m_messages[i]; }
message(int i)189     const TranslatorMessage &message(int i) const { return m_messages.at(i); }
constMessage(int i)190     const TranslatorMessage &constMessage(int i) const { return m_messages.at(i); }
191     void dump() const;
192 
193     // additional file format specific data
194     // note: use '<fileformat>:' as prefix for file format specific members,
195     // e.g. "po-flags", "po-msgid_plural"
196     typedef TranslatorMessage::ExtraData ExtraData;
197     QString extra(const QString &ba) const;
198     void setExtra(const QString &ba, const QString &var);
199     bool hasExtra(const QString &ba) const;
extras()200     const ExtraData &extras() const { return m_extra; }
setExtras(const ExtraData & extras)201     void setExtras(const ExtraData &extras) { m_extra = extras; }
202 
203     // registration of file formats
204     typedef bool (*SaveFunction)(const Translator &, QIODevice &out, ConversionData &data);
205     typedef bool (*LoadFunction)(Translator &, QIODevice &in, ConversionData &data);
206     struct FileFormat {
FileFormatFileFormat207         FileFormat() : loader(0), saver(0), priority(-1) {}
208         QString extension; // such as "ts", "xlf", ...
209         QString description; // human-readable description
210         LoadFunction loader;
211         SaveFunction saver;
212         enum FileType { TranslationSource, TranslationBinary } fileType;
213         int priority; // 0 = highest, -1 = invisible
214     };
215     static void registerFileFormat(const FileFormat &format);
216     static QList<FileFormat> &registeredFileFormats();
217 
218     enum {
219         TextVariantSeparator = 0x2762, // some weird character nobody ever heard of :-D
220         BinaryVariantSeparator = 0x9c // unicode "STRING TERMINATOR"
221     };
222 
223 private:
224     void insert(int idx, const TranslatorMessage &msg);
225     void addIndex(int idx, const TranslatorMessage &msg) const;
226     void delIndex(int idx) const;
227     void ensureIndexed() const;
228 
229     typedef QList<TranslatorMessage> TMM;       // int stores the sequence position.
230 
231     TMM m_messages;
232     QTextCodec *m_codec;
233     LocationsType m_locationsType;
234 
235     // A string beginning with a 2 or 3 letter language code (ISO 639-1
236     // or ISO-639-2), followed by the optional country variant to distinguish
237     //  between country-specific variations of the language. The language code
238     // and country code are always separated by '_'
239     // Note that the language part can also be a 3-letter ISO 639-2 code.
240     // Legal examples:
241     // 'pt'         portuguese, assumes portuguese from portugal
242     // 'pt_BR'      Brazilian portuguese (ISO 639-1 language code)
243     // 'por_BR'     Brazilian portuguese (ISO 639-2 language code)
244     QString m_language;
245     QString m_sourceLanguage;
246     ExtraData m_extra;
247 
248     mutable bool m_indexOk;
249     mutable QHash<QString, int> m_ctxCmtIdx;
250     mutable QHash<QString, int> m_idMsgIdx;
251     mutable QHash<TMMKey, int> m_msgIdx;
252 };
253 
254 bool getNumerusInfo(QLocale::Language language, QLocale::Country country,
255                     QByteArray *rules, QStringList *forms, const char **gettextRules);
256 
257 QString getNumerusInfoString();
258 
259 bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd);
260 
261 /*
262   This is a quick hack. The proper way to handle this would be
263   to extend Translator's interface.
264 */
265 #define ContextComment "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT"
266 
267 QT_END_NAMESPACE
268 
269 #endif
270