1 // SALOME MSG2QM : duplication of Qt tool
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
28 /****************************************************************************
31 ** This is a utility program for converting findtr msgfiles to
32 ** qtranslator message files
35 ** Copyright (C) 1998 by Trolltech AS. All rights reserved.
37 *****************************************************************************/
39 #include <qtextstream.h>
40 #include <qtextcodec.h>
41 #include <qtranslator.h>
46 static QString* defaultScope = 0;
48 bool hasHandle( const QString& line, const QString& handle)
50 return line.left(handle.length()) == handle;
54 QString extractContents( const QString& line )
57 if ( line.contains('\"') < 2)
60 while ( pos < int(line.length()) && line[pos] != '\"' )
63 while ( pos < int(line.length()) && line[pos] != '\"' ) {
64 if ( line[pos] == '\\') {
66 switch (char(line[pos]) ) {
89 contents += char(line[pos]);
94 contents += line[pos];
101 void addTranslation( QTranslator* translator, const QString& msgid, const QString& msgstr)
103 if (!msgid.isNull() && !msgstr.isNull() ) {
106 int coloncolon = msgid.find("::");
107 if (coloncolon != -1) {
108 scope = msgid.left( coloncolon );
109 id = msgid.right( msgid.length() - scope.length() - 2 );
111 else if (defaultScope)
112 scope = *defaultScope;
114 if (translator->contains( scope.ascii(), id.ascii() ) ) {
115 qWarning("Error: \"%s\" already in use", msgid.ascii() );
118 translator->insert( scope.latin1(), id.latin1(), msgstr );
125 void translate( const QString& filename, const QString& qmfile )
128 if ( !f.open( IO_ReadOnly) )
130 QTranslator* translator = new QTranslator(0);
131 QTextCodec *codec = 0;
132 for (int pass = 0; pass < 2; pass++) {
141 while ( !t.atEnd() || !line.isEmpty() ) {
142 if (line.isEmpty()) {
146 if ( hasHandle( line, "msgid") ) {
147 msgstr = QString::null;
148 msgid = extractContents( line );
154 line = QString::null;
155 while ( hasHandle( line, "\"") ) {
156 msgid += extractContents( line );
162 line = QString::null;
165 else if ( hasHandle( line, "msgstr") ) {
166 msgstr = extractContents( line );
172 line = QString::null;
173 while ( hasHandle( line, "\"") ) {
174 msgstr += extractContents( line );
180 line = QString::null;
183 addTranslation( translator, msgid, msgstr);
185 if ( pass == 0 && msgid.isEmpty() ) {
186 // Check for the encoding.
187 int cpos = msgstr.find( "charset=" );
189 cpos = cpos + 8; //skip "charset="
191 int len = msgstr.length();
192 while ( i < len && !msgstr[i].isSpace() )
194 QString charset = msgstr.mid( cpos, i-cpos );
195 codec = QTextCodec::codecForName( charset.ascii() );
197 debug( "PO file character set: %s. Codec: %s",
198 charset.ascii(), codec->name() );
200 debug( "No codec for %s", charset.ascii() );
207 line = QString::null;
211 translator->save( qmfile );
215 // workaround for BCC problem, qtranslator.h includes qwindowdefs.h via qobject.h, see NEEDS_QMAIN
220 int main( int argc, char* argv[] )
225 if ( QString("-scope") == argv[1] ) {
226 defaultScope = new QString(argv[2]);
231 if ( argc <= infile ) {
232 printf("usage: %s [-scope default] infile [outfile]\n", argv[0]);
236 translate(argv[infile], argc > infile+1 ? argv[infile+1] : "tr.qm");