Changeset 561 for trunk/tools/linguist/lrelease/main.cpp
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/tools/linguist/lrelease/main.cpp
r2 r561 2 2 ** 3 3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). 4 ** Contact: Qt Software Information (qt-info@nokia.com) 4 ** All rights reserved. 5 ** Contact: Nokia Corporation (qt-info@nokia.com) 5 6 ** 6 7 ** This file is part of the Qt Linguist of the Qt Toolkit. … … 21 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 22 23 ** 23 ** In addition, as a special exception, Nokia gives you certain 24 ** additional rights. These rights are described in the Nokia Qt LGPL 25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this 26 ** package. 24 ** In addition, as a special exception, Nokia gives you certain additional 25 ** rights. These rights are described in the Nokia Qt LGPL Exception 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. 27 27 ** 28 28 ** GNU General Public License Usage … … 34 34 ** met: http://www.gnu.org/copyleft/gpl.html. 35 35 ** 36 ** If you are unsure which license is appropriate for your use, please37 ** contact the sales department at qt-sales@nokia.com.36 ** If you have questions regarding the use of this file, please contact 37 ** Nokia at qt-info@nokia.com. 38 38 ** $QT_END_LICENSE$ 39 39 ** … … 41 41 42 42 #include "translator.h" 43 #include " translatortools.h"44 #include "profileevaluator.h" 45 43 #include "proreader.h" 44 45 #ifndef QT_BOOTSTRAPPED 46 46 #include <QtCore/QCoreApplication> 47 #include <QtCore/QTranslator> 48 #endif 47 49 #include <QtCore/QDebug> 48 50 #include <QtCore/QDir> … … 53 55 #include <QtCore/QStringList> 54 56 #include <QtCore/QTextStream> 55 #include <QtCore/QTranslator> 57 58 QT_USE_NAMESPACE 59 60 #ifdef QT_BOOTSTRAPPED 61 static void initBinaryDir( 62 #ifndef Q_OS_WIN 63 const char *argv0 64 #endif 65 ); 66 #endif 56 67 57 68 static void printOut(const QString & out) … … 68 79 " lrelease [options] ts-files [-qm qm-file]\n\n" 69 80 "lrelease is part of Qt's Linguist tool chain. It can be used as a\n" 70 "stand-alone tool to convert XML based translations files in the .ts\n"71 "format into the 'compiled' .qmformat used by QTranslator objects.\n\n"81 "stand-alone tool to convert XML-based translations files in the TS\n" 82 "format into the 'compiled' QM format used by QTranslator objects.\n\n" 72 83 "Options:\n" 73 84 " -help Display this information and exit\n" 85 " -idbased\n" 86 " Use IDs instead of source strings for message keying\n" 74 87 " -compress\n" 75 " Compress the .qmfiles\n"88 " Compress the QM files\n" 76 89 " -nounfinished\n" 77 90 " Do not include unfinished translations\n" … … 79 92 " If the translated text is the same as\n" 80 93 " the source text, do not include the message\n" 94 " -markuntranslated <prefix>\n" 95 " If a message has no real translation, use the source text\n" 96 " prefixed with the given string instead\n" 81 97 " -silent\n" 82 " Do n't explain what is being done\n"98 " Do not explain what is being done\n" 83 99 " -version\n" 84 100 " Display the version of lrelease and exit\n" … … 100 116 101 117 static bool releaseTranslator(Translator &tor, const QString &qmFileName, 102 bool verbose, bool ignoreUnfinished, 103 bool removeIdentical, TranslatorSaveMode mode) 104 { 105 Translator::reportDuplicates(tor.resolveDuplicates(), qmFileName, verbose); 106 107 if (verbose) 118 ConversionData &cd, bool removeIdentical) 119 { 120 tor.reportDuplicates(tor.resolveDuplicates(), qmFileName, cd.isVerbose()); 121 122 if (cd.isVerbose()) 108 123 printOut(QCoreApplication::tr( "Updating '%1'...\n").arg(qmFileName)); 109 124 if (removeIdentical) { 110 if ( verbose)125 if (cd.isVerbose()) 111 126 printOut(QCoreApplication::tr( "Removing translations equal to source text in '%1'...\n").arg(qmFileName)); 112 127 tor.stripIdenticalSourceTranslations(); … … 120 135 } 121 136 122 ConversionData cd; 123 cd.m_verbose = verbose; 124 cd.m_ignoreUnfinished = ignoreUnfinished; 125 cd.m_saveMode = mode; 137 tor.normalizeTranslations(cd); 126 138 bool ok = tor.release(&file, cd); 127 139 file.close(); … … 137 149 } 138 150 139 static bool releaseTsFile(const QString& tsFileName, bool verbose,140 bool ignoreUnfinished, bool removeIdentical, TranslatorSaveMode mode)151 static bool releaseTsFile(const QString& tsFileName, 152 ConversionData &cd, bool removeIdentical) 141 153 { 142 154 Translator tor; 143 if (!loadTsFile(tor, tsFileName, verbose))155 if (!loadTsFile(tor, tsFileName, cd.isVerbose())) 144 156 return false; 145 157 … … 153 165 qmFileName += QLatin1String(".qm"); 154 166 155 return releaseTranslator(tor, qmFileName, verbose, ignoreUnfinished, removeIdentical, mode);167 return releaseTranslator(tor, qmFileName, cd, removeIdentical); 156 168 } 157 169 158 170 int main(int argc, char **argv) 159 171 { 172 #ifdef QT_BOOTSTRAPPED 173 initBinaryDir( 174 #ifndef Q_OS_WIN 175 argv[0] 176 #endif 177 ); 178 #else 160 179 QCoreApplication app(argc, argv); 161 QStringList args = app.arguments();162 180 QTranslator translator; 163 181 if (translator.load(QLatin1String("lrelease_") + QLocale::system().name())) 164 182 app.installTranslator(&translator); 165 166 bool verbose = true; // the default is true starting with Qt 4.2 167 bool ignoreUnfinished = false; 168 // the default mode is SaveEverything starting with Qt 4.2 169 TranslatorSaveMode mode = SaveEverything; 183 #endif 184 185 ConversionData cd; 186 cd.m_verbose = true; // the default is true starting with Qt 4.2 170 187 bool removeIdentical = false; 171 188 Translator tor; 189 QStringList inputFiles; 172 190 QString outputFile; 173 int numFiles = 0;174 191 175 192 for (int i = 1; i < argc; ++i) { 176 if (args[i] == QLatin1String("-compress")) { 177 mode = SaveStripped; 178 continue; 179 } else if (args[i] == QLatin1String("-nocompress")) { 180 mode = SaveEverything; 181 continue; 182 } else if (args[i] == QLatin1String("-removeidentical")) { 193 if (!strcmp(argv[i], "-compress")) { 194 cd.m_saveMode = SaveStripped; 195 continue; 196 } else if (!strcmp(argv[i], "-idbased")) { 197 cd.m_idBased = true; 198 continue; 199 } else if (!strcmp(argv[i], "-nocompress")) { 200 cd.m_saveMode = SaveEverything; 201 continue; 202 } else if (!strcmp(argv[i], "-removeidentical")) { 183 203 removeIdentical = true; 184 204 continue; 185 } else if (args[i] == QLatin1String("-nounfinished")) { 186 ignoreUnfinished = true; 187 continue; 188 } else if (args[i] == QLatin1String("-silent")) { 189 verbose = false; 190 continue; 191 } else if (args[i] == QLatin1String("-verbose")) { 192 verbose = true; 193 continue; 194 } else if (args[i] == QLatin1String("-version")) { 195 printOut(QCoreApplication::tr( "lrelease version %1\n").arg(QLatin1String(QT_VERSION_STR)) ); 196 return 0; 197 } else if (args[i] == QLatin1String("-qm")) { 205 } else if (!strcmp(argv[i], "-nounfinished")) { 206 cd.m_ignoreUnfinished = true; 207 continue; 208 } else if (!strcmp(argv[i], "-markuntranslated")) { 198 209 if (i == argc - 1) { 199 210 printUsage(); 200 211 return 1; 201 212 } 202 i++; 203 outputFile = args[i]; 204 } else if (args[i] == QLatin1String("-help")) { 213 cd.m_unTrPrefix = QString::fromLocal8Bit(argv[++i]); 214 } else if (!strcmp(argv[i], "-silent")) { 215 cd.m_verbose = false; 216 continue; 217 } else if (!strcmp(argv[i], "-verbose")) { 218 cd.m_verbose = true; 219 continue; 220 } else if (!strcmp(argv[i], "-version")) { 221 printOut(QCoreApplication::tr( "lrelease version %1\n").arg(QLatin1String(QT_VERSION_STR)) ); 222 return 0; 223 } else if (!strcmp(argv[i], "-qm")) { 224 if (i == argc - 1) { 225 printUsage(); 226 return 1; 227 } 228 outputFile = QString::fromLocal8Bit(argv[++i]); 229 } else if (!strcmp(argv[i], "-help")) { 205 230 printUsage(); 206 231 return 0; 207 } else if (arg s[i][0] == QLatin1Char('-')) {232 } else if (argv[i][0] == '-') { 208 233 printUsage(); 209 234 return 1; 210 235 } else { 211 numFiles++;236 inputFiles << QString::fromLocal8Bit(argv[i]); 212 237 } 213 238 } 214 239 215 if ( numFiles == 0) {240 if (inputFiles.isEmpty()) { 216 241 printUsage(); 217 242 return 1; 218 243 } 219 244 220 for (int i = 1; i < argc; ++i) { 221 if (args[i][0] == QLatin1Char('-') || args[i] == outputFile) 222 continue; 223 224 if (args[i].endsWith(QLatin1String(".pro"), Qt::CaseInsensitive) 225 || args[i].endsWith(QLatin1String(".pri"), Qt::CaseInsensitive)) { 245 foreach (const QString &inputFile, inputFiles) { 246 if (inputFile.endsWith(QLatin1String(".pro"), Qt::CaseInsensitive) 247 || inputFile.endsWith(QLatin1String(".pri"), Qt::CaseInsensitive)) { 226 248 QHash<QByteArray, QStringList> varMap; 227 bool ok = evaluateProFile( args[i], verbose, &varMap);249 bool ok = evaluateProFile(inputFile, cd.isVerbose(), &varMap); 228 250 if (ok) { 229 251 QStringList translations = varMap.value("TRANSLATIONS"); … … 231 253 qWarning("lrelease warning: Met no 'TRANSLATIONS' entry in" 232 254 " project file '%s'\n", 233 qPrintable( args[i]));255 qPrintable(inputFile)); 234 256 } else { 235 257 foreach (const QString &trans, translations) 236 if (!releaseTsFile(trans, verbose, ignoreUnfinished, removeIdentical, mode))258 if (!releaseTsFile(trans, cd, removeIdentical)) 237 259 return 1; 238 260 } 239 261 } else { 240 262 qWarning("error: lrelease encountered project file functionality that is currently not supported.\n" 241 "You might want to consider using .tsfiles as input instead of a project file.\n"263 "You might want to consider using TS files as input instead of a project file.\n" 242 264 "Try the following syntax:\n" 243 265 " lrelease [options] ts-files [-qm qm-file]\n"); … … 245 267 } else { 246 268 if (outputFile.isEmpty()) { 247 if (!releaseTsFile( args[i], verbose, ignoreUnfinished, removeIdentical, mode))269 if (!releaseTsFile(inputFile, cd, removeIdentical)) 248 270 return 1; 249 271 } else { 250 if (!loadTsFile(tor, args[i], verbose))272 if (!loadTsFile(tor, inputFile, cd.isVerbose())) 251 273 return 1; 252 274 } … … 255 277 256 278 if (!outputFile.isEmpty()) 257 return releaseTranslator(tor, outputFile, verbose, ignoreUnfinished, 258 removeIdentical, mode) ? 0 : 1; 279 return releaseTranslator(tor, outputFile, cd, removeIdentical) ? 0 : 1; 259 280 260 281 return 0; 261 282 } 283 284 #ifdef QT_BOOTSTRAPPED 285 286 #ifdef Q_OS_WIN 287 # include <windows.h> 288 #endif 289 290 static QString binDir; 291 292 static void initBinaryDir( 293 #ifndef Q_OS_WIN 294 const char *_argv0 295 #endif 296 ) 297 { 298 #ifdef Q_OS_WIN 299 wchar_t module_name[MAX_PATH]; 300 GetModuleFileName(0, module_name, MAX_PATH); 301 QFileInfo filePath = QString::fromWCharArray(module_name); 302 binDir = filePath.filePath(); 303 #else 304 QString argv0 = QFile::decodeName(QByteArray(_argv0)); 305 QString absPath; 306 307 if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) { 308 /* 309 If argv0 starts with a slash, it is already an absolute 310 file path. 311 */ 312 absPath = argv0; 313 } else if (argv0.contains(QLatin1Char('/'))) { 314 /* 315 If argv0 contains one or more slashes, it is a file path 316 relative to the current directory. 317 */ 318 absPath = QDir::current().absoluteFilePath(argv0); 319 } else { 320 /* 321 Otherwise, the file path has to be determined using the 322 PATH environment variable. 323 */ 324 QByteArray pEnv = qgetenv("PATH"); 325 QDir currentDir = QDir::current(); 326 QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1String(":")); 327 for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) { 328 if ((*p).isEmpty()) 329 continue; 330 QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0); 331 QFileInfo candidate_fi(candidate); 332 if (candidate_fi.exists() && !candidate_fi.isDir()) { 333 binDir = candidate_fi.canonicalPath(); 334 return; 335 } 336 } 337 return; 338 } 339 340 QFileInfo fi(absPath); 341 if (fi.exists()) 342 binDir = fi.canonicalPath(); 343 #endif 344 } 345 346 QT_BEGIN_NAMESPACE 347 348 // The name is hard-coded in QLibraryInfo 349 QString qmake_libraryInfoFile() 350 { 351 if (binDir.isEmpty()) 352 return QString(); 353 return QDir(binDir).filePath(QString::fromLatin1("qt.conf")); 354 } 355 356 QT_END_NAMESPACE 357 358 #endif // QT_BOOTSTRAPPED
Note:
See TracChangeset
for help on using the changeset viewer.