summaryrefslogtreecommitdiff
path: root/development/translation/shared/metatranslator.cpp
Unidiff
Diffstat (limited to 'development/translation/shared/metatranslator.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--development/translation/shared/metatranslator.cpp586
1 files changed, 586 insertions, 0 deletions
diff --git a/development/translation/shared/metatranslator.cpp b/development/translation/shared/metatranslator.cpp
new file mode 100644
index 0000000..a01e1eb
--- a/dev/null
+++ b/development/translation/shared/metatranslator.cpp
@@ -0,0 +1,586 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of Qt Linguist.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "metatranslator.h"
22
23#include <qapplication.h>
24#include <qcstring.h>
25#include <qfile.h>
26#include <qmessagebox.h>
27#include <qtextcodec.h>
28#include <qtextstream.h>
29#include <qxml.h>
30
31static bool encodingIsUtf8( const QXmlAttributes& atts )
32{
33 for ( int i = 0; i < atts.length(); i++ ) {
34 // utf8="true" is a pre-3.0 syntax
35 if ( atts.qName(i) == QString("utf8") ) {
36 return ( atts.value(i) == QString("true") );
37 } else if ( atts.qName(i) == QString("encoding") ) {
38 return ( atts.value(i) == QString("UTF-8") );
39 }
40 }
41 return FALSE;
42}
43
44class TsHandler : public QXmlDefaultHandler
45{
46public:
47 TsHandler( MetaTranslator *translator )
48 : tor( translator ), type( MetaTranslatorMessage::Finished ),
49 inMessage( FALSE ), ferrorCount( 0 ), contextIsUtf8( FALSE ),
50 messageIsUtf8( FALSE ) { }
51
52 virtual bool startElement( const QString& namespaceURI,
53 const QString& localName, const QString& qName,
54 const QXmlAttributes& atts );
55 virtual bool endElement( const QString& namespaceURI,
56 const QString& localName, const QString& qName );
57 virtual bool characters( const QString& ch );
58 virtual bool fatalError( const QXmlParseException& exception );
59
60private:
61 MetaTranslator *tor;
62 MetaTranslatorMessage::Type type;
63 bool inMessage;
64 QString context;
65 QString source;
66 QString comment;
67 QString translation;
68
69 QString accum;
70 int ferrorCount;
71 bool contextIsUtf8;
72 bool messageIsUtf8;
73};
74
75bool TsHandler::startElement( const QString& /* namespaceURI */,
76 const QString& /* localName */,
77 const QString& qName,
78 const QXmlAttributes& atts )
79{
80 if ( qName == QString("byte") ) {
81 for ( int i = 0; i < atts.length(); i++ ) {
82 if ( atts.qName(i) == QString("value") ) {
83 QString value = atts.value( i );
84 int base = 10;
85 if ( value.startsWith("x") ) {
86 base = 16;
87 value = value.mid( 1 );
88 }
89 int n = value.toUInt( 0, base );
90 if ( n != 0 )
91 accum += QChar( n );
92 }
93 }
94 } else {
95 if ( qName == QString("context") ) {
96 context.truncate( 0 );
97 source.truncate( 0 );
98 comment.truncate( 0 );
99 translation.truncate( 0 );
100 contextIsUtf8 = encodingIsUtf8( atts );
101 } else if ( qName == QString("message") ) {
102 inMessage = TRUE;
103 type = MetaTranslatorMessage::Finished;
104 source.truncate( 0 );
105 comment.truncate( 0 );
106 translation.truncate( 0 );
107 messageIsUtf8 = encodingIsUtf8( atts );
108 } else if ( qName == QString("translation") ) {
109 for ( int i = 0; i < atts.length(); i++ ) {
110 if ( atts.qName(i) == QString("type") ) {
111 if ( atts.value(i) == QString("unfinished") )
112 type = MetaTranslatorMessage::Unfinished;
113 else if ( atts.value(i) == QString("obsolete") )
114 type = MetaTranslatorMessage::Obsolete;
115 else
116 type = MetaTranslatorMessage::Finished;
117 }
118 }
119 }
120 accum.truncate( 0 );
121 }
122 return TRUE;
123}
124
125bool TsHandler::endElement( const QString& /* namespaceURI */,
126 const QString& /* localName */,
127 const QString& qName )
128{
129 if ( qName == QString("codec") || qName == QString("defaultcodec") ) {
130 // "codec" is a pre-3.0 syntax
131 tor->setCodec( accum );
132 } else if ( qName == QString("name") ) {
133 context = accum;
134 } else if ( qName == QString("source") ) {
135 source = accum;
136 } else if ( qName == QString("comment") ) {
137 if ( inMessage ) {
138 comment = accum;
139 } else {
140 if ( contextIsUtf8 )
141 tor->insert( MetaTranslatorMessage(context.utf8(), "",
142 accum.utf8(), QString::null, TRUE,
143 MetaTranslatorMessage::Unfinished) );
144 else
145 tor->insert( MetaTranslatorMessage(context.ascii(), "",
146 accum.ascii(), QString::null, FALSE,
147 MetaTranslatorMessage::Unfinished) );
148 }
149 } else if ( qName == QString("translation") ) {
150 translation = accum;
151 } else if ( qName == QString("message") ) {
152 if ( messageIsUtf8 )
153 tor->insert( MetaTranslatorMessage(context.utf8(), source.utf8(),
154 comment.utf8(), translation,
155 TRUE, type) );
156 else
157 tor->insert( MetaTranslatorMessage(context.ascii(), source.ascii(),
158 comment.ascii(), translation,
159 FALSE, type) );
160 inMessage = FALSE;
161 }
162 return TRUE;
163}
164
165bool TsHandler::characters( const QString& ch )
166{
167 QString t = ch;
168 t.replace( "\r", "" );
169 accum += t;
170 return TRUE;
171}
172
173bool TsHandler::fatalError( const QXmlParseException& exception )
174{
175 if ( ferrorCount++ == 0 ) {
176 QString msg;
177 msg.sprintf( "Parse error at line %d, column %d (%s).",
178 exception.lineNumber(), exception.columnNumber(),
179 exception.message().latin1() );
180 if ( qApp == 0 )
181 fprintf( stderr, "XML error: %s\n", msg.latin1() );
182 else
183 QMessageBox::information( qApp->mainWidget(),
184 QObject::tr("Qt Linguist"), msg );
185 }
186 return FALSE;
187}
188
189static QString numericEntity( int ch )
190{
191 return QString( ch <= 0x20 ? "<byte value=\"x%1\"/>" : "&#x%1;" )
192 .arg( ch, 0, 16 );
193}
194
195static QString protect( const QCString& str )
196{
197 QString result;
198 int len = (int) str.length();
199 for ( int k = 0; k < len; k++ ) {
200 switch( str[k] ) {
201 case '\"':
202 result += QString( "&quot;" );
203 break;
204 case '&':
205 result += QString( "&amp;" );
206 break;
207 case '>':
208 result += QString( "&gt;" );
209 break;
210 case '<':
211 result += QString( "&lt;" );
212 break;
213 case '\'':
214 result += QString( "&apos;" );
215 break;
216 default:
217 if ( (uchar) str[k] < 0x20 && str[k] != '\n' )
218 result += numericEntity( (uchar) str[k] );
219 else
220 result += str[k];
221 }
222 }
223 return result;
224}
225
226static QString evilBytes( const QCString& str, bool utf8 )
227{
228 if ( utf8 ) {
229 return protect( str );
230 } else {
231 QString result;
232 QCString t = protect( str ).latin1();
233 int len = (int) t.length();
234 for ( int k = 0; k < len; k++ ) {
235 if ( (uchar) t[k] >= 0x7f )
236 result += numericEntity( (uchar) t[k] );
237 else
238 result += QChar( t[k] );
239 }
240 return result;
241 }
242}
243
244MetaTranslatorMessage::MetaTranslatorMessage()
245 : utfeight( FALSE ), ty( Unfinished )
246{
247}
248
249MetaTranslatorMessage::MetaTranslatorMessage( const char *context,
250 const char *sourceText,
251 const char *comment,
252 const QString& translation,
253 bool utf8, Type type )
254 : QTranslatorMessage( context, sourceText, comment, translation ),
255 utfeight( FALSE ), ty( type )
256{
257 /*
258 Don't use UTF-8 if it makes no difference. UTF-8 should be
259 reserved for the real problematic case: non-ASCII (possibly
260 non-Latin-1) characters in .ui files.
261 */
262 if ( utf8 ) {
263 if ( sourceText != 0 ) {
264 int i = 0;
265 while ( sourceText[i] != '\0' ) {
266 if ( (uchar) sourceText[i] >= 0x80 ) {
267 utfeight = TRUE;
268 break;
269 }
270 i++;
271 }
272 }
273 if ( !utfeight && comment != 0 ) {
274 int i = 0;
275 while ( comment[i] != '\0' ) {
276 if ( (uchar) comment[i] >= 0x80 ) {
277 utfeight = TRUE;
278 break;
279 }
280 i++;
281 }
282 }
283 }
284}
285
286MetaTranslatorMessage::MetaTranslatorMessage( const MetaTranslatorMessage& m )
287 : QTranslatorMessage( m ), utfeight( m.utfeight ), ty( m.ty )
288{
289}
290
291MetaTranslatorMessage& MetaTranslatorMessage::operator=(
292 const MetaTranslatorMessage& m )
293{
294 QTranslatorMessage::operator=( m );
295 utfeight = m.utfeight;
296 ty = m.ty;
297 return *this;
298}
299
300bool MetaTranslatorMessage::operator==( const MetaTranslatorMessage& m ) const
301{
302 return qstrcmp( context(), m.context() ) == 0 &&
303 qstrcmp( sourceText(), m.sourceText() ) == 0 &&
304 qstrcmp( comment(), m.comment() ) == 0;
305}
306
307bool MetaTranslatorMessage::operator<( const MetaTranslatorMessage& m ) const
308{
309 int delta = qstrcmp( context(), m.context() );
310 if ( delta == 0 )
311 delta = qstrcmp( sourceText(), m.sourceText() );
312 if ( delta == 0 )
313 delta = qstrcmp( comment(), m.comment() );
314 return delta < 0;
315}
316
317MetaTranslator::MetaTranslator()
318 : codecName( "ISO-8859-1" ), codec( 0 )
319{
320}
321
322MetaTranslator::MetaTranslator( const MetaTranslator& tor )
323 : mm( tor.mm ), codecName( tor.codecName ), codec( tor.codec )
324{
325
326}
327
328MetaTranslator& MetaTranslator::operator=( const MetaTranslator& tor )
329{
330 mm = tor.mm;
331 codecName = tor.codecName;
332 codec = tor.codec;
333 return *this;
334}
335
336bool MetaTranslator::load( const QString& filename )
337{
338 mm.clear();
339
340 QFile f( filename );
341 if ( !f.open(IO_ReadOnly) )
342 return FALSE;
343
344 QTextStream t( &f );
345 QXmlInputSource in( t );
346 QXmlSimpleReader reader;
347 // don't click on these!
348 reader.setFeature( "http://xml.org/sax/features/namespaces", FALSE );
349 reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", TRUE );
350 reader.setFeature( "http://trolltech.com/xml/features/report-whitespace"
351 "-only-CharData", FALSE );
352 QXmlDefaultHandler *hand = new TsHandler( this );
353 reader.setContentHandler( hand );
354 reader.setErrorHandler( hand );
355
356 bool ok = reader.parse( in );
357 reader.setContentHandler( 0 );
358 reader.setErrorHandler( 0 );
359 delete hand;
360 f.close();
361 if ( !ok )
362 mm.clear();
363 return ok;
364}
365
366bool MetaTranslator::save( const QString& filename ) const
367{
368 QFile f( filename );
369 if ( !f.open(IO_WriteOnly) )
370 return FALSE;
371
372 QTextStream t( &f );
373 t.setCodec( QTextCodec::codecForName("ISO-8859-1") );
374
375 t << "<!DOCTYPE TS><TS>\n";
376 if ( codecName != "ISO-8859-1" )
377 t << "<defaultcodec>" << codecName << "</defaultcodec>\n";
378 TMM::ConstIterator m = mm.begin();
379 while ( m != mm.end() ) {
380 TMMInv inv;
381 TMMInv::Iterator i;
382 bool contextIsUtf8 = m.key().utf8();
383 QCString context = m.key().context();
384 QCString comment = "";
385
386 do {
387 if ( QCString(m.key().sourceText()).isEmpty() ) {
388 if ( m.key().type() != MetaTranslatorMessage::Obsolete ) {
389 contextIsUtf8 = m.key().utf8();
390 comment = QCString( m.key().comment() );
391 }
392 } else {
393 inv.insert( *m, m.key() );
394 }
395 } while ( ++m != mm.end() && QCString(m.key().context()) == context );
396
397 t << "<context";
398 if ( contextIsUtf8 )
399 t << " encoding=\"UTF-8\"";
400 t << ">\n";
401 t << " <name>" << evilBytes( context, contextIsUtf8 )
402 << "</name>\n";
403 if ( !comment.isEmpty() )
404 t << " <comment>" << evilBytes( comment, contextIsUtf8 )
405 << "</comment>\n";
406
407 for ( i = inv.begin(); i != inv.end(); ++i ) {
408 // no need for such noise
409 if ( (*i).type() == MetaTranslatorMessage::Obsolete &&
410 (*i).translation().isEmpty() )
411 continue;
412
413 t << " <message";
414 if ( (*i).utf8() )
415 t << " encoding=\"UTF-8\"";
416 t << ">\n"
417 << " <source>" << evilBytes( (*i).sourceText(),
418 (*i).utf8() )
419 << "</source>\n";
420 if ( !QCString((*i).comment()).isEmpty() )
421 t << " <comment>" << evilBytes( (*i).comment(),
422 (*i).utf8() )
423 << "</comment>\n";
424 t << " <translation";
425 if ( (*i).type() == MetaTranslatorMessage::Unfinished )
426 t << " type=\"unfinished\"";
427 else if ( (*i).type() == MetaTranslatorMessage::Obsolete )
428 t << " type=\"obsolete\"";
429 t << ">" << protect( (*i).translation().utf8() )
430 << "</translation>\n";
431 t << " </message>\n";
432 }
433 t << "</context>\n";
434 }
435 t << "</TS>\n";
436 f.close();
437 return TRUE;
438}
439
440bool MetaTranslator::release( const QString& filename, bool verbose ) const
441{
442 QTranslator tor( 0 );
443 int finished = 0;
444 int unfinished = 0;
445 int untranslated = 0;
446 TMM::ConstIterator m;
447
448 for ( m = mm.begin(); m != mm.end(); ++m ) {
449 if ( m.key().type() != MetaTranslatorMessage::Obsolete ) {
450 if ( m.key().translation().isEmpty() ) {
451 untranslated++;
452 } else {
453 if ( m.key().type() == MetaTranslatorMessage::Unfinished )
454 unfinished++;
455 else
456 finished++;
457
458 QCString context = m.key().context();
459 QCString sourceText = m.key().sourceText();
460 QCString comment = m.key().comment();
461 QString translation = m.key().translation();
462
463 /*
464 Drop the comment in (context, sourceText, comment),
465 unless (context, sourceText, "") already exists, or
466 unless we already dropped the comment of (context,
467 sourceText, comment0).
468 */
469 if ( comment.isEmpty()
470 || contains(context, sourceText, "")
471 || !tor.findMessage(context, sourceText, "").translation()
472 .isNull() ) {
473 tor.insert( m.key() );
474 } else {
475 tor.insert( QTranslatorMessage(context, sourceText, "",
476 translation) );
477 }
478 }
479 }
480 }
481
482 bool saved = tor.save( filename, QTranslator::Stripped );
483 if ( saved && verbose )
484 fprintf( stderr,
485 " %d finished, %d unfinished and %d untranslated messages\n",
486 finished, unfinished, untranslated );
487
488 return saved;
489}
490
491bool MetaTranslator::contains( const char *context, const char *sourceText,
492 const char *comment ) const
493{
494 return mm.find( MetaTranslatorMessage(context, sourceText, comment) ) !=
495 mm.end();
496}
497
498void MetaTranslator::insert( const MetaTranslatorMessage& m )
499{
500 int pos = mm.count();
501 TMM::Iterator n = mm.find( m );
502 if ( n != mm.end() )
503 pos = *n;
504 mm.replace( m, pos );
505}
506
507void MetaTranslator::stripObsoleteMessages()
508{
509 TMM newmm;
510
511 TMM::Iterator m = mm.begin();
512 while ( m != mm.end() ) {
513 if ( m.key().type() != MetaTranslatorMessage::Obsolete )
514 newmm.insert( m.key(), *m );
515 ++m;
516 }
517 mm = newmm;
518}
519
520void MetaTranslator::stripEmptyContexts()
521{
522 TMM newmm;
523
524 TMM::Iterator m = mm.begin();
525 while ( m != mm.end() ) {
526 if ( QCString(m.key().sourceText()).isEmpty() ) {
527 TMM::Iterator n = m;
528 ++n;
529 // the context comment is followed by other messages
530 if ( n != newmm.end() &&
531 qstrcmp(m.key().context(), n.key().context()) == 0 )
532 newmm.insert( m.key(), *m );
533 } else {
534 newmm.insert( m.key(), *m );
535 }
536 ++m;
537 }
538 mm = newmm;
539}
540
541void MetaTranslator::setCodec( const char *name )
542{
543 const int latin1 = 4;
544
545 codecName = name;
546 codec = QTextCodec::codecForName( name );
547 if ( codec == 0 || codec->mibEnum() == latin1 )
548 codec = 0;
549}
550
551QString MetaTranslator::toUnicode( const char *str, bool utf8 ) const
552{
553 if ( utf8 )
554 return QString::fromUtf8( str );
555 else if ( codec == 0 )
556 return QString( str );
557 else
558 return codec->toUnicode( str );
559}
560
561QValueList<MetaTranslatorMessage> MetaTranslator::messages() const
562{
563 int n = mm.count();
564 TMM::ConstIterator *t = new TMM::ConstIterator[n + 1];
565 TMM::ConstIterator m;
566 for ( m = mm.begin(); m != mm.end(); ++m )
567 t[*m] = m;
568
569 QValueList<MetaTranslatorMessage> val;
570 for ( int i = 0; i < n; i++ )
571 val.append( t[i].key() );
572
573 delete[] t;
574 return val;
575}
576
577QValueList<MetaTranslatorMessage> MetaTranslator::translatedMessages() const
578{
579 QValueList<MetaTranslatorMessage> val;
580 TMM::ConstIterator m;
581 for ( m = mm.begin(); m != mm.end(); ++m ) {
582 if ( m.key().type() == MetaTranslatorMessage::Finished )
583 val.append( m.key() );
584 }
585 return val;
586}