| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the Qt Linguist of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 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. | 
|---|
| 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 are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "translator.h" | 
|---|
| 43 | #include "translatortools.h" | 
|---|
| 44 | #include "profileevaluator.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include <QtCore/QCoreApplication> | 
|---|
| 47 | #include <QtCore/QDebug> | 
|---|
| 48 | #include <QtCore/QDir> | 
|---|
| 49 | #include <QtCore/QFile> | 
|---|
| 50 | #include <QtCore/QFileInfo> | 
|---|
| 51 | #include <QtCore/QRegExp> | 
|---|
| 52 | #include <QtCore/QString> | 
|---|
| 53 | #include <QtCore/QStringList> | 
|---|
| 54 | #include <QtCore/QTextStream> | 
|---|
| 55 | #include <QtCore/QTranslator> | 
|---|
| 56 |  | 
|---|
| 57 | static void printOut(const QString & out) | 
|---|
| 58 | { | 
|---|
| 59 | QTextStream stream(stdout); | 
|---|
| 60 | stream << out; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | static void printUsage() | 
|---|
| 64 | { | 
|---|
| 65 | printOut(QCoreApplication::tr( | 
|---|
| 66 | "Usage:\n" | 
|---|
| 67 | "    lrelease [options] project-file\n" | 
|---|
| 68 | "    lrelease [options] ts-files [-qm qm-file]\n\n" | 
|---|
| 69 | "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" | 
|---|
| 72 | "Options:\n" | 
|---|
| 73 | "    -help  Display this information and exit\n" | 
|---|
| 74 | "    -compress\n" | 
|---|
| 75 | "           Compress the .qm files\n" | 
|---|
| 76 | "    -nounfinished\n" | 
|---|
| 77 | "           Do not include unfinished translations\n" | 
|---|
| 78 | "    -removeidentical\n" | 
|---|
| 79 | "           If the translated text is the same as\n" | 
|---|
| 80 | "           the source text, do not include the message\n" | 
|---|
| 81 | "    -silent\n" | 
|---|
| 82 | "           Don't explain what is being done\n" | 
|---|
| 83 | "    -version\n" | 
|---|
| 84 | "           Display the version of lrelease and exit\n" | 
|---|
| 85 | )); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | static bool loadTsFile(Translator &tor, const QString &tsFileName, bool /* verbose */) | 
|---|
| 89 | { | 
|---|
| 90 | ConversionData cd; | 
|---|
| 91 | bool ok = tor.load(tsFileName, cd, QLatin1String("auto")); | 
|---|
| 92 | if (!ok) { | 
|---|
| 93 | qWarning("lrelease error: %s\n", qPrintable(cd.error())); | 
|---|
| 94 | } else { | 
|---|
| 95 | if (!cd.errors().isEmpty()) | 
|---|
| 96 | printOut(cd.error()); | 
|---|
| 97 | } | 
|---|
| 98 | return ok; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | 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) | 
|---|
| 108 | printOut(QCoreApplication::tr( "Updating '%1'...\n").arg(qmFileName)); | 
|---|
| 109 | if (removeIdentical) { | 
|---|
| 110 | if ( verbose ) | 
|---|
| 111 | printOut(QCoreApplication::tr( "Removing translations equal to source text in '%1'...\n").arg(qmFileName)); | 
|---|
| 112 | tor.stripIdenticalSourceTranslations(); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | QFile file(qmFileName); | 
|---|
| 116 | if (!file.open(QIODevice::WriteOnly)) { | 
|---|
| 117 | qWarning("lrelease error: cannot create '%s': %s\n", | 
|---|
| 118 | qPrintable(qmFileName), qPrintable(file.errorString())); | 
|---|
| 119 | return false; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | ConversionData cd; | 
|---|
| 123 | cd.m_verbose = verbose; | 
|---|
| 124 | cd.m_ignoreUnfinished = ignoreUnfinished; | 
|---|
| 125 | cd.m_saveMode = mode; | 
|---|
| 126 | bool ok = tor.release(&file, cd); | 
|---|
| 127 | file.close(); | 
|---|
| 128 |  | 
|---|
| 129 | if (!ok) { | 
|---|
| 130 | qWarning("lrelease error: cannot save '%s': %s\n", | 
|---|
| 131 | qPrintable(qmFileName), qPrintable(cd.error())); | 
|---|
| 132 | return false; | 
|---|
| 133 | } else if (!cd.errors().isEmpty()) { | 
|---|
| 134 | printOut(cd.error()); | 
|---|
| 135 | } | 
|---|
| 136 | return true; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | static bool releaseTsFile(const QString& tsFileName, bool verbose, | 
|---|
| 140 | bool ignoreUnfinished, bool removeIdentical, TranslatorSaveMode mode) | 
|---|
| 141 | { | 
|---|
| 142 | Translator tor; | 
|---|
| 143 | if (!loadTsFile(tor, tsFileName, verbose)) | 
|---|
| 144 | return false; | 
|---|
| 145 |  | 
|---|
| 146 | QString qmFileName = tsFileName; | 
|---|
| 147 | foreach (const Translator::FileFormat &fmt, Translator::registeredFileFormats()) { | 
|---|
| 148 | if (qmFileName.endsWith(QLatin1Char('.') + fmt.extension)) { | 
|---|
| 149 | qmFileName.chop(fmt.extension.length() + 1); | 
|---|
| 150 | break; | 
|---|
| 151 | } | 
|---|
| 152 | } | 
|---|
| 153 | qmFileName += QLatin1String(".qm"); | 
|---|
| 154 |  | 
|---|
| 155 | return releaseTranslator(tor, qmFileName, verbose, ignoreUnfinished, removeIdentical, mode); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | int main(int argc, char **argv) | 
|---|
| 159 | { | 
|---|
| 160 | QCoreApplication app(argc, argv); | 
|---|
| 161 | QStringList args = app.arguments(); | 
|---|
| 162 | QTranslator translator; | 
|---|
| 163 | if (translator.load(QLatin1String("lrelease_") + QLocale::system().name())) | 
|---|
| 164 | 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; | 
|---|
| 170 | bool removeIdentical = false; | 
|---|
| 171 | Translator tor; | 
|---|
| 172 | QString outputFile; | 
|---|
| 173 | int numFiles = 0; | 
|---|
| 174 |  | 
|---|
| 175 | 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")) { | 
|---|
| 183 | removeIdentical = true; | 
|---|
| 184 | 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")) { | 
|---|
| 198 | if (i == argc - 1) { | 
|---|
| 199 | printUsage(); | 
|---|
| 200 | return 1; | 
|---|
| 201 | } | 
|---|
| 202 | i++; | 
|---|
| 203 | outputFile = args[i]; | 
|---|
| 204 | } else if (args[i] == QLatin1String("-help")) { | 
|---|
| 205 | printUsage(); | 
|---|
| 206 | return 0; | 
|---|
| 207 | } else if (args[i][0] == QLatin1Char('-')) { | 
|---|
| 208 | printUsage(); | 
|---|
| 209 | return 1; | 
|---|
| 210 | } else { | 
|---|
| 211 | numFiles++; | 
|---|
| 212 | } | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | if (numFiles == 0) { | 
|---|
| 216 | printUsage(); | 
|---|
| 217 | return 1; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 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)) { | 
|---|
| 226 | QHash<QByteArray, QStringList> varMap; | 
|---|
| 227 | bool ok = evaluateProFile(args[i], verbose, &varMap ); | 
|---|
| 228 | if (ok) { | 
|---|
| 229 | QStringList translations = varMap.value("TRANSLATIONS"); | 
|---|
| 230 | if (translations.isEmpty()) { | 
|---|
| 231 | qWarning("lrelease warning: Met no 'TRANSLATIONS' entry in" | 
|---|
| 232 | " project file '%s'\n", | 
|---|
| 233 | qPrintable(args[i])); | 
|---|
| 234 | } else { | 
|---|
| 235 | foreach (const QString &trans, translations) | 
|---|
| 236 | if (!releaseTsFile(trans, verbose, ignoreUnfinished, removeIdentical, mode)) | 
|---|
| 237 | return 1; | 
|---|
| 238 | } | 
|---|
| 239 | } else { | 
|---|
| 240 | 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" | 
|---|
| 242 | "Try the following syntax:\n" | 
|---|
| 243 | "    lrelease [options] ts-files [-qm qm-file]\n"); | 
|---|
| 244 | } | 
|---|
| 245 | } else { | 
|---|
| 246 | if (outputFile.isEmpty()) { | 
|---|
| 247 | if (!releaseTsFile(args[i], verbose, ignoreUnfinished, removeIdentical, mode)) | 
|---|
| 248 | return 1; | 
|---|
| 249 | } else { | 
|---|
| 250 | if (!loadTsFile(tor, args[i], verbose)) | 
|---|
| 251 | return 1; | 
|---|
| 252 | } | 
|---|
| 253 | } | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | if (!outputFile.isEmpty()) | 
|---|
| 257 | return releaseTranslator(tor, outputFile, verbose, ignoreUnfinished, | 
|---|
| 258 | removeIdentical, mode) ? 0 : 1; | 
|---|
| 259 |  | 
|---|
| 260 | return 0; | 
|---|
| 261 | } | 
|---|