1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the Qt Linguist of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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:
|
---|
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
|
---|
87 | const QStringList &dropTags() const { return m_dropTags; }
|
---|
88 | QStringList &dropTags() { return m_dropTags; }
|
---|
89 | const QDir &targetDir() const { return m_targetDir; }
|
---|
90 | bool isVerbose() const { return m_verbose; }
|
---|
91 | bool ignoreUnfinished() const { return m_ignoreUnfinished; }
|
---|
92 | bool sortContexts() const { return m_sortContexts; }
|
---|
93 |
|
---|
94 | void appendError(const QString &error) { m_errors.append(error); }
|
---|
95 | QString error() const { return m_errors.join(QLatin1String("\n")); }
|
---|
96 | QStringList errors() const { return m_errors; }
|
---|
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 Translator
|
---|
122 | {
|
---|
123 | public:
|
---|
124 | Translator();
|
---|
125 |
|
---|
126 | bool load(const QString &filename, ConversionData &err, const QString &format /*= "auto"*/);
|
---|
127 | bool save(const QString &filename, ConversionData &err, const QString &format /*= "auto"*/) const;
|
---|
128 | bool release(QFile *iod, ConversionData &cd) const;
|
---|
129 |
|
---|
130 | int find(const TranslatorMessage &msg) const;
|
---|
131 | TranslatorMessage find(const QString &context,
|
---|
132 | const QString &comment, const TranslatorMessage::References &refs) const;
|
---|
133 |
|
---|
134 | bool contains(const QString &context) const;
|
---|
135 | TranslatorMessage find(const QString &context) const;
|
---|
136 |
|
---|
137 | void replaceSorted(const TranslatorMessage &msg);
|
---|
138 | void extend(const TranslatorMessage &msg); // Only for single-location messages
|
---|
139 | void append(const TranslatorMessage &msg);
|
---|
140 | void appendSorted(const TranslatorMessage &msg);
|
---|
141 |
|
---|
142 | void stripObsoleteMessages();
|
---|
143 | void stripFinishedMessages();
|
---|
144 | void stripEmptyContexts();
|
---|
145 | void stripNonPluralForms();
|
---|
146 | void stripIdenticalSourceTranslations();
|
---|
147 | void dropTranslations();
|
---|
148 | void dropUiLines();
|
---|
149 | void makeFileNamesAbsolute(const QDir &originalPath);
|
---|
150 |
|
---|
151 | struct Duplicates { QSet<int> byId, byContents; };
|
---|
152 | Duplicates resolveDuplicates();
|
---|
153 | void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose);
|
---|
154 |
|
---|
155 | void setCodecName(const QByteArray &name);
|
---|
156 | QByteArray codecName() const;
|
---|
157 | QTextCodec *codec() const { return m_codec; }
|
---|
158 |
|
---|
159 | QString languageCode() const { return m_language; }
|
---|
160 | QString sourceLanguageCode() const { return m_sourceLanguage; }
|
---|
161 |
|
---|
162 | enum LocationsType { DefaultLocations, NoLocations, RelativeLocations, AbsoluteLocations };
|
---|
163 | void setLocationsType(LocationsType lt) { m_locationsType = lt; }
|
---|
164 | LocationsType locationsType() const { return m_locationsType; }
|
---|
165 |
|
---|
166 | static QString makeLanguageCode(QLocale::Language language, QLocale::Country country);
|
---|
167 | static void languageAndCountry(const QString &languageCode,
|
---|
168 | QLocale::Language *lang, QLocale::Country *country);
|
---|
169 | void setLanguageCode(const QString &languageCode) { m_language = languageCode; }
|
---|
170 | void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; }
|
---|
171 | static QString guessLanguageCodeFromFileName(const QString &fileName);
|
---|
172 | QList<TranslatorMessage> messages() const;
|
---|
173 | QList<TranslatorMessage> translatedMessages() const;
|
---|
174 | static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals);
|
---|
175 | void normalizeTranslations(ConversionData &cd);
|
---|
176 | QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const;
|
---|
177 |
|
---|
178 | int messageCount() const { return m_messages.size(); }
|
---|
179 | TranslatorMessage &message(int i) { return m_messages[i]; }
|
---|
180 | const TranslatorMessage &message(int i) const { return m_messages.at(i); }
|
---|
181 | void dump() const;
|
---|
182 |
|
---|
183 | // additional file format specific data
|
---|
184 | // note: use '<fileformat>:' as prefix for file format specific members,
|
---|
185 | // e.g. "po-flags", "po-msgid_plural"
|
---|
186 | typedef TranslatorMessage::ExtraData ExtraData;
|
---|
187 | QString extra(const QString &ba) const;
|
---|
188 | void setExtra(const QString &ba, const QString &var);
|
---|
189 | bool hasExtra(const QString &ba) const;
|
---|
190 | const ExtraData &extras() const { return m_extra; }
|
---|
191 | void setExtras(const ExtraData &extras) { m_extra = extras; }
|
---|
192 |
|
---|
193 | // registration of file formats
|
---|
194 | typedef bool (*SaveFunction)(const Translator &, QIODevice &out, ConversionData &data);
|
---|
195 | typedef bool (*LoadFunction)(Translator &, QIODevice &in, ConversionData &data);
|
---|
196 | struct FileFormat {
|
---|
197 | FileFormat() : loader(0), saver(0), priority(-1) {}
|
---|
198 | QString extension; // such as "ts", "xlf", ...
|
---|
199 | QString description; // human-readable description
|
---|
200 | LoadFunction loader;
|
---|
201 | SaveFunction saver;
|
---|
202 | enum FileType { TranslationSource, TranslationBinary } fileType;
|
---|
203 | int priority; // 0 = highest, -1 = invisible
|
---|
204 | };
|
---|
205 | static void registerFileFormat(const FileFormat &format);
|
---|
206 | static QList<FileFormat> ®isteredFileFormats();
|
---|
207 |
|
---|
208 | enum {
|
---|
209 | TextVariantSeparator = 0x2762, // some weird character nobody ever heard of :-D
|
---|
210 | BinaryVariantSeparator = 0x9c // unicode "STRING TERMINATOR"
|
---|
211 | };
|
---|
212 |
|
---|
213 | private:
|
---|
214 | typedef QList<TranslatorMessage> TMM; // int stores the sequence position.
|
---|
215 |
|
---|
216 | TMM m_messages;
|
---|
217 | QTextCodec *m_codec;
|
---|
218 | LocationsType m_locationsType;
|
---|
219 |
|
---|
220 | // A string beginning with a 2 or 3 letter language code (ISO 639-1
|
---|
221 | // or ISO-639-2), followed by the optional country variant to distinguish
|
---|
222 | // between country-specific variations of the language. The language code
|
---|
223 | // and country code are always separated by '_'
|
---|
224 | // Note that the language part can also be a 3-letter ISO 639-2 code.
|
---|
225 | // Legal examples:
|
---|
226 | // 'pt' portuguese, assumes portuguese from portugal
|
---|
227 | // 'pt_BR' Brazilian portuguese (ISO 639-1 language code)
|
---|
228 | // 'por_BR' Brazilian portuguese (ISO 639-2 language code)
|
---|
229 | QString m_language;
|
---|
230 | QString m_sourceLanguage;
|
---|
231 | ExtraData m_extra;
|
---|
232 | };
|
---|
233 |
|
---|
234 | bool getNumerusInfo(QLocale::Language language, QLocale::Country country,
|
---|
235 | QByteArray *rules, QStringList *forms);
|
---|
236 |
|
---|
237 | /*
|
---|
238 | This is a quick hack. The proper way to handle this would be
|
---|
239 | to extend Translator's interface.
|
---|
240 | */
|
---|
241 | #define ContextComment "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT"
|
---|
242 |
|
---|
243 | QT_END_NAMESPACE
|
---|
244 |
|
---|
245 | #endif
|
---|