/tools/linguist/lconvert/main.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 301 lines · 242 code · 19 blank · 40 comment · 97 complexity · d1289fd6729c42ccdecd676c8ab3e880 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 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. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "translator.h"
  42. #include <QtCore/QCoreApplication>
  43. #include <QtCore/QDebug>
  44. #include <QtCore/QString>
  45. #include <QtCore/QStringList>
  46. #include <QtCore/QTranslator>
  47. #include <QtCore/QLibraryInfo>
  48. #include <iostream>
  49. QT_USE_NAMESPACE
  50. class LC {
  51. Q_DECLARE_TR_FUNCTIONS(LConvert)
  52. };
  53. static int usage(const QStringList &args)
  54. {
  55. Q_UNUSED(args);
  56. QString loaders;
  57. QString line(QLatin1String(" %1 - %2\n"));
  58. foreach (Translator::FileFormat format, Translator::registeredFileFormats())
  59. loaders += line.arg(format.extension, -5).arg(format.description);
  60. std::cout << qPrintable(LC::tr("\nUsage:\n"
  61. " lconvert [options] <infile> [<infile>...]\n\n"
  62. "lconvert is part of Qt's Linguist tool chain. It can be used as a\n"
  63. "stand-alone tool to convert and filter translation data files.\n"
  64. "The following file formats are supported:\n\n%1\n"
  65. "If multiple input files are specified, they are merged with\n"
  66. "translations from later files taking precedence.\n\n"
  67. "Options:\n"
  68. " -h\n"
  69. " --help Display this information and exit.\n\n"
  70. " -i <infile>\n"
  71. " --input-file <infile>\n"
  72. " Specify input file. Use if <infile> might start with a dash.\n"
  73. " This option can be used several times to merge inputs.\n"
  74. " May be '-' (standard input) for use in a pipe.\n\n"
  75. " -o <outfile>\n"
  76. " --output-file <outfile>\n"
  77. " Specify output file. Default is '-' (standard output).\n\n"
  78. " -if <informat>\n"
  79. " --input-format <format>\n"
  80. " Specify input format for subsequent <infile>s.\n"
  81. " The format is auto-detected from the file name and defaults to 'ts'.\n\n"
  82. " -of <outformat>\n"
  83. " --output-format <outformat>\n"
  84. " Specify output format. See -if.\n\n"
  85. " --input-codec <codec>\n"
  86. " Specify encoding for QM and PO input files. Default is 'Latin1'\n"
  87. " for QM and 'UTF-8' for PO files. UTF-8 is always tried as well for\n"
  88. " QM, corresponding to the possible use of the trUtf8() function.\n\n"
  89. " --output-codec <codec>\n"
  90. " Specify encoding for PO output files. Default is 'UTF-8'.\n\n"
  91. " --drop-tags <regexp>\n"
  92. " Drop named extra tags when writing TS or XLIFF files.\n"
  93. " May be specified repeatedly.\n\n"
  94. " --drop-translations\n"
  95. " Drop existing translations and reset the status to 'unfinished'.\n"
  96. " Note: this implies --no-obsolete.\n\n"
  97. " --source-language <language>[_<region>]\n"
  98. " Specify/override the language of the source strings. Defaults to\n"
  99. " POSIX if not specified and the file does not name it yet.\n\n"
  100. " --target-language <language>[_<region>]\n"
  101. " Specify/override the language of the translation.\n"
  102. " The target language is guessed from the file name if this option\n"
  103. " is not specified and the file contents name no language yet.\n\n"
  104. " --no-obsolete\n"
  105. " Drop obsolete messages.\n\n"
  106. " --no-finished\n"
  107. " Drop finished messages.\n\n"
  108. " --sort-contexts\n"
  109. " Sort contexts in output TS file alphabetically.\n\n"
  110. " --locations {absolute|relative|none}\n"
  111. " Override how source code references are saved in TS files.\n"
  112. " Default is absolute.\n\n"
  113. " --no-ui-lines\n"
  114. " Drop line numbers from references to UI files.\n\n"
  115. " --verbose\n"
  116. " be a bit more verbose\n\n"
  117. "Long options can be specified with only one leading dash, too.\n\n"
  118. "Return value:\n"
  119. " 0 on success\n"
  120. " 1 on command line parse failures\n"
  121. " 2 on read failures\n"
  122. " 3 on write failures\n").arg(loaders));
  123. return 1;
  124. }
  125. struct File
  126. {
  127. QString name;
  128. QString format;
  129. };
  130. int main(int argc, char *argv[])
  131. {
  132. QCoreApplication app(argc, argv);
  133. #ifndef Q_OS_WIN32
  134. QTranslator translator;
  135. QTranslator qtTranslator;
  136. QString sysLocale = QLocale::system().name();
  137. QString resourceDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
  138. if (translator.load(QLatin1String("linguist_") + sysLocale, resourceDir)
  139. && qtTranslator.load(QLatin1String("qt_") + sysLocale, resourceDir)) {
  140. app.installTranslator(&translator);
  141. app.installTranslator(&qtTranslator);
  142. }
  143. #endif // Q_OS_WIN32
  144. QStringList args = app.arguments();
  145. QList<File> inFiles;
  146. QString inFormat(QLatin1String("auto"));
  147. QString outFileName;
  148. QString outFormat(QLatin1String("auto"));
  149. QString targetLanguage;
  150. QString sourceLanguage;
  151. bool dropTranslations = false;
  152. bool noObsolete = false;
  153. bool noFinished = false;
  154. bool verbose = false;
  155. bool noUiLines = false;
  156. Translator::LocationsType locations = Translator::DefaultLocations;
  157. ConversionData cd;
  158. Translator tr;
  159. for (int i = 1; i < args.size(); ++i) {
  160. if (args[i].startsWith(QLatin1String("--")))
  161. args[i].remove(0, 1);
  162. if (args[i] == QLatin1String("-o")
  163. || args[i] == QLatin1String("-output-file")) {
  164. if (++i >= args.size())
  165. return usage(args);
  166. outFileName = args[i];
  167. } else if (args[i] == QLatin1String("-of")
  168. || args[i] == QLatin1String("-output-format")) {
  169. if (++i >= args.size())
  170. return usage(args);
  171. outFormat = args[i];
  172. } else if (args[i] == QLatin1String("-i")
  173. || args[i] == QLatin1String("-input-file")) {
  174. if (++i >= args.size())
  175. return usage(args);
  176. File file;
  177. file.name = args[i];
  178. file.format = inFormat;
  179. inFiles.append(file);
  180. } else if (args[i] == QLatin1String("-if")
  181. || args[i] == QLatin1String("-input-format")) {
  182. if (++i >= args.size())
  183. return usage(args);
  184. inFormat = args[i];
  185. } else if (args[i] == QLatin1String("-input-codec")) {
  186. if (++i >= args.size())
  187. return usage(args);
  188. cd.m_codecForSource = args[i].toLatin1();
  189. } else if (args[i] == QLatin1String("-output-codec")) {
  190. if (++i >= args.size())
  191. return usage(args);
  192. cd.m_outputCodec = args[i].toLatin1();
  193. } else if (args[i] == QLatin1String("-drop-tag")) {
  194. if (++i >= args.size())
  195. return usage(args);
  196. cd.m_dropTags.append(args[i]);
  197. } else if (args[i] == QLatin1String("-drop-translations")) {
  198. dropTranslations = true;
  199. } else if (args[i] == QLatin1String("-target-language")) {
  200. if (++i >= args.size())
  201. return usage(args);
  202. targetLanguage = args[i];
  203. } else if (args[i] == QLatin1String("-source-language")) {
  204. if (++i >= args.size())
  205. return usage(args);
  206. sourceLanguage = args[i];
  207. } else if (args[i].startsWith(QLatin1String("-h"))) {
  208. usage(args);
  209. return 0;
  210. } else if (args[i] == QLatin1String("-no-obsolete")) {
  211. noObsolete = true;
  212. } else if (args[i] == QLatin1String("-no-finished")) {
  213. noFinished = true;
  214. } else if (args[i] == QLatin1String("-sort-contexts")) {
  215. cd.m_sortContexts = true;
  216. } else if (args[i] == QLatin1String("-locations")) {
  217. if (++i >= args.size())
  218. return usage(args);
  219. if (args[i] == QLatin1String("none"))
  220. locations = Translator::NoLocations;
  221. else if (args[i] == QLatin1String("relative"))
  222. locations = Translator::RelativeLocations;
  223. else if (args[i] == QLatin1String("absolute"))
  224. locations = Translator::AbsoluteLocations;
  225. else
  226. return usage(args);
  227. } else if (args[i] == QLatin1String("-no-ui-lines")) {
  228. noUiLines = true;
  229. } else if (args[i] == QLatin1String("-verbose")) {
  230. verbose = true;
  231. } else if (args[i].startsWith(QLatin1Char('-'))) {
  232. return usage(args);
  233. } else {
  234. File file;
  235. file.name = args[i];
  236. file.format = inFormat;
  237. inFiles.append(file);
  238. }
  239. }
  240. if (inFiles.isEmpty())
  241. return usage(args);
  242. tr.setLanguageCode(Translator::guessLanguageCodeFromFileName(inFiles[0].name));
  243. if (!tr.load(inFiles[0].name, cd, inFiles[0].format)) {
  244. std::cerr << qPrintable(cd.error());
  245. return 2;
  246. }
  247. tr.reportDuplicates(tr.resolveDuplicates(), inFiles[0].name, verbose);
  248. for (int i = 1; i < inFiles.size(); ++i) {
  249. Translator tr2;
  250. if (!tr2.load(inFiles[i].name, cd, inFiles[i].format)) {
  251. std::cerr << qPrintable(cd.error());
  252. return 2;
  253. }
  254. tr2.reportDuplicates(tr2.resolveDuplicates(), inFiles[i].name, verbose);
  255. for (int j = 0; j < tr2.messageCount(); ++j)
  256. tr.replaceSorted(tr2.message(j));
  257. }
  258. if (!targetLanguage.isEmpty())
  259. tr.setLanguageCode(targetLanguage);
  260. if (!sourceLanguage.isEmpty())
  261. tr.setSourceLanguageCode(sourceLanguage);
  262. if (noObsolete)
  263. tr.stripObsoleteMessages();
  264. if (noFinished)
  265. tr.stripFinishedMessages();
  266. if (dropTranslations)
  267. tr.dropTranslations();
  268. if (noUiLines)
  269. tr.dropUiLines();
  270. if (locations != Translator::DefaultLocations)
  271. tr.setLocationsType(locations);
  272. tr.normalizeTranslations(cd);
  273. if (!cd.errors().isEmpty()) {
  274. std::cerr << qPrintable(cd.error());
  275. cd.clearErrors();
  276. }
  277. if (!tr.save(outFileName, cd, outFormat)) {
  278. std::cerr << qPrintable(cd.error());
  279. return 3;
  280. }
  281. return 0;
  282. }