| 1 | /**********************************************************************
|
|---|
| 2 | ** Copyright (C) 2000 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 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 12 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 13 | ** Agreement provided with the Software.
|
|---|
| 14 | **
|
|---|
| 15 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 16 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 17 | **
|
|---|
| 18 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 19 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 20 | ** information about Qt Commercial License Agreements.
|
|---|
| 21 | **
|
|---|
| 22 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 23 | ** not clear to you.
|
|---|
| 24 | **
|
|---|
| 25 | **********************************************************************/
|
|---|
| 26 |
|
|---|
| 27 | #include <metatranslator.h>
|
|---|
| 28 | #include <proparser.h>
|
|---|
| 29 |
|
|---|
| 30 | #include <qdir.h>
|
|---|
| 31 | #include <qfile.h>
|
|---|
| 32 | #include <qfileinfo.h>
|
|---|
| 33 | #include <qregexp.h>
|
|---|
| 34 | #include <qstring.h>
|
|---|
| 35 | #include <qstringlist.h>
|
|---|
| 36 | #include <qtextstream.h>
|
|---|
| 37 |
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 |
|
|---|
| 40 | typedef QValueList<MetaTranslatorMessage> TML;
|
|---|
| 41 |
|
|---|
| 42 | static void printUsage()
|
|---|
| 43 | {
|
|---|
| 44 | fprintf( stderr, "Usage:\n"
|
|---|
| 45 | " lrelease [options] project-file\n"
|
|---|
| 46 | " lrelease [options] ts-files [-qm qm-file]\n"
|
|---|
| 47 | "Options:\n"
|
|---|
| 48 | " -help Display this information and exit\n"
|
|---|
| 49 | " -nocompress\n"
|
|---|
| 50 | " Do not compress the .qm files\n"
|
|---|
| 51 | " -verbose\n"
|
|---|
| 52 | " Explain what is being done\n"
|
|---|
| 53 | " -version\n"
|
|---|
| 54 | " Display the version of lrelease and exit\n" );
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | static bool loadTsFile( MetaTranslator& tor, const QString& tsFileName,
|
|---|
| 58 | bool /* verbose */ )
|
|---|
| 59 | {
|
|---|
| 60 | QString qmFileName = tsFileName;
|
|---|
| 61 | qmFileName.replace( QRegExp("\\.ts$"), "" );
|
|---|
| 62 | qmFileName += ".qm";
|
|---|
| 63 |
|
|---|
| 64 | bool ok = tor.load( tsFileName );
|
|---|
| 65 | if ( !ok )
|
|---|
| 66 | fprintf( stderr,
|
|---|
| 67 | "lrelease warning: For some reason, I cannot load '%s'\n",
|
|---|
| 68 | tsFileName.latin1() );
|
|---|
| 69 | return ok;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | static void releaseMetaTranslator( const MetaTranslator& tor,
|
|---|
| 73 | const QString& qmFileName, bool verbose,
|
|---|
| 74 | bool stripped )
|
|---|
| 75 | {
|
|---|
| 76 | if ( verbose )
|
|---|
| 77 | fprintf( stderr, "Updating '%s'...\n", qmFileName.latin1() );
|
|---|
| 78 | if ( !tor.release(qmFileName, verbose,
|
|---|
| 79 | stripped ? QTranslator::Stripped
|
|---|
| 80 | : QTranslator::Everything) )
|
|---|
| 81 | fprintf( stderr,
|
|---|
| 82 | "lrelease warning: For some reason, I cannot save '%s'\n",
|
|---|
| 83 | qmFileName.latin1() );
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | static void releaseTsFile( const QString& tsFileName, bool verbose,
|
|---|
| 87 | bool stripped )
|
|---|
| 88 | {
|
|---|
| 89 | MetaTranslator tor;
|
|---|
| 90 | if ( loadTsFile(tor, tsFileName, verbose) ) {
|
|---|
| 91 | QString qmFileName = tsFileName;
|
|---|
| 92 | qmFileName.replace( QRegExp("\\.ts$"), "" );
|
|---|
| 93 | qmFileName += ".qm";
|
|---|
| 94 | releaseMetaTranslator( tor, qmFileName, verbose, stripped );
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | int main( int argc, char **argv )
|
|---|
| 99 | {
|
|---|
| 100 | bool verbose = FALSE;
|
|---|
| 101 | bool stripped = TRUE;
|
|---|
| 102 | bool metTranslations = FALSE;
|
|---|
| 103 | MetaTranslator tor;
|
|---|
| 104 | QString outputFile;
|
|---|
| 105 | int numFiles = 0;
|
|---|
| 106 | int i;
|
|---|
| 107 |
|
|---|
| 108 | for ( i = 1; i < argc; i++ ) {
|
|---|
| 109 | if ( qstrcmp(argv[i], "-nocompress") == 0 ) {
|
|---|
| 110 | stripped = FALSE;
|
|---|
| 111 | continue;
|
|---|
| 112 | } else if ( qstrcmp(argv[i], "-verbose") == 0 ) {
|
|---|
| 113 | verbose = TRUE;
|
|---|
| 114 | continue;
|
|---|
| 115 | } else if ( qstrcmp(argv[i], "-version") == 0 ) {
|
|---|
| 116 | fprintf( stderr, "lrelease version %s\n", QT_VERSION_STR );
|
|---|
| 117 | return 0;
|
|---|
| 118 | } else if ( qstrcmp(argv[i], "-qm") == 0 ) {
|
|---|
| 119 | if ( i == argc - 1 ) {
|
|---|
| 120 | printUsage();
|
|---|
| 121 | return 1;
|
|---|
| 122 | } else {
|
|---|
| 123 | i++;
|
|---|
| 124 | outputFile = argv[i];
|
|---|
| 125 | argv[i][0] = '-';
|
|---|
| 126 | }
|
|---|
| 127 | } else if ( qstrcmp(argv[i], "-help") == 0 ) {
|
|---|
| 128 | printUsage();
|
|---|
| 129 | return 0;
|
|---|
| 130 | } else if ( argv[i][0] == '-' ) {
|
|---|
| 131 | printUsage();
|
|---|
| 132 | return 1;
|
|---|
| 133 | } else {
|
|---|
| 134 | numFiles++;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if ( numFiles == 0 ) {
|
|---|
| 139 | printUsage();
|
|---|
| 140 | return 1;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | for ( i = 1; i < argc; i++ ) {
|
|---|
| 144 | if ( argv[i][0] == '-' )
|
|---|
| 145 | continue;
|
|---|
| 146 |
|
|---|
| 147 | QFile f( argv[i] );
|
|---|
| 148 | if ( !f.open(IO_ReadOnly) ) {
|
|---|
| 149 | fprintf( stderr,
|
|---|
| 150 | "lrelease error: Cannot open file '%s': %s\n", argv[i],
|
|---|
| 151 | strerror(errno) );
|
|---|
| 152 | return 1;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | QTextStream t( &f );
|
|---|
| 156 | QString fullText = t.read();
|
|---|
| 157 | f.close();
|
|---|
| 158 |
|
|---|
| 159 | if ( fullText.find(QString("<!DOCTYPE TS>")) >= 0 ) {
|
|---|
| 160 | if ( outputFile.isEmpty() ) {
|
|---|
| 161 | releaseTsFile( argv[i], verbose, stripped );
|
|---|
| 162 | } else {
|
|---|
| 163 | loadTsFile( tor, argv[i], verbose );
|
|---|
| 164 | }
|
|---|
| 165 | } else {
|
|---|
| 166 | QString oldDir = QDir::currentDirPath();
|
|---|
| 167 | QDir::setCurrent( QFileInfo(argv[i]).dirPath() );
|
|---|
| 168 |
|
|---|
| 169 | QMap<QString, QString> tagMap = proFileTagMap( fullText );
|
|---|
| 170 | QMap<QString, QString>::Iterator it;
|
|---|
| 171 |
|
|---|
| 172 | for ( it = tagMap.begin(); it != tagMap.end(); ++it ) {
|
|---|
| 173 | QStringList toks = QStringList::split( ' ', it.data() );
|
|---|
| 174 | QStringList::Iterator t;
|
|---|
| 175 |
|
|---|
| 176 | for ( t = toks.begin(); t != toks.end(); ++t ) {
|
|---|
| 177 | if ( it.key() == QString("TRANSLATIONS") ) {
|
|---|
| 178 | metTranslations = TRUE;
|
|---|
| 179 | releaseTsFile( *t, verbose, stripped );
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | if ( !metTranslations )
|
|---|
| 184 | fprintf( stderr,
|
|---|
| 185 | "lrelease warning: Met no 'TRANSLATIONS' entry in"
|
|---|
| 186 | " project file '%s'\n",
|
|---|
| 187 | argv[i] );
|
|---|
| 188 | QDir::setCurrent( oldDir );
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | if ( !outputFile.isEmpty() )
|
|---|
| 193 | releaseMetaTranslator( tor, outputFile, verbose, stripped );
|
|---|
| 194 |
|
|---|
| 195 | return 0;
|
|---|
| 196 | }
|
|---|