Changeset 561 for trunk/tools/linguist/lupdate/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/lupdate/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 ** 40 40 ****************************************************************************/ 41 41 42 #include "translator.h" 43 #include "translatortools.h" 44 #include "profileevaluator.h" 42 #include "lupdate.h" 43 44 #include <translator.h> 45 #include <profileevaluator.h> 46 #include <proreader.h> 45 47 46 48 #include <QtCore/QCoreApplication> … … 53 55 #include <QtCore/QTextCodec> 54 56 57 #include <iostream> 58 55 59 static QString m_defaultExtensions; 56 60 57 static void print Out(const QString & out)61 static void printErr(const QString & out) 58 62 { 59 63 qWarning("%s", qPrintable(out)); 60 64 } 61 65 66 static void printOut(const QString & out) 67 { 68 std::cerr << qPrintable(out); 69 } 70 62 71 static void recursiveFileInfoList(const QDir &dir, 63 const QS tringList &nameFilters, QDir::Filters filter, bool recursive,72 const QSet<QString> &nameFilters, QDir::Filters filter, 64 73 QFileInfoList *fileinfolist) 65 74 { 66 if (recursive) 67 filter |= QDir::AllDirs; 68 QFileInfoList entries = dir.entryInfoList(nameFilters, filter); 69 70 QFileInfoList::iterator it; 71 for (it = entries.begin(); it != entries.end(); ++it) { 72 QString fname = it->fileName(); 73 if (fname != QLatin1String(".") && fname != QLatin1String("..")) { 74 if (it->isDir()) 75 recursiveFileInfoList(QDir(it->absoluteFilePath()), nameFilters, filter, recursive, fileinfolist); 76 else 77 fileinfolist->append(*it); 78 } 79 } 75 foreach (const QFileInfo &fi, dir.entryInfoList(filter)) 76 if (fi.isDir()) 77 recursiveFileInfoList(QDir(fi.absoluteFilePath()), nameFilters, filter, fileinfolist); 78 else if (nameFilters.contains(fi.suffix())) 79 fileinfolist->append(fi); 80 80 } 81 81 … … 84 84 printOut(QObject::tr( 85 85 "Usage:\n" 86 " lupdate [options] [project-file] \n"86 " lupdate [options] [project-file]...\n" 87 87 " lupdate [options] [source-file|path]... -ts ts-files\n\n" 88 "lupdate is part of Qt's Linguist tool chain. It can be used as a\n"89 " stand-alone tool to create XML based translations files in the .ts\n"90 " format from translatable messages in C++ and Java source code.\n\n"91 " lupdate can also merge such messages into existing .tsfiles.\n\n"88 "lupdate is part of Qt's Linguist tool chain. It extracts translatable\n" 89 "messages from Qt UI files, C++, Java and JavaScript/QtScript source code.\n" 90 "Extracted messages are stored in textual translation source files (typically\n" 91 "Qt TS XML). New and modified messages can be merged into existing TS files.\n\n" 92 92 "Options:\n" 93 93 " -help Display this information and exit.\n" … … 103 103 " Do not explain what is being done.\n" 104 104 " -no-sort\n" 105 " Do not sort contexts in .tsfiles.\n"105 " Do not sort contexts in TS files.\n" 106 106 " -no-recursive\n" 107 107 " Do not recursively scan the following directories.\n" 108 108 " -recursive\n" 109 " Recursively scan the following directories.\n" 109 " Recursively scan the following directories (default).\n" 110 " -I <includepath> or -I<includepath>\n" 111 " Additional location to look for include files.\n" 112 " May be specified multiple times.\n" 110 113 " -locations {absolute|relative|none}\n" 111 " Specify/override how source code references are saved in tsfiles.\n"114 " Specify/override how source code references are saved in TS files.\n" 112 115 " Default is absolute.\n" 113 116 " -no-ui-lines\n" 114 " Do not record line numbers in references to .uifiles.\n"117 " Do not record line numbers in references to UI files.\n" 115 118 " -disable-heuristic {sametext|similartext|number}\n" 116 119 " Disable the named merge heuristic. Can be specified multiple times.\n" … … 119 122 " file syntax but different file suffix\n" 120 123 " -source-language <language>[_<region>]\n" 121 " Specify /override the language of the source strings. Defaults to\n"122 " POSIX if not specified and the file does not name it yet.\n"124 " Specify the language of the source strings for new files.\n" 125 " Defaults to POSIX if not specified.\n" 123 126 " -target-language <language>[_<region>]\n" 124 " Specify/override the language of the translation.\n" 125 " The target language is guessed from the file name if this option\n" 126 " is not specified and the file contents name no language yet.\n" 127 " Specify the language of the translations for new files.\n" 128 " Guessed from the file name if not specified.\n" 127 129 " -version\n" 128 130 " Display the version of lupdate and exit.\n" … … 143 145 if (QFile(fileName).exists()) { 144 146 if (!tor.load(fileName, cd, QLatin1String("auto"))) { 145 print Out(cd.error());147 printErr(cd.error()); 146 148 *fail = true; 147 149 continue; … … 166 168 if (!targetLanguage.isEmpty()) 167 169 tor.setLanguageCode(targetLanguage); 170 else 171 tor.setLanguageCode(Translator::guessLanguageCodeFromFileName(fileName)); 168 172 if (!sourceLanguage.isEmpty()) 169 173 tor.setSourceLanguageCode(sourceLanguage); … … 179 183 printOut(QObject::tr("Updating '%1'...\n").arg(fn)); 180 184 185 UpdateOptions theseOptions = options; 181 186 if (tor.locationsType() == Translator::NoLocations) // Could be set from file 182 options |= NoLocations;183 Translator out = merge(tor, fetchedTor, options, err);187 theseOptions |= NoLocations; 188 Translator out = merge(tor, fetchedTor, theseOptions, err); 184 189 if (!codecForTr.isEmpty()) 185 190 out.setCodecName(codecForTr); … … 198 203 out.stripEmptyContexts(); 199 204 205 out.normalizeTranslations(cd); 206 if (!cd.errors().isEmpty()) { 207 printErr(cd.error()); 208 cd.clearErrors(); 209 } 200 210 if (!out.save(fileName, cd, QLatin1String("auto"))) { 201 print Out(cd.error());211 printErr(cd.error()); 202 212 *fail = true; 203 213 } … … 217 227 QStringList tsFileNames; 218 228 QStringList proFiles; 229 QMultiHash<QString, QString> allCSources; 230 QSet<QString> projectRoots; 219 231 QStringList sourceFiles; 232 QStringList includePath; 220 233 QString targetLanguage; 221 234 QString sourceLanguage; … … 230 243 231 244 QString extensions = m_defaultExtensions; 232 QS tringListextensionsNameFilters;245 QSet<QString> extensionsNameFilters; 233 246 234 247 for (int i = 1; i < argc; ++i) { … … 343 356 proFiles += args[i]; 344 357 numFiles++; 358 continue; 359 } else if (arg.startsWith(QLatin1String("-I"))) { 360 if (arg.length() == 2) { 361 ++i; 362 if (i == argc) { 363 qWarning("The -I option should be followed by a path."); 364 return 1; 365 } 366 includePath += args[i]; 367 } else { 368 includePath += args[i].mid(2); 369 } 345 370 continue; 346 371 } else if (arg.startsWith(QLatin1String("-")) && arg != QLatin1String("-")) { … … 388 413 printOut(QObject::tr("Scanning directory '%1'...").arg(arg)); 389 414 QDir dir = QDir(fi.filePath()); 415 projectRoots.insert(dir.absolutePath() + QLatin1Char('/')); 390 416 if (extensionsNameFilters.isEmpty()) { 391 extensions = extensions.trimmed(); 392 // Remove the potential dot in front of each extension 393 if (extensions.startsWith(QLatin1Char('.'))) 394 extensions.remove(0,1); 395 extensions.replace(QLatin1String(",."), QLatin1String(",")); 396 397 extensions.insert(0, QLatin1String("*.")); 398 extensions.replace(QLatin1Char(','), QLatin1String(",*.")); 399 extensionsNameFilters = extensions.split(QLatin1Char(',')); 417 foreach (QString ext, extensions.split(QLatin1Char(','))) { 418 ext = ext.trimmed(); 419 if (ext.startsWith(QLatin1Char('.'))) 420 ext.remove(0, 1); 421 extensionsNameFilters.insert(ext); 422 } 400 423 } 401 424 QDir::Filters filters = QDir::Files | QDir::NoSymLinks; 425 if (recursiveScan) 426 filters |= QDir::AllDirs | QDir::NoDotAndDotDot; 402 427 QFileInfoList fileinfolist; 403 recursiveFileInfoList(dir, extensionsNameFilters, filters, 404 recursiveScan, &fileinfolist); 405 QFileInfoList::iterator ii; 406 QString fn; 407 for (ii = fileinfolist.begin(); ii != fileinfolist.end(); ++ii) { 408 // Make sure the path separator is stored with '/' in the ts file 409 sourceFiles << ii->canonicalFilePath().replace(QLatin1Char('\\'), QLatin1Char('/')); 428 recursiveFileInfoList(dir, extensionsNameFilters, filters, &fileinfolist); 429 int scanRootLen = dir.absolutePath().length(); 430 foreach (const QFileInfo &fi, fileinfolist) { 431 QString fn = QDir::cleanPath(fi.absoluteFilePath()); 432 sourceFiles << fn; 433 434 if (!fn.endsWith(QLatin1String(".java")) 435 && !fn.endsWith(QLatin1String(".ui")) 436 && !fn.endsWith(QLatin1String(".js")) 437 && !fn.endsWith(QLatin1String(".qs"))) { 438 int offset = 0; 439 int depth = 0; 440 do { 441 offset = fn.lastIndexOf(QLatin1Char('/'), offset - 1); 442 QString ffn = fn.mid(offset + 1); 443 allCSources.insert(ffn, fn); 444 } while (++depth < 3 && offset > scanRootLen); 445 } 410 446 } 411 447 } else { 412 sourceFiles << fi.canonicalFilePath().replace(QLatin1Char('\\'), QLatin1Char('/'));448 sourceFiles << QDir::cleanPath(fi.absoluteFilePath());; 413 449 } 414 450 } 415 451 } // for args 416 452 453 foreach (const QString &proFile, proFiles) 454 projectRoots.insert(QDir::cleanPath(QFileInfo(proFile).absolutePath()) + QLatin1Char('/')); 417 455 418 456 bool firstPass = true; … … 422 460 cd.m_defaultContext = defaultContext; 423 461 cd.m_noUiLines = options & NoUiLines; 462 cd.m_projectRoots = projectRoots; 463 cd.m_includePath = includePath; 464 cd.m_allCSources = allCSources; 424 465 425 466 QStringList tsFiles = tsFileNames; … … 451 492 } 452 493 453 evaluateProFile(visitor, &variables); 494 cd.m_includePath += visitor.values(QLatin1String("INCLUDEPATH")); 495 496 evaluateProFile(visitor, &variables, pfi.absolutePath()); 454 497 455 498 sourceFiles = variables.value("SOURCES"); … … 459 502 codecForTr = tmp.first().toLatin1(); 460 503 fetchedTor.setCodecName(codecForTr); 504 cd.m_outputCodec = codecForTr; 461 505 } 462 506 tmp = variables.value("CODECFORSRC"); … … 473 517 } 474 518 519 QStringList sourceFilesCpp; 475 520 for (QStringList::iterator it = sourceFiles.begin(); it != sourceFiles.end(); ++it) { 476 if (it->endsWith(QLatin1String(".java"), Qt::CaseInsensitive)) { 477 cd.m_sourceFileName = *it; 478 fetchedTor.load(*it, cd, QLatin1String("java")); 479 //fetchtr_java(*it, &fetchedTor, defaultContext, true, codecForSource); 480 } 481 else if (it->endsWith(QLatin1String(".ui"), Qt::CaseInsensitive)) { 482 fetchedTor.load(*it, cd, QLatin1String("ui")); 483 //fetchedTor.load(*it + QLatin1String(".h"), cd, QLatin1String("cpp")); 484 //fetchtr_ui(*it, &fetchedTor, defaultContext, true); 485 //fetchtr_cpp(*it + QLatin1String(".h"), &fetchedTor, 486 // defaultContext, false, codecForSource); 487 } 521 if (it->endsWith(QLatin1String(".java"), Qt::CaseInsensitive)) 522 loadJava(fetchedTor, *it, cd); 523 else if (it->endsWith(QLatin1String(".ui"), Qt::CaseInsensitive) 524 || it->endsWith(QLatin1String(".jui"), Qt::CaseInsensitive)) 525 loadUI(fetchedTor, *it, cd); 488 526 else if (it->endsWith(QLatin1String(".js"), Qt::CaseInsensitive) 489 || it->endsWith(QLatin1String(".qs"), Qt::CaseInsensitive)) { 490 fetchedTor.load(*it, cd, QLatin1String("js")); 491 } else { 492 fetchedTor.load(*it, cd, QLatin1String("cpp")); 493 //fetchtr_cpp(*it, &fetchedTor, defaultContext, true, codecForSource); 494 } 495 } 527 || it->endsWith(QLatin1String(".qs"), Qt::CaseInsensitive)) 528 loadQScript(fetchedTor, *it, cd); 529 else 530 sourceFilesCpp << *it; 531 } 532 loadCPP(fetchedTor, sourceFilesCpp, cd); 496 533 if (!cd.error().isEmpty()) 497 534 printOut(cd.error());
Note:
See TracChangeset
for help on using the changeset viewer.