| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the Qt Linguist of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 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 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "translator.h" | 
|---|
| 43 | #include "profileevaluator.h" | 
|---|
| 44 |  | 
|---|
| 45 | #ifndef QT_BOOTSTRAPPED | 
|---|
| 46 | #include <QtCore/QCoreApplication> | 
|---|
| 47 | #include <QtCore/QTranslator> | 
|---|
| 48 | #endif | 
|---|
| 49 | #include <QtCore/QDebug> | 
|---|
| 50 | #include <QtCore/QDir> | 
|---|
| 51 | #include <QtCore/QFile> | 
|---|
| 52 | #include <QtCore/QFileInfo> | 
|---|
| 53 | #include <QtCore/QRegExp> | 
|---|
| 54 | #include <QtCore/QString> | 
|---|
| 55 | #include <QtCore/QStringList> | 
|---|
| 56 | #include <QtCore/QTextStream> | 
|---|
| 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 | 
|---|
| 67 |  | 
|---|
| 68 | static void printOut(const QString & out) | 
|---|
| 69 | { | 
|---|
| 70 | QTextStream stream(stdout); | 
|---|
| 71 | stream << out; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | static void printUsage() | 
|---|
| 75 | { | 
|---|
| 76 | printOut(QCoreApplication::tr( | 
|---|
| 77 | "Usage:\n" | 
|---|
| 78 | "    lrelease [options] project-file\n" | 
|---|
| 79 | "    lrelease [options] ts-files [-qm qm-file]\n\n" | 
|---|
| 80 | "lrelease is part of Qt's Linguist tool chain. It can be used as a\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" | 
|---|
| 83 | "Options:\n" | 
|---|
| 84 | "    -help  Display this information and exit\n" | 
|---|
| 85 | "    -idbased\n" | 
|---|
| 86 | "           Use IDs instead of source strings for message keying\n" | 
|---|
| 87 | "    -compress\n" | 
|---|
| 88 | "           Compress the QM files\n" | 
|---|
| 89 | "    -nounfinished\n" | 
|---|
| 90 | "           Do not include unfinished translations\n" | 
|---|
| 91 | "    -removeidentical\n" | 
|---|
| 92 | "           If the translated text is the same as\n" | 
|---|
| 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" | 
|---|
| 97 | "    -silent\n" | 
|---|
| 98 | "           Do not explain what is being done\n" | 
|---|
| 99 | "    -version\n" | 
|---|
| 100 | "           Display the version of lrelease and exit\n" | 
|---|
| 101 | )); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | static bool loadTsFile(Translator &tor, const QString &tsFileName, bool /* verbose */) | 
|---|
| 105 | { | 
|---|
| 106 | ConversionData cd; | 
|---|
| 107 | bool ok = tor.load(tsFileName, cd, QLatin1String("auto")); | 
|---|
| 108 | if (!ok) { | 
|---|
| 109 | qWarning("lrelease error: %s\n", qPrintable(cd.error())); | 
|---|
| 110 | } else { | 
|---|
| 111 | if (!cd.errors().isEmpty()) | 
|---|
| 112 | printOut(cd.error()); | 
|---|
| 113 | } | 
|---|
| 114 | cd.clearErrors(); | 
|---|
| 115 | return ok; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | static bool releaseTranslator(Translator &tor, const QString &qmFileName, | 
|---|
| 119 | ConversionData &cd, bool removeIdentical) | 
|---|
| 120 | { | 
|---|
| 121 | tor.reportDuplicates(tor.resolveDuplicates(), qmFileName, cd.isVerbose()); | 
|---|
| 122 |  | 
|---|
| 123 | if (cd.isVerbose()) | 
|---|
| 124 | printOut(QCoreApplication::tr( "Updating '%1'...\n").arg(qmFileName)); | 
|---|
| 125 | if (removeIdentical) { | 
|---|
| 126 | if (cd.isVerbose()) | 
|---|
| 127 | printOut(QCoreApplication::tr( "Removing translations equal to source text in '%1'...\n").arg(qmFileName)); | 
|---|
| 128 | tor.stripIdenticalSourceTranslations(); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | QFile file(qmFileName); | 
|---|
| 132 | if (!file.open(QIODevice::WriteOnly)) { | 
|---|
| 133 | qWarning("lrelease error: cannot create '%s': %s\n", | 
|---|
| 134 | qPrintable(qmFileName), qPrintable(file.errorString())); | 
|---|
| 135 | return false; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | tor.normalizeTranslations(cd); | 
|---|
| 139 | bool ok = tor.release(&file, cd); | 
|---|
| 140 | file.close(); | 
|---|
| 141 |  | 
|---|
| 142 | if (!ok) { | 
|---|
| 143 | qWarning("lrelease error: cannot save '%s': %s\n", | 
|---|
| 144 | qPrintable(qmFileName), qPrintable(cd.error())); | 
|---|
| 145 | } else if (!cd.errors().isEmpty()) { | 
|---|
| 146 | printOut(cd.error()); | 
|---|
| 147 | } | 
|---|
| 148 | cd.clearErrors(); | 
|---|
| 149 | return ok; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | static bool releaseTsFile(const QString& tsFileName, | 
|---|
| 153 | ConversionData &cd, bool removeIdentical) | 
|---|
| 154 | { | 
|---|
| 155 | Translator tor; | 
|---|
| 156 | if (!loadTsFile(tor, tsFileName, cd.isVerbose())) | 
|---|
| 157 | return false; | 
|---|
| 158 |  | 
|---|
| 159 | QString qmFileName = tsFileName; | 
|---|
| 160 | foreach (const Translator::FileFormat &fmt, Translator::registeredFileFormats()) { | 
|---|
| 161 | if (qmFileName.endsWith(QLatin1Char('.') + fmt.extension)) { | 
|---|
| 162 | qmFileName.chop(fmt.extension.length() + 1); | 
|---|
| 163 | break; | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|
| 166 | qmFileName += QLatin1String(".qm"); | 
|---|
| 167 |  | 
|---|
| 168 | return releaseTranslator(tor, qmFileName, cd, removeIdentical); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | int main(int argc, char **argv) | 
|---|
| 172 | { | 
|---|
| 173 | #ifdef QT_BOOTSTRAPPED | 
|---|
| 174 | initBinaryDir( | 
|---|
| 175 | #ifndef Q_OS_WIN | 
|---|
| 176 | argv[0] | 
|---|
| 177 | #endif | 
|---|
| 178 | ); | 
|---|
| 179 | #else | 
|---|
| 180 | QCoreApplication app(argc, argv); | 
|---|
| 181 | QTranslator translator; | 
|---|
| 182 | if (translator.load(QLatin1String("lrelease_") + QLocale::system().name())) | 
|---|
| 183 | app.installTranslator(&translator); | 
|---|
| 184 | #endif | 
|---|
| 185 |  | 
|---|
| 186 | ConversionData cd; | 
|---|
| 187 | cd.m_verbose = true; // the default is true starting with Qt 4.2 | 
|---|
| 188 | bool removeIdentical = false; | 
|---|
| 189 | Translator tor; | 
|---|
| 190 | QStringList inputFiles; | 
|---|
| 191 | QString outputFile; | 
|---|
| 192 |  | 
|---|
| 193 | for (int i = 1; i < argc; ++i) { | 
|---|
| 194 | if (!strcmp(argv[i], "-compress")) { | 
|---|
| 195 | cd.m_saveMode = SaveStripped; | 
|---|
| 196 | continue; | 
|---|
| 197 | } else if (!strcmp(argv[i], "-idbased")) { | 
|---|
| 198 | cd.m_idBased = true; | 
|---|
| 199 | continue; | 
|---|
| 200 | } else if (!strcmp(argv[i], "-nocompress")) { | 
|---|
| 201 | cd.m_saveMode = SaveEverything; | 
|---|
| 202 | continue; | 
|---|
| 203 | } else if (!strcmp(argv[i], "-removeidentical")) { | 
|---|
| 204 | removeIdentical = true; | 
|---|
| 205 | continue; | 
|---|
| 206 | } else if (!strcmp(argv[i], "-nounfinished")) { | 
|---|
| 207 | cd.m_ignoreUnfinished = true; | 
|---|
| 208 | continue; | 
|---|
| 209 | } else if (!strcmp(argv[i], "-markuntranslated")) { | 
|---|
| 210 | if (i == argc - 1) { | 
|---|
| 211 | printUsage(); | 
|---|
| 212 | return 1; | 
|---|
| 213 | } | 
|---|
| 214 | cd.m_unTrPrefix = QString::fromLocal8Bit(argv[++i]); | 
|---|
| 215 | } else if (!strcmp(argv[i], "-silent")) { | 
|---|
| 216 | cd.m_verbose = false; | 
|---|
| 217 | continue; | 
|---|
| 218 | } else if (!strcmp(argv[i], "-verbose")) { | 
|---|
| 219 | cd.m_verbose = true; | 
|---|
| 220 | continue; | 
|---|
| 221 | } else if (!strcmp(argv[i], "-version")) { | 
|---|
| 222 | printOut(QCoreApplication::tr( "lrelease version %1\n").arg(QLatin1String(QT_VERSION_STR)) ); | 
|---|
| 223 | return 0; | 
|---|
| 224 | } else if (!strcmp(argv[i], "-qm")) { | 
|---|
| 225 | if (i == argc - 1) { | 
|---|
| 226 | printUsage(); | 
|---|
| 227 | return 1; | 
|---|
| 228 | } | 
|---|
| 229 | outputFile = QString::fromLocal8Bit(argv[++i]); | 
|---|
| 230 | } else if (!strcmp(argv[i], "-help")) { | 
|---|
| 231 | printUsage(); | 
|---|
| 232 | return 0; | 
|---|
| 233 | } else if (argv[i][0] == '-') { | 
|---|
| 234 | printUsage(); | 
|---|
| 235 | return 1; | 
|---|
| 236 | } else { | 
|---|
| 237 | inputFiles << QString::fromLocal8Bit(argv[i]); | 
|---|
| 238 | } | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | if (inputFiles.isEmpty()) { | 
|---|
| 242 | printUsage(); | 
|---|
| 243 | return 1; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | foreach (const QString &inputFile, inputFiles) { | 
|---|
| 247 | if (inputFile.endsWith(QLatin1String(".pro"), Qt::CaseInsensitive) | 
|---|
| 248 | || inputFile.endsWith(QLatin1String(".pri"), Qt::CaseInsensitive)) { | 
|---|
| 249 | QFileInfo fi(inputFile); | 
|---|
| 250 | ProFile pro(fi.absoluteFilePath()); | 
|---|
| 251 |  | 
|---|
| 252 | ProFileEvaluator visitor; | 
|---|
| 253 | visitor.setVerbose(cd.isVerbose()); | 
|---|
| 254 |  | 
|---|
| 255 | if (!visitor.queryProFile(&pro)) { | 
|---|
| 256 | qWarning("lrelease error: cannot read project file '%s'.", qPrintable(inputFile)); | 
|---|
| 257 | continue; | 
|---|
| 258 | } | 
|---|
| 259 | if (!visitor.accept(&pro)) { | 
|---|
| 260 | qWarning("lrelease error: cannot process project file '%s'.", qPrintable(inputFile)); | 
|---|
| 261 | continue; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | QStringList translations = visitor.values(QLatin1String("TRANSLATIONS")); | 
|---|
| 265 | if (translations.isEmpty()) { | 
|---|
| 266 | qWarning("lrelease warning: Met no 'TRANSLATIONS' entry in" | 
|---|
| 267 | " project file '%s'\n", | 
|---|
| 268 | qPrintable(inputFile)); | 
|---|
| 269 | } else { | 
|---|
| 270 | QDir proDir(fi.absolutePath()); | 
|---|
| 271 | foreach (const QString &trans, translations) | 
|---|
| 272 | if (!releaseTsFile(QFileInfo(proDir, trans).filePath(), cd, removeIdentical)) | 
|---|
| 273 | return 1; | 
|---|
| 274 | } | 
|---|
| 275 | } else { | 
|---|
| 276 | if (outputFile.isEmpty()) { | 
|---|
| 277 | if (!releaseTsFile(inputFile, cd, removeIdentical)) | 
|---|
| 278 | return 1; | 
|---|
| 279 | } else { | 
|---|
| 280 | if (!loadTsFile(tor, inputFile, cd.isVerbose())) | 
|---|
| 281 | return 1; | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | if (!outputFile.isEmpty()) | 
|---|
| 287 | return releaseTranslator(tor, outputFile, cd, removeIdentical) ? 0 : 1; | 
|---|
| 288 |  | 
|---|
| 289 | return 0; | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | #ifdef QT_BOOTSTRAPPED | 
|---|
| 293 |  | 
|---|
| 294 | #ifdef Q_OS_WIN | 
|---|
| 295 | # include <windows.h> | 
|---|
| 296 | #endif | 
|---|
| 297 |  | 
|---|
| 298 | static QString binDir; | 
|---|
| 299 |  | 
|---|
| 300 | static void initBinaryDir( | 
|---|
| 301 | #ifndef Q_OS_WIN | 
|---|
| 302 | const char *_argv0 | 
|---|
| 303 | #endif | 
|---|
| 304 | ) | 
|---|
| 305 | { | 
|---|
| 306 | #ifdef Q_OS_WIN | 
|---|
| 307 | wchar_t module_name[MAX_PATH]; | 
|---|
| 308 | GetModuleFileName(0, module_name, MAX_PATH); | 
|---|
| 309 | QFileInfo filePath = QString::fromWCharArray(module_name); | 
|---|
| 310 | binDir = filePath.filePath(); | 
|---|
| 311 | #else | 
|---|
| 312 | QString argv0 = QFile::decodeName(QByteArray(_argv0)); | 
|---|
| 313 | QString absPath; | 
|---|
| 314 |  | 
|---|
| 315 | if (!argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) { | 
|---|
| 316 | /* | 
|---|
| 317 | If argv0 starts with a slash, it is already an absolute | 
|---|
| 318 | file path. | 
|---|
| 319 | */ | 
|---|
| 320 | absPath = argv0; | 
|---|
| 321 | } else if (argv0.contains(QLatin1Char('/'))) { | 
|---|
| 322 | /* | 
|---|
| 323 | If argv0 contains one or more slashes, it is a file path | 
|---|
| 324 | relative to the current directory. | 
|---|
| 325 | */ | 
|---|
| 326 | absPath = QDir::current().absoluteFilePath(argv0); | 
|---|
| 327 | } else { | 
|---|
| 328 | /* | 
|---|
| 329 | Otherwise, the file path has to be determined using the | 
|---|
| 330 | PATH environment variable. | 
|---|
| 331 | */ | 
|---|
| 332 | QByteArray pEnv = qgetenv("PATH"); | 
|---|
| 333 | QDir currentDir = QDir::current(); | 
|---|
| 334 | QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1String(":")); | 
|---|
| 335 | for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) { | 
|---|
| 336 | if ((*p).isEmpty()) | 
|---|
| 337 | continue; | 
|---|
| 338 | QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0); | 
|---|
| 339 | QFileInfo candidate_fi(candidate); | 
|---|
| 340 | if (candidate_fi.exists() && !candidate_fi.isDir()) { | 
|---|
| 341 | binDir = candidate_fi.canonicalPath(); | 
|---|
| 342 | return; | 
|---|
| 343 | } | 
|---|
| 344 | } | 
|---|
| 345 | return; | 
|---|
| 346 | } | 
|---|
| 347 |  | 
|---|
| 348 | QFileInfo fi(absPath); | 
|---|
| 349 | if (fi.exists()) | 
|---|
| 350 | binDir = fi.canonicalPath(); | 
|---|
| 351 | #endif | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | QT_BEGIN_NAMESPACE | 
|---|
| 355 |  | 
|---|
| 356 | // The name is hard-coded in QLibraryInfo | 
|---|
| 357 | QString qmake_libraryInfoFile() | 
|---|
| 358 | { | 
|---|
| 359 | if (binDir.isEmpty()) | 
|---|
| 360 | return QString(); | 
|---|
| 361 | return QDir(binDir).filePath(QString::fromLatin1("qt.conf")); | 
|---|
| 362 | } | 
|---|
| 363 |  | 
|---|
| 364 | QT_END_NAMESPACE | 
|---|
| 365 |  | 
|---|
| 366 | #endif // QT_BOOTSTRAPPED | 
|---|