author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 5 | d3bac044e0f0 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the Qt Linguist of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
#ifdef QT_BOOTSTRAPPED |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
58 |
class QObject { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
59 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
60 |
static QString tr(const char *sourceText, const char * = 0, int n = -1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
61 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
class QCoreApplication : public QObject { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
63 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
enum Encoding { CodecForTr }; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
static QString translate(const char *, const char *sourceText, const char * = 0, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
Encoding = CodecForTr, int n = -1) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
{ return tr(sourceText, 0, n); } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
70 |
|
0 | 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 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
102 |
QByteArray m_outputCodec; // CPP & PO specific |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
103 |
QString m_unTrPrefix; // QM specific |
0 | 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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
int find(const TranslatorMessage &msg) const; |
0 | 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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
struct Duplicates { QSet<int> byId, byContents; }; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
Duplicates resolveDuplicates(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose); |
0 | 154 |
|
155 |
void setCodecName(const QByteArray &name); |
|
5
d3bac044e0f0
Revision: 201007
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
4
diff
changeset
|
156 |
void setCodec(QTextCodec *codec) { m_codec = codec; } |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
QByteArray codecName() const; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
QTextCodec *codec() const { return m_codec; } |
0 | 159 |
|
160 |
QString languageCode() const { return m_language; } |
|
161 |
QString sourceLanguageCode() const { return m_sourceLanguage; } |
|
162 |
||
163 |
enum LocationsType { DefaultLocations, NoLocations, RelativeLocations, AbsoluteLocations }; |
|
164 |
void setLocationsType(LocationsType lt) { m_locationsType = lt; } |
|
165 |
LocationsType locationsType() const { return m_locationsType; } |
|
166 |
||
167 |
static QString makeLanguageCode(QLocale::Language language, QLocale::Country country); |
|
168 |
static void languageAndCountry(const QString &languageCode, |
|
169 |
QLocale::Language *lang, QLocale::Country *country); |
|
170 |
void setLanguageCode(const QString &languageCode) { m_language = languageCode; } |
|
171 |
void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; } |
|
172 |
static QString guessLanguageCodeFromFileName(const QString &fileName); |
|
173 |
QList<TranslatorMessage> messages() const; |
|
174 |
QList<TranslatorMessage> translatedMessages() const; |
|
175 |
static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals); |
|
176 |
void normalizeTranslations(ConversionData &cd); |
|
177 |
QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const; |
|
178 |
||
179 |
int messageCount() const { return m_messages.size(); } |
|
180 |
TranslatorMessage &message(int i) { return m_messages[i]; } |
|
181 |
const TranslatorMessage &message(int i) const { return m_messages.at(i); } |
|
182 |
void dump() const; |
|
183 |
||
184 |
// additional file format specific data |
|
185 |
// note: use '<fileformat>:' as prefix for file format specific members, |
|
186 |
// e.g. "po-flags", "po-msgid_plural" |
|
187 |
typedef TranslatorMessage::ExtraData ExtraData; |
|
188 |
QString extra(const QString &ba) const; |
|
189 |
void setExtra(const QString &ba, const QString &var); |
|
190 |
bool hasExtra(const QString &ba) const; |
|
191 |
const ExtraData &extras() const { return m_extra; } |
|
192 |
void setExtras(const ExtraData &extras) { m_extra = extras; } |
|
193 |
||
194 |
// registration of file formats |
|
195 |
typedef bool (*SaveFunction)(const Translator &, QIODevice &out, ConversionData &data); |
|
196 |
typedef bool (*LoadFunction)(Translator &, QIODevice &in, ConversionData &data); |
|
197 |
struct FileFormat { |
|
198 |
FileFormat() : loader(0), saver(0), priority(-1) {} |
|
199 |
QString extension; // such as "ts", "xlf", ... |
|
200 |
QString description; // human-readable description |
|
201 |
LoadFunction loader; |
|
202 |
SaveFunction saver; |
|
203 |
enum FileType { TranslationSource, TranslationBinary } fileType; |
|
204 |
int priority; // 0 = highest, -1 = invisible |
|
205 |
}; |
|
206 |
static void registerFileFormat(const FileFormat &format); |
|
207 |
static QList<FileFormat> ®isteredFileFormats(); |
|
208 |
||
209 |
enum { |
|
210 |
TextVariantSeparator = 0x2762, // some weird character nobody ever heard of :-D |
|
211 |
BinaryVariantSeparator = 0x9c // unicode "STRING TERMINATOR" |
|
212 |
}; |
|
213 |
||
214 |
private: |
|
215 |
typedef QList<TranslatorMessage> TMM; // int stores the sequence position. |
|
216 |
||
217 |
TMM m_messages; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
QTextCodec *m_codec; |
0 | 219 |
LocationsType m_locationsType; |
220 |
||
221 |
// A string beginning with a 2 or 3 letter language code (ISO 639-1 |
|
222 |
// or ISO-639-2), followed by the optional country variant to distinguish |
|
223 |
// between country-specific variations of the language. The language code |
|
224 |
// and country code are always separated by '_' |
|
225 |
// Note that the language part can also be a 3-letter ISO 639-2 code. |
|
226 |
// Legal examples: |
|
227 |
// 'pt' portuguese, assumes portuguese from portugal |
|
228 |
// 'pt_BR' Brazilian portuguese (ISO 639-1 language code) |
|
229 |
// 'por_BR' Brazilian portuguese (ISO 639-2 language code) |
|
230 |
QString m_language; |
|
231 |
QString m_sourceLanguage; |
|
232 |
ExtraData m_extra; |
|
233 |
}; |
|
234 |
||
235 |
bool getNumerusInfo(QLocale::Language language, QLocale::Country country, |
|
236 |
QByteArray *rules, QStringList *forms); |
|
237 |
||
238 |
/* |
|
239 |
This is a quick hack. The proper way to handle this would be |
|
240 |
to extend Translator's interface. |
|
241 |
*/ |
|
242 |
#define ContextComment "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT" |
|
243 |
||
244 |
QT_END_NAMESPACE |
|
245 |
||
246 |
#endif |