source: trunk/tools/linguist/lupdate/main.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 20.8 KB
Line 
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/QString>
52#include <QtCore/QStringList>
53#include <QtCore/QTextCodec>
54
55static QString m_defaultExtensions;
56
57static void printOut(const QString & out)
58{
59 qWarning("%s", qPrintable(out));
60}
61
62static void recursiveFileInfoList(const QDir &dir,
63 const QStringList &nameFilters, QDir::Filters filter, bool recursive,
64 QFileInfoList *fileinfolist)
65{
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 }
80}
81
82static void printUsage()
83{
84 printOut(QObject::tr(
85 "Usage:\n"
86 " lupdate [options] [project-file]\n"
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 .ts files.\n\n"
92 "Options:\n"
93 " -help Display this information and exit.\n"
94 " -no-obsolete\n"
95 " Drop all obsolete strings.\n"
96 " -extensions <ext>[,<ext>]...\n"
97 " Process files with the given extensions only.\n"
98 " The extension list must be separated with commas, not with whitespace.\n"
99 " Default: '%1'.\n"
100 " -pluralonly\n"
101 " Only include plural form messages.\n"
102 " -silent\n"
103 " Do not explain what is being done.\n"
104 " -no-sort\n"
105 " Do not sort contexts in .ts files.\n"
106 " -no-recursive\n"
107 " Do not recursively scan the following directories.\n"
108 " -recursive\n"
109 " Recursively scan the following directories.\n"
110 " -locations {absolute|relative|none}\n"
111 " Specify/override how source code references are saved in ts files.\n"
112 " Default is absolute.\n"
113 " -no-ui-lines\n"
114 " Do not record line numbers in references to .ui files.\n"
115 " -disable-heuristic {sametext|similartext|number}\n"
116 " Disable the named merge heuristic. Can be specified multiple times.\n"
117 " -pro <filename>\n"
118 " Name of a .pro file. Useful for files with .pro\n"
119 " file syntax but different file suffix\n"
120 " -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"
123 " -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 " -version\n"
128 " Display the version of lupdate and exit.\n"
129 ).arg(m_defaultExtensions));
130}
131
132static void updateTsFiles(const Translator &fetchedTor, const QStringList &tsFileNames,
133 const QByteArray &codecForTr, const QString &sourceLanguage, const QString &targetLanguage,
134 UpdateOptions options, bool *fail)
135{
136 QDir dir;
137 QString err;
138 foreach (const QString &fileName, tsFileNames) {
139 QString fn = dir.relativeFilePath(fileName);
140 ConversionData cd;
141 Translator tor;
142 cd.m_sortContexts = !(options & NoSort);
143 if (QFile(fileName).exists()) {
144 if (!tor.load(fileName, cd, QLatin1String("auto"))) {
145 printOut(cd.error());
146 *fail = true;
147 continue;
148 }
149 tor.resolveDuplicates();
150 cd.clearErrors();
151 if (!codecForTr.isEmpty() && codecForTr != tor.codecName())
152 qWarning("lupdate warning: Codec for tr() '%s' disagrees with "
153 "existing file's codec '%s'. Expect trouble.",
154 codecForTr.constData(), tor.codecName().constData());
155 if (!targetLanguage.isEmpty() && targetLanguage != tor.languageCode())
156 qWarning("lupdate warning: Specified target language '%s' disagrees with "
157 "existing file's language '%s'. Ignoring.",
158 qPrintable(targetLanguage), qPrintable(tor.languageCode()));
159 if (!sourceLanguage.isEmpty() && sourceLanguage != tor.sourceLanguageCode())
160 qWarning("lupdate warning: Specified source language '%s' disagrees with "
161 "existing file's language '%s'. Ignoring.",
162 qPrintable(sourceLanguage), qPrintable(tor.sourceLanguageCode()));
163 } else {
164 if (!codecForTr.isEmpty())
165 tor.setCodecName(codecForTr);
166 if (!targetLanguage.isEmpty())
167 tor.setLanguageCode(targetLanguage);
168 if (!sourceLanguage.isEmpty())
169 tor.setSourceLanguageCode(sourceLanguage);
170 }
171 tor.makeFileNamesAbsolute(QFileInfo(fileName).absoluteDir());
172 if (options & NoLocations)
173 tor.setLocationsType(Translator::NoLocations);
174 else if (options & RelativeLocations)
175 tor.setLocationsType(Translator::RelativeLocations);
176 else if (options & AbsoluteLocations)
177 tor.setLocationsType(Translator::AbsoluteLocations);
178 if (options & Verbose)
179 printOut(QObject::tr("Updating '%1'...\n").arg(fn));
180
181 if (tor.locationsType() == Translator::NoLocations) // Could be set from file
182 options |= NoLocations;
183 Translator out = merge(tor, fetchedTor, options, err);
184 if (!codecForTr.isEmpty())
185 out.setCodecName(codecForTr);
186
187 if ((options & Verbose) && !err.isEmpty()) {
188 printOut(err);
189 err.clear();
190 }
191 if (options & PluralOnly) {
192 if (options & Verbose)
193 printOut(QObject::tr("Stripping non plural forms in '%1'...\n").arg(fn));
194 out.stripNonPluralForms();
195 }
196 if (options & NoObsolete)
197 out.stripObsoleteMessages();
198 out.stripEmptyContexts();
199
200 if (!out.save(fileName, cd, QLatin1String("auto"))) {
201 printOut(cd.error());
202 *fail = true;
203 }
204 }
205}
206
207int main(int argc, char **argv)
208{
209 QCoreApplication app(argc, argv);
210 m_defaultExtensions = QLatin1String("ui,c,c++,cc,cpp,cxx,ch,h,h++,hh,hpp,hxx");
211
212 QStringList args = app.arguments();
213 QString defaultContext; // This was QLatin1String("@default") before.
214 Translator fetchedTor;
215 QByteArray codecForTr;
216 QByteArray codecForSource;
217 QStringList tsFileNames;
218 QStringList proFiles;
219 QStringList sourceFiles;
220 QString targetLanguage;
221 QString sourceLanguage;
222
223 UpdateOptions options =
224 Verbose | // verbose is on by default starting with Qt 4.2
225 HeuristicSameText | HeuristicSimilarText | HeuristicNumber;
226 int numFiles = 0;
227 bool standardSyntax = true;
228 bool metTsFlag = false;
229 bool recursiveScan = true;
230
231 QString extensions = m_defaultExtensions;
232 QStringList extensionsNameFilters;
233
234 for (int i = 1; i < argc; ++i) {
235 if (args.at(i) == QLatin1String("-ts"))
236 standardSyntax = false;
237 }
238
239 for (int i = 1; i < argc; ++i) {
240 QString arg = args.at(i);
241 if (arg == QLatin1String("-help")
242 || arg == QLatin1String("--help")
243 || arg == QLatin1String("-h")) {
244 printUsage();
245 return 0;
246 } else if (arg == QLatin1String("-pluralonly")) {
247 options |= PluralOnly;
248 continue;
249 } else if (arg == QLatin1String("-noobsolete")
250 || arg == QLatin1String("-no-obsolete")) {
251 options |= NoObsolete;
252 continue;
253 } else if (arg == QLatin1String("-silent")) {
254 options &= ~Verbose;
255 continue;
256 } else if (arg == QLatin1String("-target-language")) {
257 ++i;
258 if (i == argc) {
259 qWarning("The option -target-language requires a parameter.");
260 return 1;
261 }
262 targetLanguage = args[i];
263 continue;
264 } else if (arg == QLatin1String("-source-language")) {
265 ++i;
266 if (i == argc) {
267 qWarning("The option -source-language requires a parameter.");
268 return 1;
269 }
270 sourceLanguage = args[i];
271 continue;
272 } else if (arg == QLatin1String("-disable-heuristic")) {
273 ++i;
274 if (i == argc) {
275 qWarning("The option -disable-heuristic requires a parameter.");
276 return 1;
277 }
278 arg = args[i];
279 if (arg == QLatin1String("sametext")) {
280 options &= ~HeuristicSameText;
281 } else if (arg == QLatin1String("similartext")) {
282 options &= ~HeuristicSimilarText;
283 } else if (arg == QLatin1String("number")) {
284 options &= ~HeuristicNumber;
285 } else {
286 qWarning("Invalid heuristic name passed to -disable-heuristic.");
287 return 1;
288 }
289 continue;
290 } else if (arg == QLatin1String("-locations")) {
291 ++i;
292 if (i == argc) {
293 qWarning("The option -locations requires a parameter.");
294 return 1;
295 }
296 if (args[i] == QLatin1String("none")) {
297 options |= NoLocations;
298 } else if (args[i] == QLatin1String("relative")) {
299 options |= RelativeLocations;
300 } else if (args[i] == QLatin1String("absolute")) {
301 options |= AbsoluteLocations;
302 } else {
303 qWarning("Invalid parameter passed to -locations.");
304 return 1;
305 }
306 continue;
307 } else if (arg == QLatin1String("-no-ui-lines")) {
308 options |= NoUiLines;
309 continue;
310 } else if (arg == QLatin1String("-verbose")) {
311 options |= Verbose;
312 continue;
313 } else if (arg == QLatin1String("-no-recursive")) {
314 recursiveScan = false;
315 continue;
316 } else if (arg == QLatin1String("-recursive")) {
317 recursiveScan = true;
318 continue;
319 } else if (arg == QLatin1String("-no-sort")
320 || arg == QLatin1String("-nosort")) {
321 options |= NoSort;
322 continue;
323 } else if (arg == QLatin1String("-version")) {
324 printOut(QObject::tr("lupdate version %1\n").arg(QLatin1String(QT_VERSION_STR)));
325 return 0;
326 } else if (arg == QLatin1String("-ts")) {
327 metTsFlag = true;
328 continue;
329 } else if (arg == QLatin1String("-extensions")) {
330 ++i;
331 if (i == argc) {
332 qWarning("The -extensions option should be followed by an extension list.");
333 return 1;
334 }
335 extensions = args[i];
336 continue;
337 } else if (arg == QLatin1String("-pro")) {
338 ++i;
339 if (i == argc) {
340 qWarning("The -pro option should be followed by a filename of .pro file.");
341 return 1;
342 }
343 proFiles += args[i];
344 numFiles++;
345 continue;
346 } else if (arg.startsWith(QLatin1String("-")) && arg != QLatin1String("-")) {
347 qWarning("Unrecognized option '%s'", qPrintable(arg));
348 return 1;
349 }
350
351 numFiles++;
352
353 QString fullText;
354
355 codecForTr.clear();
356 codecForSource.clear();
357
358 if (metTsFlag) {
359 bool found = false;
360 foreach (const Translator::FileFormat &fmt, Translator::registeredFileFormats()) {
361 if (arg.endsWith(QLatin1Char('.') + fmt.extension, Qt::CaseInsensitive)) {
362 QFileInfo fi(arg);
363 if (!fi.exists() || fi.isWritable()) {
364 tsFileNames.append(QFileInfo(arg).absoluteFilePath());
365 } else {
366 qWarning("lupdate warning: For some reason, '%s' is not writable.\n",
367 qPrintable(arg));
368 }
369 found = true;
370 break;
371 }
372 }
373 if (!found) {
374 qWarning("lupdate error: File '%s' has no recognized extension\n",
375 qPrintable(arg));
376 return 1;
377 }
378 } else if (arg.endsWith(QLatin1String(".pro"), Qt::CaseInsensitive)
379 || arg.endsWith(QLatin1String(".pri"), Qt::CaseInsensitive)) {
380 proFiles << arg;
381 } else {
382 QFileInfo fi(arg);
383 if (!fi.exists()) {
384 qWarning("lupdate error: File '%s' does not exists\n", qPrintable(arg));
385 return 1;
386 } else if (fi.isDir()) {
387 if (options & Verbose)
388 printOut(QObject::tr("Scanning directory '%1'...").arg(arg));
389 QDir dir = QDir(fi.filePath());
390 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(','));
400 }
401 QDir::Filters filters = QDir::Files | QDir::NoSymLinks;
402 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('/'));
410 }
411 } else {
412 sourceFiles << fi.canonicalFilePath().replace(QLatin1Char('\\'), QLatin1Char('/'));
413 }
414 }
415 } // for args
416
417
418 bool firstPass = true;
419 bool fail = false;
420 while (firstPass || !proFiles.isEmpty()) {
421 ConversionData cd;
422 cd.m_defaultContext = defaultContext;
423 cd.m_noUiLines = options & NoUiLines;
424
425 QStringList tsFiles = tsFileNames;
426 if (proFiles.count() > 0) {
427 QFileInfo pfi(proFiles.takeFirst());
428 QHash<QByteArray, QStringList> variables;
429
430 ProFileEvaluator visitor;
431 visitor.setVerbose(options & Verbose);
432
433 ProFile pro(pfi.absoluteFilePath());
434 if (!visitor.queryProFile(&pro))
435 return 2;
436 if (!visitor.accept(&pro))
437 return 2;
438
439 if (visitor.templateType() == ProFileEvaluator::TT_Subdirs) {
440 QDir proDir(pfi.absoluteDir());
441 foreach (const QString &subdir, visitor.values(QLatin1String("SUBDIRS"))) {
442 QString subPro = QDir::cleanPath(proDir.absoluteFilePath(subdir));
443 QFileInfo subInfo(subPro);
444 if (subInfo.isDir())
445 proFiles << (subPro + QLatin1Char('/')
446 + subInfo.fileName() + QLatin1String(".pro"));
447 else
448 proFiles << subPro;
449 }
450 continue;
451 }
452
453 evaluateProFile(visitor, &variables);
454
455 sourceFiles = variables.value("SOURCES");
456
457 QStringList tmp = variables.value("CODECFORTR");
458 if (!tmp.isEmpty() && !tmp.first().isEmpty()) {
459 codecForTr = tmp.first().toLatin1();
460 fetchedTor.setCodecName(codecForTr);
461 }
462 tmp = variables.value("CODECFORSRC");
463 if (!tmp.isEmpty() && !tmp.first().isEmpty()) {
464 codecForSource = tmp.first().toLatin1();
465 if (!QTextCodec::codecForName(codecForSource))
466 qWarning("lupdate warning: Codec for source '%s' is invalid. Falling back to codec for tr().",
467 codecForSource.constData());
468 else
469 cd.m_codecForSource = codecForSource;
470 }
471
472 tsFiles += variables.value("TRANSLATIONS");
473 }
474
475 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 }
488 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 }
496 if (!cd.error().isEmpty())
497 printOut(cd.error());
498
499 tsFiles.sort();
500 tsFiles.removeDuplicates();
501
502 if (!tsFiles.isEmpty())
503 updateTsFiles(fetchedTor, tsFiles, codecForTr, sourceLanguage, targetLanguage, options, &fail);
504
505 firstPass = false;
506 }
507
508 if (numFiles == 0) {
509 printUsage();
510 return 1;
511 }
512
513 return fail ? 1 : 0;
514}
Note: See TracBrowser for help on using the repository browser.