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