2 /****************************************************************************
5 ** This is a utility program for converting findtr msgfiles to
6 ** qtranslator message files
9 ** Copyright (C) 1998 by Trolltech AS. All rights reserved.
11 *****************************************************************************/
13 #include <qtextstream.h>
14 #include <qtextcodec.h>
15 #include <qtranslator.h>
20 static QString* defaultScope = 0;
22 bool hasHandle( const QString& line, const QString& handle)
24 return line.left(handle.length()) == handle;
28 QString extractContents( const QString& line )
31 if ( line.contains('\"') < 2)
34 while ( pos < int(line.length()) && line[pos] != '\"' )
37 while ( pos < int(line.length()) && line[pos] != '\"' ) {
38 if ( line[pos] == '\\') {
40 switch (char(line[pos]) ) {
63 contents += char(line[pos]);
68 contents += line[pos];
75 void addTranslation( QTranslator* translator, const QString& msgid, const QString& msgstr)
77 if (!msgid.isNull() && !msgstr.isNull() ) {
80 int coloncolon = msgid.find("::");
81 if (coloncolon != -1) {
82 scope = msgid.left( coloncolon );
83 id = msgid.right( msgid.length() - scope.length() - 2 );
85 else if (defaultScope)
86 scope = *defaultScope;
88 if (translator->contains( scope.ascii(), id.ascii() ) ) {
89 qWarning("Error: \"%s\" already in use", msgid.ascii() );
92 translator->insert( scope.latin1(), id.latin1(), msgstr );
99 void translate( const QString& filename, const QString& qmfile )
102 if ( !f.open( IO_ReadOnly) )
104 QTranslator* translator = new QTranslator(0);
105 QTextCodec *codec = 0;
106 for (int pass = 0; pass < 2; pass++) {
115 while ( !t.atEnd() || !line.isEmpty() ) {
116 if (line.isEmpty()) {
120 if ( hasHandle( line, "msgid") ) {
121 msgstr = QString::null;
122 msgid = extractContents( line );
128 line = QString::null;
129 while ( hasHandle( line, "\"") ) {
130 msgid += extractContents( line );
136 line = QString::null;
139 else if ( hasHandle( line, "msgstr") ) {
140 msgstr = extractContents( line );
146 line = QString::null;
147 while ( hasHandle( line, "\"") ) {
148 msgstr += extractContents( line );
154 line = QString::null;
157 addTranslation( translator, msgid, msgstr);
159 if ( pass == 0 && msgid.isEmpty() ) {
160 // Check for the encoding.
161 int cpos = msgstr.find( "charset=" );
163 cpos = cpos + 8; //skip "charset="
165 int len = msgstr.length();
166 while ( i < len && !msgstr[i].isSpace() )
168 QString charset = msgstr.mid( cpos, i-cpos );
169 codec = QTextCodec::codecForName( charset.ascii() );
171 debug( "PO file character set: %s. Codec: %s",
172 charset.ascii(), codec->name() );
174 debug( "No codec for %s", charset.ascii() );
181 line = QString::null;
185 translator->save( qmfile );
189 // workaround for BCC problem, qtranslator.h includes qwindowdefs.h via qobject.h, see NEEDS_QMAIN
194 int main( int argc, char* argv[] )
199 if ( QString("-scope") == argv[1] ) {
200 defaultScope = new QString(argv[2]);
205 if ( argc <= infile ) {
206 printf("usage: %s [-scope default] infile [outfile]\n", argv[0]);
210 translate(argv[infile], argc > infile+1 ? argv[infile+1] : "tr.qm");