1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 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 |
|
---|
44 | #include <QtCore/QCoreApplication>
|
---|
45 | #include <QtCore/QDebug>
|
---|
46 | #include <QtCore/QString>
|
---|
47 | #include <QtCore/QStringList>
|
---|
48 |
|
---|
49 | #include <iostream>
|
---|
50 |
|
---|
51 | static int usage(const QStringList &args)
|
---|
52 | {
|
---|
53 | Q_UNUSED(args);
|
---|
54 |
|
---|
55 | QString loaders;
|
---|
56 | QString line(QLatin1String(" %1 - %2\n"));
|
---|
57 | foreach (Translator::FileFormat format, Translator::registeredFileFormats())
|
---|
58 | loaders += line.arg(format.extension, -5).arg(format.description);
|
---|
59 |
|
---|
60 | std::cerr << qPrintable(QString(QLatin1String("\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 |
|
---|
126 | struct File
|
---|
127 | {
|
---|
128 | QString name;
|
---|
129 | QString format;
|
---|
130 | };
|
---|
131 |
|
---|
132 | int main(int argc, char *argv[])
|
---|
133 | {
|
---|
134 | QCoreApplication app(argc, argv);
|
---|
135 | QStringList args = app.arguments();
|
---|
136 |
|
---|
137 | QList<File> inFiles;
|
---|
138 | QString inFormat(QLatin1String("auto"));
|
---|
139 | QString outFileName;
|
---|
140 | QString outFormat(QLatin1String("auto"));
|
---|
141 | QString targetLanguage;
|
---|
142 | QString sourceLanguage;
|
---|
143 | bool dropTranslations = false;
|
---|
144 | bool noObsolete = false;
|
---|
145 | bool noFinished = false;
|
---|
146 | bool verbose = false;
|
---|
147 | bool noUiLines = false;
|
---|
148 | Translator::LocationsType locations = Translator::DefaultLocations;
|
---|
149 |
|
---|
150 | ConversionData cd;
|
---|
151 | Translator tr;
|
---|
152 |
|
---|
153 | for (int i = 1; i < args.size(); ++i) {
|
---|
154 | if (args[i].startsWith(QLatin1String("--")))
|
---|
155 | args[i].remove(0, 1);
|
---|
156 | if (args[i] == QLatin1String("-o")
|
---|
157 | || args[i] == QLatin1String("-output-file")) {
|
---|
158 | if (++i >= args.size())
|
---|
159 | return usage(args);
|
---|
160 | outFileName = args[i];
|
---|
161 | } else if (args[i] == QLatin1String("-of")
|
---|
162 | || args[i] == QLatin1String("-output-format")) {
|
---|
163 | if (++i >= args.size())
|
---|
164 | return usage(args);
|
---|
165 | outFormat = args[i];
|
---|
166 | } else if (args[i] == QLatin1String("-i")
|
---|
167 | || args[i] == QLatin1String("-input-file")) {
|
---|
168 | if (++i >= args.size())
|
---|
169 | return usage(args);
|
---|
170 | File file;
|
---|
171 | file.name = args[i];
|
---|
172 | file.format = inFormat;
|
---|
173 | inFiles.append(file);
|
---|
174 | } else if (args[i] == QLatin1String("-if")
|
---|
175 | || args[i] == QLatin1String("-input-format")) {
|
---|
176 | if (++i >= args.size())
|
---|
177 | return usage(args);
|
---|
178 | inFormat = args[i];
|
---|
179 | } else if (args[i] == QLatin1String("-input-codec")) {
|
---|
180 | if (++i >= args.size())
|
---|
181 | return usage(args);
|
---|
182 | cd.m_codecForSource = args[i].toLatin1();
|
---|
183 | } else if (args[i] == QLatin1String("-output-codec")) {
|
---|
184 | if (++i >= args.size())
|
---|
185 | return usage(args);
|
---|
186 | cd.m_outputCodec = args[i].toLatin1();
|
---|
187 | } else if (args[i] == QLatin1String("-drop-tag")) {
|
---|
188 | if (++i >= args.size())
|
---|
189 | return usage(args);
|
---|
190 | cd.m_dropTags.append(args[i]);
|
---|
191 | } else if (args[i] == QLatin1String("-drop-translations")) {
|
---|
192 | dropTranslations = true;
|
---|
193 | } else if (args[i] == QLatin1String("-target-language")) {
|
---|
194 | if (++i >= args.size())
|
---|
195 | return usage(args);
|
---|
196 | targetLanguage = args[i];
|
---|
197 | } else if (args[i] == QLatin1String("-source-language")) {
|
---|
198 | if (++i >= args.size())
|
---|
199 | return usage(args);
|
---|
200 | sourceLanguage = args[i];
|
---|
201 | } else if (args[i].startsWith(QLatin1String("-h"))) {
|
---|
202 | usage(args);
|
---|
203 | return 0;
|
---|
204 | } else if (args[i] == QLatin1String("-no-obsolete")) {
|
---|
205 | noObsolete = true;
|
---|
206 | } else if (args[i] == QLatin1String("-no-finished")) {
|
---|
207 | noFinished = true;
|
---|
208 | } else if (args[i] == QLatin1String("-sort-contexts")) {
|
---|
209 | cd.m_sortContexts = true;
|
---|
210 | } else if (args[i] == QLatin1String("-locations")) {
|
---|
211 | if (++i >= args.size())
|
---|
212 | return usage(args);
|
---|
213 | if (args[i] == QLatin1String("none"))
|
---|
214 | locations = Translator::NoLocations;
|
---|
215 | else if (args[i] == QLatin1String("relative"))
|
---|
216 | locations = Translator::RelativeLocations;
|
---|
217 | else if (args[i] == QLatin1String("absolute"))
|
---|
218 | locations = Translator::AbsoluteLocations;
|
---|
219 | else
|
---|
220 | return usage(args);
|
---|
221 | } else if (args[i] == QLatin1String("-no-ui-lines")) {
|
---|
222 | noUiLines = true;
|
---|
223 | } else if (args[i] == QLatin1String("-verbose")) {
|
---|
224 | verbose = true;
|
---|
225 | } else if (args[i].startsWith(QLatin1Char('-'))) {
|
---|
226 | return usage(args);
|
---|
227 | } else {
|
---|
228 | File file;
|
---|
229 | file.name = args[i];
|
---|
230 | file.format = inFormat;
|
---|
231 | inFiles.append(file);
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (inFiles.isEmpty())
|
---|
236 | return usage(args);
|
---|
237 |
|
---|
238 | tr.setLanguageCode(Translator::guessLanguageCodeFromFileName(inFiles[0].name));
|
---|
239 |
|
---|
240 | if (!tr.load(inFiles[0].name, cd, inFiles[0].format)) {
|
---|
241 | qWarning() << qPrintable(cd.error());
|
---|
242 | return 2;
|
---|
243 | }
|
---|
244 | tr.reportDuplicates(tr.resolveDuplicates(), inFiles[0].name, verbose);
|
---|
245 |
|
---|
246 | for (int i = 1; i < inFiles.size(); ++i) {
|
---|
247 | Translator tr2;
|
---|
248 | if (!tr2.load(inFiles[i].name, cd, inFiles[i].format)) {
|
---|
249 | qWarning() << qPrintable(cd.error());
|
---|
250 | return 2;
|
---|
251 | }
|
---|
252 | tr2.reportDuplicates(tr2.resolveDuplicates(), inFiles[i].name, verbose);
|
---|
253 | for (int j = 0; j < tr2.messageCount(); ++j)
|
---|
254 | tr.replaceSorted(tr2.message(j));
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (!targetLanguage.isEmpty())
|
---|
258 | tr.setLanguageCode(targetLanguage);
|
---|
259 | if (!sourceLanguage.isEmpty())
|
---|
260 | tr.setSourceLanguageCode(sourceLanguage);
|
---|
261 | if (noObsolete)
|
---|
262 | tr.stripObsoleteMessages();
|
---|
263 | if (noFinished)
|
---|
264 | tr.stripFinishedMessages();
|
---|
265 | if (dropTranslations)
|
---|
266 | tr.dropTranslations();
|
---|
267 | if (noUiLines)
|
---|
268 | tr.dropUiLines();
|
---|
269 | if (locations != Translator::DefaultLocations)
|
---|
270 | tr.setLocationsType(locations);
|
---|
271 |
|
---|
272 | tr.normalizeTranslations(cd);
|
---|
273 | if (!cd.errors().isEmpty()) {
|
---|
274 | qWarning("%s", qPrintable(cd.error()));
|
---|
275 | cd.clearErrors();
|
---|
276 | }
|
---|
277 | if (!tr.save(outFileName, cd, outFormat)) {
|
---|
278 | qWarning("%s", qPrintable(cd.error()));
|
---|
279 | return 3;
|
---|
280 | }
|
---|
281 | return 0;
|
---|
282 | }
|
---|