Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tools/linguist/lrelease/main.cpp

    r2 r561  
    22**
    33** 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)
    56**
    67** This file is part of the Qt Linguist of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    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.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** 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.
    3838** $QT_END_LICENSE$
    3939**
     
    4141
    4242#include "translator.h"
    43 #include "translatortools.h"
    44 #include "profileevaluator.h"
    45 
     43#include "proreader.h"
     44
     45#ifndef QT_BOOTSTRAPPED
    4646#include <QtCore/QCoreApplication>
     47#include <QtCore/QTranslator>
     48#endif
    4749#include <QtCore/QDebug>
    4850#include <QtCore/QDir>
     
    5355#include <QtCore/QStringList>
    5456#include <QtCore/QTextStream>
    55 #include <QtCore/QTranslator>
     57
     58QT_USE_NAMESPACE
     59
     60#ifdef QT_BOOTSTRAPPED
     61static void initBinaryDir(
     62#ifndef Q_OS_WIN
     63        const char *argv0
     64#endif
     65        );
     66#endif
    5667
    5768static void printOut(const QString & out)
     
    6879        "    lrelease [options] ts-files [-qm qm-file]\n\n"
    6980        "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' .qm format 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"
    7283        "Options:\n"
    7384        "    -help  Display this information and exit\n"
     85        "    -idbased\n"
     86        "           Use IDs instead of source strings for message keying\n"
    7487        "    -compress\n"
    75         "           Compress the .qm files\n"
     88        "           Compress the QM files\n"
    7689        "    -nounfinished\n"
    7790        "           Do not include unfinished translations\n"
     
    7992        "           If the translated text is the same as\n"
    8093        "           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"
    8197        "    -silent\n"
    82         "           Don't explain what is being done\n"
     98        "           Do not explain what is being done\n"
    8399        "    -version\n"
    84100        "           Display the version of lrelease and exit\n"
     
    100116
    101117static 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())
    108123        printOut(QCoreApplication::tr( "Updating '%1'...\n").arg(qmFileName));
    109124    if (removeIdentical) {
    110         if ( verbose )
     125        if (cd.isVerbose())
    111126            printOut(QCoreApplication::tr( "Removing translations equal to source text in '%1'...\n").arg(qmFileName));
    112127        tor.stripIdenticalSourceTranslations();
     
    120135    }
    121136
    122     ConversionData cd;
    123     cd.m_verbose = verbose;
    124     cd.m_ignoreUnfinished = ignoreUnfinished;
    125     cd.m_saveMode = mode;
     137    tor.normalizeTranslations(cd);
    126138    bool ok = tor.release(&file, cd);
    127139    file.close();
     
    137149}
    138150
    139 static bool releaseTsFile(const QString& tsFileName, bool verbose,
    140     bool ignoreUnfinished, bool removeIdentical, TranslatorSaveMode mode)
     151static bool releaseTsFile(const QString& tsFileName,
     152    ConversionData &cd, bool removeIdentical)
    141153{
    142154    Translator tor;
    143     if (!loadTsFile(tor, tsFileName, verbose))
     155    if (!loadTsFile(tor, tsFileName, cd.isVerbose()))
    144156        return false;
    145157
     
    153165    qmFileName += QLatin1String(".qm");
    154166
    155     return releaseTranslator(tor, qmFileName, verbose, ignoreUnfinished, removeIdentical, mode);
     167    return releaseTranslator(tor, qmFileName, cd, removeIdentical);
    156168}
    157169
    158170int main(int argc, char **argv)
    159171{
     172#ifdef QT_BOOTSTRAPPED
     173    initBinaryDir(
     174#ifndef Q_OS_WIN
     175            argv[0]
     176#endif
     177            );
     178#else
    160179    QCoreApplication app(argc, argv);
    161     QStringList args = app.arguments();
    162180    QTranslator translator;
    163181    if (translator.load(QLatin1String("lrelease_") + QLocale::system().name()))
    164182        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
    170187    bool removeIdentical = false;
    171188    Translator tor;
     189    QStringList inputFiles;
    172190    QString outputFile;
    173     int numFiles = 0;
    174191
    175192    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")) {
    183203            removeIdentical = true;
    184204            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")) {
    198209            if (i == argc - 1) {
    199210                printUsage();
    200211                return 1;
    201212            }
    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")) {
    205230            printUsage();
    206231            return 0;
    207         } else if (args[i][0] == QLatin1Char('-')) {
     232        } else if (argv[i][0] == '-') {
    208233            printUsage();
    209234            return 1;
    210235        } else {
    211             numFiles++;
     236            inputFiles << QString::fromLocal8Bit(argv[i]);
    212237        }
    213238    }
    214239
    215     if (numFiles == 0) {
     240    if (inputFiles.isEmpty()) {
    216241        printUsage();
    217242        return 1;
    218243    }
    219244
    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)) {
    226248            QHash<QByteArray, QStringList> varMap;
    227             bool ok = evaluateProFile(args[i], verbose, &varMap );
     249            bool ok = evaluateProFile(inputFile, cd.isVerbose(), &varMap);
    228250            if (ok) {
    229251                QStringList translations = varMap.value("TRANSLATIONS");
     
    231253                    qWarning("lrelease warning: Met no 'TRANSLATIONS' entry in"
    232254                             " project file '%s'\n",
    233                              qPrintable(args[i]));
     255                             qPrintable(inputFile));
    234256                } else {
    235257                    foreach (const QString &trans, translations)
    236                         if (!releaseTsFile(trans, verbose, ignoreUnfinished, removeIdentical, mode))
     258                        if (!releaseTsFile(trans, cd, removeIdentical))
    237259                            return 1;
    238260                }
    239261            } else {
    240262                qWarning("error: lrelease encountered project file functionality that is currently not supported.\n"
    241                     "You might want to consider using .ts files 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"
    242264                    "Try the following syntax:\n"
    243265                    "    lrelease [options] ts-files [-qm qm-file]\n");
     
    245267        } else {
    246268            if (outputFile.isEmpty()) {
    247                 if (!releaseTsFile(args[i], verbose, ignoreUnfinished, removeIdentical, mode))
     269                if (!releaseTsFile(inputFile, cd, removeIdentical))
    248270                    return 1;
    249271            } else {
    250                 if (!loadTsFile(tor, args[i], verbose))
     272                if (!loadTsFile(tor, inputFile, cd.isVerbose()))
    251273                    return 1;
    252274            }
     
    255277
    256278    if (!outputFile.isEmpty())
    257         return releaseTranslator(tor, outputFile, verbose, ignoreUnfinished,
    258                                  removeIdentical, mode) ? 0 : 1;
     279        return releaseTranslator(tor, outputFile, cd, removeIdentical) ? 0 : 1;
    259280
    260281    return 0;
    261282}
     283
     284#ifdef QT_BOOTSTRAPPED
     285
     286#ifdef Q_OS_WIN
     287# include <windows.h>
     288#endif
     289
     290static QString binDir;
     291
     292static 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
     346QT_BEGIN_NAMESPACE
     347
     348// The name is hard-coded in QLibraryInfo
     349QString qmake_libraryInfoFile()
     350{
     351    if (binDir.isEmpty())
     352        return QString();
     353    return QDir(binDir).filePath(QString::fromLatin1("qt.conf"));
     354}
     355
     356QT_END_NAMESPACE
     357
     358#endif // QT_BOOTSTRAPPED
Note: See TracChangeset for help on using the changeset viewer.