author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
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 |
#include "translator.h" |
|
43 |
||
44 |
#include "simtexth.h" |
|
45 |
||
46 |
#include <stdio.h> |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
47 |
#ifdef Q_OS_WIN |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
48 |
#include <io.h> // required for _setmode, to avoid _O_TEXT streams... |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
49 |
#include <fcntl.h> // for _O_BINARY |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
50 |
#endif |
0 | 51 |
|
52 |
#include <QtCore/QDebug> |
|
53 |
#include <QtCore/QDir> |
|
54 |
#include <QtCore/QFile> |
|
55 |
#include <QtCore/QFileInfo> |
|
56 |
#include <QtCore/QTextCodec> |
|
57 |
#include <QtCore/QTextStream> |
|
58 |
||
59 |
#include <private/qtranslator_p.h> |
|
60 |
||
61 |
QT_BEGIN_NAMESPACE |
|
62 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
63 |
#ifdef QT_BOOTSTRAPPED |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
QString QObject::tr(const char *sourceText, const char *, int n) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
QString ret = QString::fromLatin1(sourceText); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
if (n >= 0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
ret.replace(QLatin1String("%n"), QString::number(n)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
return ret; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
70 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
71 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
72 |
|
0 | 73 |
Translator::Translator() : |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
74 |
m_codec(QTextCodec::codecForName("ISO-8859-1")), |
0 | 75 |
m_locationsType(AbsoluteLocations) |
76 |
{ |
|
77 |
} |
|
78 |
||
79 |
void Translator::registerFileFormat(const FileFormat &format) |
|
80 |
{ |
|
81 |
//qDebug() << "Translator: Registering format " << format.extension; |
|
82 |
QList<Translator::FileFormat> &formats = registeredFileFormats(); |
|
83 |
for (int i = 0; i < formats.size(); ++i) |
|
84 |
if (format.fileType == formats[i].fileType && format.priority < formats[i].priority) { |
|
85 |
formats.insert(i, format); |
|
86 |
return; |
|
87 |
} |
|
88 |
formats.append(format); |
|
89 |
} |
|
90 |
||
91 |
QList<Translator::FileFormat> &Translator::registeredFileFormats() |
|
92 |
{ |
|
93 |
static QList<Translator::FileFormat> theFormats; |
|
94 |
return theFormats; |
|
95 |
} |
|
96 |
||
97 |
void Translator::replaceSorted(const TranslatorMessage &msg) |
|
98 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
int index = find(msg); |
0 | 100 |
if (index == -1) |
101 |
appendSorted(msg); |
|
102 |
else |
|
103 |
m_messages[index] = msg; |
|
104 |
} |
|
105 |
||
106 |
void Translator::extend(const TranslatorMessage &msg) |
|
107 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
int index = find(msg); |
0 | 109 |
if (index == -1) { |
110 |
m_messages.append(msg); |
|
111 |
} else { |
|
112 |
TranslatorMessage &emsg = m_messages[index]; |
|
113 |
emsg.addReferenceUniq(msg.fileName(), msg.lineNumber()); |
|
114 |
if (!msg.extraComment().isEmpty()) { |
|
115 |
QString cmt = emsg.extraComment(); |
|
116 |
if (!cmt.isEmpty()) |
|
117 |
cmt.append(QLatin1String("\n----------\n")); |
|
118 |
cmt.append(msg.extraComment()); |
|
119 |
emsg.setExtraComment(cmt); |
|
120 |
} |
|
121 |
if (msg.isUtf8() != emsg.isUtf8()) { |
|
122 |
emsg.setUtf8(true); |
|
123 |
emsg.setNonUtf8(true); |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
void Translator::append(const TranslatorMessage &msg) |
|
129 |
{ |
|
130 |
m_messages.append(msg); |
|
131 |
} |
|
132 |
||
133 |
void Translator::appendSorted(const TranslatorMessage &msg) |
|
134 |
{ |
|
135 |
int msgLine = msg.lineNumber(); |
|
136 |
if (msgLine < 0) { |
|
137 |
m_messages.append(msg); |
|
138 |
return; |
|
139 |
} |
|
140 |
||
141 |
int bestIdx = 0; // Best insertion point found so far |
|
142 |
int bestScore = 0; // Its category: 0 = no hit, 1 = pre or post, 2 = middle |
|
143 |
int bestSize = 0; // The length of the region. Longer is better within one category. |
|
144 |
||
145 |
// The insertion point to use should this region turn out to be the best one so far |
|
146 |
int thisIdx = 0; |
|
147 |
int thisScore = 0; |
|
148 |
int thisSize = 0; |
|
149 |
// Working vars |
|
150 |
int prevLine = 0; |
|
151 |
int curIdx = 0; |
|
152 |
foreach (const TranslatorMessage &mit, m_messages) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
bool sameFile = mit.fileName() == msg.fileName() && mit.context() == msg.context(); |
0 | 154 |
int curLine; |
155 |
if (sameFile && (curLine = mit.lineNumber()) >= prevLine) { |
|
156 |
if (msgLine >= prevLine && msgLine < curLine) { |
|
157 |
thisIdx = curIdx; |
|
158 |
thisScore = thisSize ? 2 : 1; |
|
159 |
} |
|
160 |
++thisSize; |
|
161 |
prevLine = curLine; |
|
162 |
} else { |
|
163 |
if (thisSize) { |
|
164 |
if (!thisScore) { |
|
165 |
thisIdx = curIdx; |
|
166 |
thisScore = 1; |
|
167 |
} |
|
168 |
if (thisScore > bestScore || (thisScore == bestScore && thisSize > bestSize)) { |
|
169 |
bestIdx = thisIdx; |
|
170 |
bestScore = thisScore; |
|
171 |
bestSize = thisSize; |
|
172 |
} |
|
173 |
thisScore = 0; |
|
174 |
thisSize = sameFile ? 1 : 0; |
|
175 |
prevLine = 0; |
|
176 |
} |
|
177 |
} |
|
178 |
++curIdx; |
|
179 |
} |
|
180 |
if (thisSize && !thisScore) { |
|
181 |
thisIdx = curIdx; |
|
182 |
thisScore = 1; |
|
183 |
} |
|
184 |
if (thisScore > bestScore || (thisScore == bestScore && thisSize > bestSize)) |
|
185 |
m_messages.insert(thisIdx, msg); |
|
186 |
else if (bestScore) |
|
187 |
m_messages.insert(bestIdx, msg); |
|
188 |
else |
|
189 |
m_messages.append(msg); |
|
190 |
} |
|
191 |
||
192 |
static QString guessFormat(const QString &filename, const QString &format) |
|
193 |
{ |
|
194 |
if (format != QLatin1String("auto")) |
|
195 |
return format; |
|
196 |
||
197 |
foreach (const Translator::FileFormat &fmt, Translator::registeredFileFormats()) { |
|
198 |
if (filename.endsWith(QLatin1Char('.') + fmt.extension, Qt::CaseInsensitive)) |
|
199 |
return fmt.extension; |
|
200 |
} |
|
201 |
||
202 |
// the default format. |
|
203 |
// FIXME: change to something more widely distributed later. |
|
204 |
return QLatin1String("ts"); |
|
205 |
} |
|
206 |
||
207 |
bool Translator::load(const QString &filename, ConversionData &cd, const QString &format) |
|
208 |
{ |
|
209 |
cd.m_sourceDir = QFileInfo(filename).absoluteDir(); |
|
210 |
cd.m_sourceFileName = filename; |
|
211 |
||
212 |
QFile file; |
|
213 |
if (filename.isEmpty() || filename == QLatin1String("-")) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
214 |
#ifdef Q_OS_WIN |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
215 |
// QFile is broken for text files |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
216 |
::_setmode(0, _O_BINARY); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
217 |
#endif |
0 | 218 |
if (!file.open(stdin, QIODevice::ReadOnly)) { |
219 |
cd.appendError(QString::fromLatin1("Cannot open stdin!? (%1)") |
|
220 |
.arg(file.errorString())); |
|
221 |
return false; |
|
222 |
} |
|
223 |
} else { |
|
224 |
file.setFileName(filename); |
|
225 |
if (!file.open(QIODevice::ReadOnly)) { |
|
226 |
cd.appendError(QString::fromLatin1("Cannot open %1: %2") |
|
227 |
.arg(filename, file.errorString())); |
|
228 |
return false; |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
QString fmt = guessFormat(filename, format); |
|
233 |
||
234 |
foreach (const FileFormat &format, registeredFileFormats()) { |
|
235 |
if (fmt == format.extension) { |
|
236 |
if (format.loader) |
|
237 |
return (*format.loader)(*this, file, cd); |
|
238 |
cd.appendError(QString(QLatin1String("No loader for format %1 found")) |
|
239 |
.arg(fmt)); |
|
240 |
return false; |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
cd.appendError(QString(QLatin1String("Unknown format %1 for file %2")) |
|
245 |
.arg(format, filename)); |
|
246 |
return false; |
|
247 |
} |
|
248 |
||
249 |
||
250 |
bool Translator::save(const QString &filename, ConversionData &cd, const QString &format) const |
|
251 |
{ |
|
252 |
QFile file; |
|
253 |
if (filename.isEmpty() || filename == QLatin1String("-")) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
254 |
#ifdef Q_OS_WIN |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
255 |
// QFile is broken for text files |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
256 |
::_setmode(1, _O_BINARY); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
257 |
#endif |
0 | 258 |
if (!file.open(stdout, QIODevice::WriteOnly)) { |
259 |
cd.appendError(QString::fromLatin1("Cannot open stdout!? (%1)") |
|
260 |
.arg(file.errorString())); |
|
261 |
return false; |
|
262 |
} |
|
263 |
} else { |
|
264 |
file.setFileName(filename); |
|
265 |
if (!file.open(QIODevice::WriteOnly)) { |
|
266 |
cd.appendError(QString::fromLatin1("Cannot create %1: %2") |
|
267 |
.arg(filename, file.errorString())); |
|
268 |
return false; |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
QString fmt = guessFormat(filename, format); |
|
273 |
cd.m_targetDir = QFileInfo(filename).absoluteDir(); |
|
274 |
||
275 |
foreach (const FileFormat &format, registeredFileFormats()) { |
|
276 |
if (fmt == format.extension) { |
|
277 |
if (format.saver) |
|
278 |
return (*format.saver)(*this, file, cd); |
|
279 |
cd.appendError(QString(QLatin1String("Cannot save %1 files")).arg(fmt)); |
|
280 |
return false; |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
cd.appendError(QString(QLatin1String("Unknown format %1 for file %2")) |
|
285 |
.arg(format).arg(filename)); |
|
286 |
return false; |
|
287 |
} |
|
288 |
||
289 |
QString Translator::makeLanguageCode(QLocale::Language language, QLocale::Country country) |
|
290 |
{ |
|
291 |
QLocale locale(language, country); |
|
292 |
if (country == QLocale::AnyCountry) { |
|
293 |
QString languageCode = locale.name().section(QLatin1Char('_'), 0, 0); |
|
294 |
if (languageCode.length() <= 3) |
|
295 |
return languageCode; |
|
296 |
return QString(); |
|
297 |
} else { |
|
298 |
return locale.name(); |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
void Translator::languageAndCountry(const QString &languageCode, |
|
303 |
QLocale::Language *lang, QLocale::Country *country) |
|
304 |
{ |
|
305 |
QLocale locale(languageCode); |
|
306 |
if (lang) |
|
307 |
*lang = locale.language(); |
|
308 |
||
309 |
if (country) { |
|
310 |
if (languageCode.indexOf(QLatin1Char('_')) != -1) |
|
311 |
*country = locale.country(); |
|
312 |
else |
|
313 |
*country = QLocale::AnyCountry; |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
bool Translator::release(QFile *iod, ConversionData &cd) const |
|
318 |
{ |
|
319 |
foreach (const FileFormat &format, registeredFileFormats()) { |
|
320 |
if (format.extension == QLatin1String("qm")) |
|
321 |
return (*format.saver)(*this, *iod, cd); |
|
322 |
} |
|
323 |
cd.appendError(QLatin1String("No .qm saver available.")); |
|
324 |
return false; |
|
325 |
} |
|
326 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
327 |
int Translator::find(const TranslatorMessage &msg) const |
0 | 328 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
329 |
for (int i = 0; i < m_messages.count(); ++i) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
330 |
const TranslatorMessage &tmsg = m_messages.at(i); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
331 |
if (msg.id().isEmpty() || tmsg.id().isEmpty()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
332 |
if (msg.context() == tmsg.context() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
333 |
&& msg.sourceText() == tmsg.sourceText() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
334 |
&& msg.comment() == tmsg.comment()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
335 |
return i; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
336 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
337 |
if (msg.id() == tmsg.id()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
338 |
return i; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
339 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
340 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
341 |
return -1; |
0 | 342 |
} |
343 |
||
344 |
TranslatorMessage Translator::find(const QString &context, |
|
345 |
const QString &comment, const TranslatorMessage::References &refs) const |
|
346 |
{ |
|
347 |
if (!refs.isEmpty()) { |
|
348 |
for (TMM::ConstIterator it = m_messages.constBegin(); it != m_messages.constEnd(); ++it) { |
|
349 |
if (it->context() == context && it->comment() == comment) |
|
350 |
foreach (const TranslatorMessage::Reference &itref, it->allReferences()) |
|
351 |
foreach (const TranslatorMessage::Reference &ref, refs) |
|
352 |
if (itref == ref) |
|
353 |
return *it; |
|
354 |
} |
|
355 |
} |
|
356 |
return TranslatorMessage(); |
|
357 |
} |
|
358 |
||
359 |
bool Translator::contains(const QString &context) const |
|
360 |
{ |
|
361 |
foreach (const TranslatorMessage &msg, m_messages) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
362 |
if (msg.context() == context && msg.sourceText().isEmpty() && msg.id().isEmpty()) |
0 | 363 |
return true; |
364 |
return false; |
|
365 |
} |
|
366 |
||
367 |
TranslatorMessage Translator::find(const QString &context) const |
|
368 |
{ |
|
369 |
foreach (const TranslatorMessage &msg, m_messages) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
370 |
if (msg.context() == context && msg.sourceText().isEmpty() && msg.id().isEmpty()) |
0 | 371 |
return msg; |
372 |
return TranslatorMessage(); |
|
373 |
} |
|
374 |
||
375 |
void Translator::stripObsoleteMessages() |
|
376 |
{ |
|
377 |
TMM newmm; |
|
378 |
for (TMM::ConstIterator it = m_messages.begin(); it != m_messages.end(); ++it) |
|
379 |
if (it->type() != TranslatorMessage::Obsolete) |
|
380 |
newmm.append(*it); |
|
381 |
m_messages = newmm; |
|
382 |
} |
|
383 |
||
384 |
void Translator::stripFinishedMessages() |
|
385 |
{ |
|
386 |
TMM newmm; |
|
387 |
for (TMM::ConstIterator it = m_messages.begin(); it != m_messages.end(); ++it) |
|
388 |
if (it->type() != TranslatorMessage::Finished) |
|
389 |
newmm.append(*it); |
|
390 |
m_messages = newmm; |
|
391 |
} |
|
392 |
||
393 |
void Translator::stripEmptyContexts() |
|
394 |
{ |
|
395 |
TMM newmm; |
|
396 |
for (TMM::ConstIterator it = m_messages.begin(); it != m_messages.end(); ++it) |
|
397 |
if (it->sourceText() != QLatin1String(ContextComment)) |
|
398 |
newmm.append(*it); |
|
399 |
m_messages = newmm; |
|
400 |
} |
|
401 |
||
402 |
void Translator::stripNonPluralForms() |
|
403 |
{ |
|
404 |
TMM newmm; |
|
405 |
for (TMM::ConstIterator it = m_messages.begin(); it != m_messages.end(); ++it) |
|
406 |
if (it->isPlural()) |
|
407 |
newmm.append(*it); |
|
408 |
m_messages = newmm; |
|
409 |
} |
|
410 |
||
411 |
void Translator::stripIdenticalSourceTranslations() |
|
412 |
{ |
|
413 |
TMM newmm; |
|
414 |
for (TMM::ConstIterator it = m_messages.begin(); it != m_messages.end(); ++it) { |
|
415 |
// we need to have just one translation, and it be equal to the source |
|
416 |
if (it->translations().count() != 1) |
|
417 |
newmm.append(*it); |
|
418 |
else if (it->translation() != it->sourceText()) |
|
419 |
newmm.append(*it); |
|
420 |
} |
|
421 |
m_messages = newmm; |
|
422 |
} |
|
423 |
||
424 |
void Translator::dropTranslations() |
|
425 |
{ |
|
426 |
for (TMM::Iterator it = m_messages.begin(); it != m_messages.end(); ++it) { |
|
427 |
if (it->type() == TranslatorMessage::Finished) |
|
428 |
it->setType(TranslatorMessage::Unfinished); |
|
429 |
it->setTranslation(QString()); |
|
430 |
} |
|
431 |
} |
|
432 |
||
433 |
void Translator::dropUiLines() |
|
434 |
{ |
|
435 |
QString uiXt = QLatin1String(".ui"); |
|
436 |
QString juiXt = QLatin1String(".jui"); |
|
437 |
for (TMM::Iterator it = m_messages.begin(); it != m_messages.end(); ++it) { |
|
438 |
QHash<QString, int> have; |
|
439 |
QList<TranslatorMessage::Reference> refs; |
|
440 |
foreach (const TranslatorMessage::Reference &itref, it->allReferences()) { |
|
441 |
const QString &fn = itref.fileName(); |
|
442 |
if (fn.endsWith(uiXt) || fn.endsWith(juiXt)) { |
|
443 |
if (++have[fn] == 1) |
|
444 |
refs.append(TranslatorMessage::Reference(fn, -1)); |
|
445 |
} else { |
|
446 |
refs.append(itref); |
|
447 |
} |
|
448 |
} |
|
449 |
it->setReferences(refs); |
|
450 |
} |
|
451 |
} |
|
452 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
453 |
struct TranslatorMessageIdPtr { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
454 |
explicit TranslatorMessageIdPtr(const TranslatorMessage &tm) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
455 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
456 |
ptr = &tm; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
457 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
458 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
459 |
inline const TranslatorMessage *operator->() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
460 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
461 |
return ptr; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
462 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
463 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
464 |
const TranslatorMessage *ptr; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
465 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
466 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
467 |
Q_DECLARE_TYPEINFO(TranslatorMessageIdPtr, Q_MOVABLE_TYPE); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
468 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
469 |
inline int qHash(TranslatorMessageIdPtr tmp) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
470 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
471 |
return qHash(tmp->id()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
472 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
473 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
474 |
inline bool operator==(TranslatorMessageIdPtr tmp1, TranslatorMessageIdPtr tmp2) |
0 | 475 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
476 |
return tmp1->id() == tmp2->id(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
477 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
478 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
479 |
struct TranslatorMessageContentPtr { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
480 |
explicit TranslatorMessageContentPtr(const TranslatorMessage &tm) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
481 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
482 |
ptr = &tm; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
483 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
484 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
485 |
inline const TranslatorMessage *operator->() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
486 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
487 |
return ptr; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
488 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
489 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
490 |
const TranslatorMessage *ptr; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
491 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
492 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
493 |
Q_DECLARE_TYPEINFO(TranslatorMessageContentPtr, Q_MOVABLE_TYPE); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
494 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
495 |
inline int qHash(TranslatorMessageContentPtr tmp) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
496 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
497 |
int hash = qHash(tmp->context()) ^ qHash(tmp->sourceText()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
498 |
if (!tmp->sourceText().isEmpty()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
499 |
// Special treatment for context comments (empty source). |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
500 |
hash ^= qHash(tmp->comment()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
501 |
return hash; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
502 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
503 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
504 |
inline bool operator==(TranslatorMessageContentPtr tmp1, TranslatorMessageContentPtr tmp2) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
505 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
506 |
if (tmp1->context() != tmp2->context() || tmp1->sourceText() != tmp2->sourceText()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
507 |
return false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
// Special treatment for context comments (empty source). |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
509 |
if (tmp1->sourceText().isEmpty()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
510 |
return true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
511 |
return tmp1->comment() == tmp2->comment(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
512 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
513 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
514 |
Translator::Duplicates Translator::resolveDuplicates() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
515 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
516 |
Duplicates dups; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
517 |
QHash<TranslatorMessageIdPtr, int> idRefs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
518 |
QHash<TranslatorMessageContentPtr, int> contentRefs; |
0 | 519 |
for (int i = 0; i < m_messages.count();) { |
520 |
const TranslatorMessage &msg = m_messages.at(i); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
521 |
TranslatorMessage *omsg; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
522 |
int oi; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
523 |
QSet<int> *pDup; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
524 |
if (!msg.id().isEmpty()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
525 |
QHash<TranslatorMessageIdPtr, int>::ConstIterator it = |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
526 |
idRefs.constFind(TranslatorMessageIdPtr(msg)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
527 |
if (it != idRefs.constEnd()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
528 |
oi = *it; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
529 |
omsg = &m_messages[oi]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
530 |
pDup = &dups.byId; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
531 |
goto gotDupe; |
0 | 532 |
} |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
533 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
534 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
535 |
QHash<TranslatorMessageContentPtr, int>::ConstIterator it = |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
536 |
contentRefs.constFind(TranslatorMessageContentPtr(msg)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
537 |
if (it != contentRefs.constEnd()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
538 |
oi = *it; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
539 |
omsg = &m_messages[oi]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
540 |
if (msg.id().isEmpty() || omsg->id().isEmpty()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
541 |
if (!msg.id().isEmpty() && omsg->id().isEmpty()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
542 |
omsg->setId(msg.id()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
543 |
idRefs[TranslatorMessageIdPtr(*omsg)] = oi; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
544 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
545 |
pDup = &dups.byContents; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
546 |
goto gotDupe; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
547 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
548 |
// This is really a content dupe, but with two distinct IDs. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
549 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
550 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
551 |
if (!msg.id().isEmpty()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
552 |
idRefs[TranslatorMessageIdPtr(msg)] = i; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
553 |
contentRefs[TranslatorMessageContentPtr(msg)] = i; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
554 |
++i; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
555 |
continue; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
556 |
gotDupe: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
557 |
if (omsg->isUtf8() != msg.isUtf8() && !omsg->isNonUtf8()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
558 |
// Dual-encoded message |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
559 |
omsg->setUtf8(true); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
560 |
omsg->setNonUtf8(true); |
0 | 561 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
562 |
// Duplicate |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
563 |
pDup->insert(oi); |
0 | 564 |
} |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
565 |
if (!omsg->isTranslated() && msg.isTranslated()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
566 |
omsg->setTranslations(msg.translations()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
567 |
m_messages.removeAt(i); |
0 | 568 |
} |
569 |
return dups; |
|
570 |
} |
|
571 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
572 |
void Translator::reportDuplicates(const Duplicates &dupes, |
0 | 573 |
const QString &fileName, bool verbose) |
574 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
575 |
if (!dupes.byId.isEmpty() || !dupes.byContents.isEmpty()) { |
0 | 576 |
if (!verbose) { |
577 |
qWarning("Warning: dropping duplicate messages in '%s'\n(try -verbose for more info).", |
|
578 |
qPrintable(fileName)); |
|
579 |
} else { |
|
580 |
qWarning("Warning: dropping duplicate messages in '%s':", qPrintable(fileName)); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
581 |
foreach (int i, dupes.byId) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
582 |
qWarning("\n* ID: %s", qPrintable(message(i).id())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
583 |
foreach (int j, dupes.byContents) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
584 |
const TranslatorMessage &msg = message(j); |
0 | 585 |
qWarning("\n* Context: %s\n* Source: %s", |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
586 |
qPrintable(msg.context()), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
587 |
qPrintable(msg.sourceText())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
588 |
if (!msg.comment().isEmpty()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
589 |
qWarning("* Comment: %s", qPrintable(msg.comment())); |
0 | 590 |
} |
591 |
qWarning(); |
|
592 |
} |
|
593 |
} |
|
594 |
} |
|
595 |
||
596 |
// Used by lupdate to be able to search using absolute paths during merging |
|
597 |
void Translator::makeFileNamesAbsolute(const QDir &originalPath) |
|
598 |
{ |
|
599 |
TMM newmm; |
|
600 |
for (TMM::iterator it = m_messages.begin(); it != m_messages.end(); ++it) { |
|
601 |
TranslatorMessage msg = *it; |
|
602 |
msg.setReferences(TranslatorMessage::References()); |
|
603 |
foreach (const TranslatorMessage::Reference &ref, it->allReferences()) { |
|
604 |
QString fileName = ref.fileName(); |
|
605 |
QFileInfo fi (fileName); |
|
606 |
if (fi.isRelative()) |
|
607 |
fileName = originalPath.absoluteFilePath(fileName); |
|
608 |
msg.addReference(fileName, ref.lineNumber()); |
|
609 |
} |
|
610 |
newmm.append(msg); |
|
611 |
} |
|
612 |
m_messages = newmm; |
|
613 |
} |
|
614 |
||
615 |
QList<TranslatorMessage> Translator::messages() const |
|
616 |
{ |
|
617 |
return m_messages; |
|
618 |
} |
|
619 |
||
620 |
QList<TranslatorMessage> Translator::translatedMessages() const |
|
621 |
{ |
|
622 |
TMM result; |
|
623 |
for (TMM::ConstIterator it = m_messages.begin(); it != m_messages.end(); ++it) |
|
624 |
if (it->type() == TranslatorMessage::Finished) |
|
625 |
result.append(*it); |
|
626 |
return result; |
|
627 |
} |
|
628 |
||
629 |
QStringList Translator::normalizedTranslations(const TranslatorMessage &msg, int numPlurals) |
|
630 |
{ |
|
631 |
QStringList translations = msg.translations(); |
|
632 |
int numTranslations = msg.isPlural() ? numPlurals : 1; |
|
633 |
||
634 |
// make sure that the stringlist always have the size of the |
|
635 |
// language's current numerus, or 1 if its not plural |
|
636 |
if (translations.count() > numTranslations) { |
|
637 |
for (int i = translations.count(); i > numTranslations; --i) |
|
638 |
translations.removeLast(); |
|
639 |
} else if (translations.count() < numTranslations) { |
|
640 |
for (int i = translations.count(); i < numTranslations; ++i) |
|
641 |
translations.append(QString()); |
|
642 |
} |
|
643 |
return translations; |
|
644 |
} |
|
645 |
||
646 |
void Translator::normalizeTranslations(ConversionData &cd) |
|
647 |
{ |
|
648 |
bool truncated = false; |
|
649 |
QLocale::Language l; |
|
650 |
QLocale::Country c; |
|
651 |
languageAndCountry(languageCode(), &l, &c); |
|
652 |
int numPlurals = 1; |
|
653 |
if (l != QLocale::C) { |
|
654 |
QStringList forms; |
|
655 |
if (getNumerusInfo(l, c, 0, &forms)) |
|
656 |
numPlurals = forms.count(); // includes singular |
|
657 |
} |
|
658 |
for (int i = 0; i < m_messages.count(); ++i) { |
|
659 |
const TranslatorMessage &msg = m_messages.at(i); |
|
660 |
QStringList tlns = msg.translations(); |
|
661 |
int ccnt = msg.isPlural() ? numPlurals : 1; |
|
662 |
if (tlns.count() != ccnt) { |
|
663 |
while (tlns.count() < ccnt) |
|
664 |
tlns.append(QString()); |
|
665 |
while (tlns.count() > ccnt) { |
|
666 |
tlns.removeLast(); |
|
667 |
truncated = true; |
|
668 |
} |
|
669 |
TranslatorMessage msg2(msg); |
|
670 |
msg2.setTranslations(tlns); |
|
671 |
m_messages[i] = msg2; |
|
672 |
} |
|
673 |
} |
|
674 |
if (truncated) |
|
675 |
cd.appendError(QLatin1String( |
|
676 |
"Removed plural forms as the target language has less " |
|
677 |
"forms.\nIf this sounds wrong, possibly the target language is " |
|
678 |
"not set or recognized.\n")); |
|
679 |
} |
|
680 |
||
681 |
QString Translator::guessLanguageCodeFromFileName(const QString &filename) |
|
682 |
{ |
|
683 |
QString str = filename; |
|
684 |
foreach (const FileFormat &format, registeredFileFormats()) { |
|
685 |
if (str.endsWith(format.extension)) { |
|
686 |
str = str.left(str.size() - format.extension.size() - 1); |
|
687 |
break; |
|
688 |
} |
|
689 |
} |
|
690 |
static QRegExp re(QLatin1String("[\\._]")); |
|
691 |
while (true) { |
|
692 |
QLocale locale(str); |
|
693 |
//qDebug() << "LANGUAGE FROM " << str << "LANG: " << locale.language(); |
|
694 |
if (locale.language() != QLocale::C) { |
|
695 |
//qDebug() << "FOUND " << locale.name(); |
|
696 |
return locale.name(); |
|
697 |
} |
|
698 |
int pos = str.indexOf(re); |
|
699 |
if (pos == -1) |
|
700 |
break; |
|
701 |
str = str.mid(pos + 1); |
|
702 |
} |
|
703 |
//qDebug() << "LANGUAGE GUESSING UNSUCCESSFUL"; |
|
704 |
return QString(); |
|
705 |
} |
|
706 |
||
707 |
bool Translator::hasExtra(const QString &key) const |
|
708 |
{ |
|
709 |
return m_extra.contains(key); |
|
710 |
} |
|
711 |
||
712 |
QString Translator::extra(const QString &key) const |
|
713 |
{ |
|
714 |
return m_extra[key]; |
|
715 |
} |
|
716 |
||
717 |
void Translator::setExtra(const QString &key, const QString &value) |
|
718 |
{ |
|
719 |
m_extra[key] = value; |
|
720 |
} |
|
721 |
||
722 |
void Translator::setCodecName(const QByteArray &name) |
|
723 |
{ |
|
724 |
QTextCodec *codec = QTextCodec::codecForName(name); |
|
725 |
if (!codec) { |
|
726 |
if (!name.isEmpty()) |
|
727 |
qWarning("No QTextCodec for %s available. Using Latin1\n", name.constData()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
728 |
m_codec = QTextCodec::codecForName("ISO-8859-1"); |
0 | 729 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
730 |
m_codec = codec; |
0 | 731 |
} |
732 |
} |
|
733 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
734 |
QByteArray Translator::codecName() const |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
735 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
736 |
return m_codec->name(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
737 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
738 |
|
0 | 739 |
void Translator::dump() const |
740 |
{ |
|
741 |
for (int i = 0; i != messageCount(); ++i) |
|
742 |
message(i).dump(); |
|
743 |
} |
|
744 |
||
745 |
QT_END_NAMESPACE |