1 /////////////////////////////////////////////////////////////////////////////
4 // Description : This is a duplication of Qt tool for resources compiling
5 /////////////////////////////////////////////////////////////////////////////
7 /****************************************************************************
10 ** This is a utility program for converting findtr msgfiles to
11 ** qtranslator message files
14 ** Copyright (C) 1998 by Trolltech AS. All rights reserved.
16 *****************************************************************************/
19 #include <qtextstream.h>
20 #include <qtextcodec.h>
21 #include <qtranslator.h>
27 static QString* defaultScope = 0;
29 bool hasHandle( const QString& line, const QString& handle)
31 return line.left(handle.length()) == handle;
35 QString extractContents( const QString& line )
38 if ( line.contains('\"') < 2)
41 while ( pos < int(line.length()) && line[pos] != '\"' )
44 while ( pos < int(line.length()) && line[pos] != '\"' ) {
45 // 0xa5: the yen sign is the Japanese backslash
46 if ( line[pos] == '\\' || line[pos] == QChar(0xa5) ) {
48 switch (char(line[pos]) ) {
71 contents += char(line[pos]);
76 contents += line[pos];
83 void addTranslation( QTranslator* translator, const QString& msgid, const QString& msgstr)
85 if (!msgid.isNull() && !msgstr.isNull() ) {
88 int coloncolon = msgid.find("::");
89 if (coloncolon != -1) {
90 scope = msgid.left( coloncolon );
91 id = msgid.right( msgid.length() - scope.length() - 2 );
93 else if (defaultScope)
94 scope = *defaultScope;
96 if (translator->contains( scope.ascii(), id.ascii() ) ) {
97 qWarning("Error: \"%s\" already in use", msgid.ascii() );
100 translator->insert( scope.latin1(), id.latin1(), msgstr );
107 void translate( const QString& filename, const QString& qmfile )
110 if ( !f.open( IO_ReadOnly) )
112 QTranslator* translator = new QTranslator(0);
113 QTextCodec *codec = 0;
114 for (int pass = 0; pass < 2; pass++) {
123 while ( !t.atEnd() || !line.isEmpty() ) {
124 if (line.isEmpty()) {
128 if ( hasHandle( line, "msgid") ) {
129 msgstr = QString::null;
130 msgid = extractContents( line );
136 line = QString::null;
137 while ( hasHandle( line, "\"") ) {
138 msgid += extractContents( line );
144 line = QString::null;
147 else if ( hasHandle( line, "msgstr") ) {
148 msgstr = extractContents( line );
154 line = QString::null;
155 while ( hasHandle( line, "\"") ) {
156 msgstr += extractContents( line );
162 line = QString::null;
165 addTranslation( translator, msgid, msgstr);
167 if ( pass == 0 && msgid.isEmpty() ) {
168 // Check for the encoding.
169 int cpos = msgstr.find( "charset=" );
171 cpos = cpos + 8; //skip "charset="
173 int len = msgstr.length();
174 while ( i < len && !msgstr[i].isSpace() )
176 QString charset = msgstr.mid( cpos, i-cpos );
177 codec = QTextCodec::codecForName( charset.ascii() );
179 debug( "PO file character set: %s. Codec: %s",
180 charset.ascii(), codec->name() );
182 debug( "No codec for %s", charset.ascii() );
189 line = QString::null;
193 translator->save( qmfile );
197 // workaround for BCC problem, qtranslator.h includes qwindowdefs.h via qobject.h, see NEEDS_QMAIN
202 int main( int argc, char* argv[] )
207 if ( QString("-scope") == argv[1] ) {
208 defaultScope = new QString(argv[2]);
213 if ( argc <= infile ) {
214 printf("usage: %s [-scope default] infile [outfile]\n", argv[0]);
218 translate(argv[infile], argc > infile+1 ? argv[infile+1] : "tr.qm");