| 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 |  | 
|---|
| 44 | #include <QtCore/QCoreApplication> | 
|---|
| 45 | #include <QtCore/QDebug> | 
|---|
| 46 | #include <QtCore/QString> | 
|---|
| 47 | #include <QtCore/QStringList> | 
|---|
| 48 |  | 
|---|
| 49 | static int usage(const QStringList &args) | 
|---|
| 50 | { | 
|---|
| 51 | Q_UNUSED(args); | 
|---|
| 52 |  | 
|---|
| 53 | QString loaders; | 
|---|
| 54 | QString savers; | 
|---|
| 55 | QString line = QString(QLatin1String("    %1 - %2\n")); | 
|---|
| 56 | foreach (Translator::FileFormat format, Translator::registeredFileFormats()) { | 
|---|
| 57 | loaders += line.arg(format.extension, -5).arg(format.description); | 
|---|
| 58 | if (format.fileType != Translator::FileFormat::SourceCode) | 
|---|
| 59 | savers += line.arg(format.extension, -5).arg(format.description); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | qWarning("%s", qPrintable(QString(QLatin1String("\nUsage:\n" | 
|---|
| 63 | "    lconvert [options] <infile> [<infile>...]\n\n" | 
|---|
| 64 | "lconvert is part of Qt's Linguist tool chain. It can be used as a\n" | 
|---|
| 65 | "stand-alone tool to convert translation data files from one of the\n" | 
|---|
| 66 | "following input formats\n\n%1\n" | 
|---|
| 67 | "to one of the following output formats\n\n%2\n" | 
|---|
| 68 | "If multiple input files are specified the translations are merged with\n" | 
|---|
| 69 | "translations from later files taking precedence.\n\n" | 
|---|
| 70 | "Options:\n" | 
|---|
| 71 | "    -h\n" | 
|---|
| 72 | "    --help  Display this information and exit.\n\n" | 
|---|
| 73 | "    -i <infile>\n" | 
|---|
| 74 | "    --input-file <infile>\n" | 
|---|
| 75 | "           Specify input file. Use if <infile> might start with a dash.\n" | 
|---|
| 76 | "           This option can be used several times to merge inputs.\n" | 
|---|
| 77 | "           May be '-' (standard input) for use in a pipe.\n\n" | 
|---|
| 78 | "    -o <outfile>\n" | 
|---|
| 79 | "    --output-file <outfile>\n" | 
|---|
| 80 | "           Specify output file. Default is '-' (standard output).\n\n" | 
|---|
| 81 | "    -if <informat>\n" | 
|---|
| 82 | "    --input-format <format>\n" | 
|---|
| 83 | "           Specify input format for subsequent <infile>s.\n" | 
|---|
| 84 | "           The format is auto-detected from the file name and defaults to 'ts'.\n\n" | 
|---|
| 85 | "    -of <outformat>\n" | 
|---|
| 86 | "    --output-format <outformat>\n" | 
|---|
| 87 | "           Specify output format. See -if.\n\n" | 
|---|
| 88 | "    --drop-tags <regexp>\n" | 
|---|
| 89 | "           Drop named extra tags when writing 'ts' or 'xlf' files.\n" | 
|---|
| 90 | "           May be specified repeatedly.\n\n" | 
|---|
| 91 | "    --drop-translations\n" | 
|---|
| 92 | "           Drop existing translations and reset the status to 'unfinished'.\n" | 
|---|
| 93 | "           Note: this implies --no-obsolete.\n\n" | 
|---|
| 94 | "    --source-language <language>[_<region>]\n" | 
|---|
| 95 | "           Specify/override the language of the source strings. Defaults to\n" | 
|---|
| 96 | "           POSIX if not specified and the file does not name it yet.\n" | 
|---|
| 97 | "    --target-language <language>[_<region>]\n" | 
|---|
| 98 | "           Specify/override the language of the translation.\n" | 
|---|
| 99 | "           The target language is guessed from the file name if this option\n" | 
|---|
| 100 | "           is not specified and the file contents name no language yet.\n\n" | 
|---|
| 101 | "    --no-obsolete\n" | 
|---|
| 102 | "           Drop obsolete messages.\n\n" | 
|---|
| 103 | "    --no-finished\n" | 
|---|
| 104 | "           Drop finished messages.\n\n" | 
|---|
| 105 | "    --verbose\n" | 
|---|
| 106 | "           be a bit more verbose\n\n" | 
|---|
| 107 | "Long options can be specified with only one leading dash, too.\n\n" | 
|---|
| 108 | "Return value:\n" | 
|---|
| 109 | "    0 on success\n" | 
|---|
| 110 | "    1 on command line parse failures\n" | 
|---|
| 111 | "    2 on read failures\n" | 
|---|
| 112 | "    3 on write failures\n")).arg(loaders).arg(savers))); | 
|---|
| 113 | return 1; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | struct File | 
|---|
| 117 | { | 
|---|
| 118 | QString name; | 
|---|
| 119 | QString format; | 
|---|
| 120 | }; | 
|---|
| 121 |  | 
|---|
| 122 | int main(int argc, char *argv[]) | 
|---|
| 123 | { | 
|---|
| 124 | QCoreApplication app(argc, argv); | 
|---|
| 125 | QStringList args = app.arguments(); | 
|---|
| 126 |  | 
|---|
| 127 | QList<File> inFiles; | 
|---|
| 128 | QString inFormat(QLatin1String("auto")); | 
|---|
| 129 | QString outFileName; | 
|---|
| 130 | QString outFormat(QLatin1String("auto")); | 
|---|
| 131 | QString targetLanguage; | 
|---|
| 132 | QString sourceLanguage; | 
|---|
| 133 | bool dropTranslations = false; | 
|---|
| 134 | bool noObsolete = false; | 
|---|
| 135 | bool noFinished = false; | 
|---|
| 136 | bool verbose = false; | 
|---|
| 137 |  | 
|---|
| 138 | ConversionData cd; | 
|---|
| 139 | Translator tr; | 
|---|
| 140 |  | 
|---|
| 141 | for (int i = 1; i < args.size(); ++i) { | 
|---|
| 142 | if (args[i].startsWith(QLatin1String("--"))) | 
|---|
| 143 | args[i].remove(0, 1); | 
|---|
| 144 | if (args[i] == QLatin1String("-o") | 
|---|
| 145 | || args[i] == QLatin1String("-output-file")) { | 
|---|
| 146 | if (++i >= args.size()) | 
|---|
| 147 | return usage(args); | 
|---|
| 148 | outFileName = args[i]; | 
|---|
| 149 | } else if (args[i] == QLatin1String("-of") | 
|---|
| 150 | || args[i] == QLatin1String("-output-format")) { | 
|---|
| 151 | if (++i >= args.size()) | 
|---|
| 152 | return usage(args); | 
|---|
| 153 | outFormat = args[i]; | 
|---|
| 154 | } else if (args[i] == QLatin1String("-i") | 
|---|
| 155 | || args[i] == QLatin1String("-input-file")) { | 
|---|
| 156 | if (++i >= args.size()) | 
|---|
| 157 | return usage(args); | 
|---|
| 158 | File file; | 
|---|
| 159 | file.name = args[i]; | 
|---|
| 160 | file.format = inFormat; | 
|---|
| 161 | inFiles.append(file); | 
|---|
| 162 | } else if (args[i] == QLatin1String("-if") | 
|---|
| 163 | || args[i] == QLatin1String("-input-format")) { | 
|---|
| 164 | if (++i >= args.size()) | 
|---|
| 165 | return usage(args); | 
|---|
| 166 | inFormat = args[i]; | 
|---|
| 167 | } else if (args[i] == QLatin1String("-drop-tag")) { | 
|---|
| 168 | if (++i >= args.size()) | 
|---|
| 169 | return usage(args); | 
|---|
| 170 | cd.m_dropTags.append(args[i]); | 
|---|
| 171 | } else if (args[i] == QLatin1String("-drop-translations")) { | 
|---|
| 172 | dropTranslations = true; | 
|---|
| 173 | } else if (args[i] == QLatin1String("-target-language")) { | 
|---|
| 174 | if (++i >= args.size()) | 
|---|
| 175 | return usage(args); | 
|---|
| 176 | targetLanguage = args[i]; | 
|---|
| 177 | } else if (args[i] == QLatin1String("-source-language")) { | 
|---|
| 178 | if (++i >= args.size()) | 
|---|
| 179 | return usage(args); | 
|---|
| 180 | sourceLanguage = args[i]; | 
|---|
| 181 | } else if (args[i].startsWith(QLatin1String("-h"))) { | 
|---|
| 182 | usage(args); | 
|---|
| 183 | return 0; | 
|---|
| 184 | } else if (args[i] == QLatin1String("-no-obsolete")) { | 
|---|
| 185 | noObsolete = true; | 
|---|
| 186 | } else if (args[i] == QLatin1String("-no-finished")) { | 
|---|
| 187 | noFinished = true; | 
|---|
| 188 | } else if (args[i] == QLatin1String("-verbose")) { | 
|---|
| 189 | verbose = true; | 
|---|
| 190 | } else if (args[i].startsWith(QLatin1Char('-'))) { | 
|---|
| 191 | return usage(args); | 
|---|
| 192 | } else { | 
|---|
| 193 | File file; | 
|---|
| 194 | file.name = args[i]; | 
|---|
| 195 | file.format = inFormat; | 
|---|
| 196 | inFiles.append(file); | 
|---|
| 197 | } | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | if (inFiles.isEmpty()) | 
|---|
| 201 | return usage(args); | 
|---|
| 202 |  | 
|---|
| 203 | tr.setLanguageCode(Translator::guessLanguageCodeFromFileName(inFiles[0].name)); | 
|---|
| 204 | if (!targetLanguage.isEmpty()) | 
|---|
| 205 | tr.setLanguageCode(targetLanguage); | 
|---|
| 206 | if (!sourceLanguage.isEmpty()) | 
|---|
| 207 | tr.setSourceLanguageCode(sourceLanguage); | 
|---|
| 208 |  | 
|---|
| 209 | if (!tr.load(inFiles[0].name, cd, inFiles[0].format)) { | 
|---|
| 210 | qWarning() << qPrintable(cd.error()); | 
|---|
| 211 | return 2; | 
|---|
| 212 | } | 
|---|
| 213 | Translator::reportDuplicates(tr.resolveDuplicates(), inFiles[0].name, verbose); | 
|---|
| 214 |  | 
|---|
| 215 | for (int i = 1; i < inFiles.size(); ++i) { | 
|---|
| 216 | Translator tr2; | 
|---|
| 217 | if (!tr2.load(inFiles[i].name, cd, inFiles[i].format)) { | 
|---|
| 218 | qWarning() << qPrintable(cd.error()); | 
|---|
| 219 | return 2; | 
|---|
| 220 | } | 
|---|
| 221 | Translator::reportDuplicates(tr2.resolveDuplicates(), inFiles[i].name, verbose); | 
|---|
| 222 | for (int j = 0; j < tr2.messageCount(); ++j) | 
|---|
| 223 | tr.replaceSorted(tr2.message(j)); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | if (noObsolete) | 
|---|
| 227 | tr.stripObsoleteMessages(); | 
|---|
| 228 | if (noFinished) | 
|---|
| 229 | tr.stripFinishedMessages(); | 
|---|
| 230 | if (dropTranslations) | 
|---|
| 231 | tr.dropTranslations(); | 
|---|
| 232 |  | 
|---|
| 233 | if (!tr.save(outFileName, cd, outFormat)) { | 
|---|
| 234 | qWarning("%s", qPrintable(cd.error())); | 
|---|
| 235 | return 3; | 
|---|
| 236 | } | 
|---|
| 237 | return 0; | 
|---|
| 238 | } | 
|---|