Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "translator.h"
5
6#include <QtCore/QCoreApplication>
7#include <QtCore/QDebug>
8#include <QtCore/QString>
9#include <QtCore/QStringList>
10#include <QtCore/QTranslator>
11#include <QtCore/QLibraryInfo>
12
13#include <iostream>
14
16
17static int usage(const QStringList &args)
18{
19 Q_UNUSED(args);
20
21 QString loaders;
22 QString line(QLatin1String(" %1 - %2\n"));
23 for (const Translator::FileFormat &format : std::as_const(Translator::registeredFileFormats()))
24 loaders += line.arg(format.extension, -5).arg(format.description());
25
26 std::cout << qPrintable(QStringLiteral("\nUsage:\n"
27 " lconvert [options] <infile> [<infile>...]\n\n"
28 "lconvert is part of Qt's Linguist tool chain. It can be used as a\n"
29 "stand-alone tool to convert and filter translation data files.\n"
30 "The following file formats are supported:\n\n%1\n"
31 "If multiple input files are specified, they are merged with\n"
32 "translations from later files taking precedence.\n\n"
33 "Options:\n"
34 " -h\n"
35 " -help Display this information and exit.\n\n"
36 " -i <infile>\n"
37 " -input-file <infile>\n"
38 " Specify input file. Use if <infile> might start with a dash.\n"
39 " This option can be used several times to merge inputs.\n"
40 " May be '-' (standard input) for use in a pipe.\n\n"
41 " -o <outfile>\n"
42 " -output-file <outfile>\n"
43 " Specify output file. Default is '-' (standard output).\n\n"
44 " -if <informat>\n"
45 " -input-format <format>\n"
46 " Specify input format for subsequent <infile>s.\n"
47 " The format is auto-detected from the file name and defaults to 'ts'.\n\n"
48 " -of <outformat>\n"
49 " -output-format <outformat>\n"
50 " Specify output format. See -if.\n\n"
51 " -drop-tags <regexp>\n"
52 " Drop named extra tags when writing TS or XLIFF files.\n"
53 " May be specified repeatedly.\n\n"
54 " -drop-translations\n"
55 " Drop existing translations and reset the status to 'unfinished'.\n"
56 " Note: this implies --no-obsolete.\n\n"
57 " -source-language <language>[_<region>]\n"
58 " Specify/override the language of the source strings. Defaults to\n"
59 " POSIX if not specified and the file does not name it yet.\n\n"
60 " -target-language <language>[_<region>]\n"
61 " Specify/override the language of the translation.\n"
62 " The target language is guessed from the file name if this option\n"
63 " is not specified and the file contents name no language yet.\n\n"
64 " -no-obsolete\n"
65 " Drop obsolete messages.\n\n"
66 " -no-finished\n"
67 " Drop finished messages.\n\n"
68 " -no-untranslated\n"
69 " Drop untranslated messages.\n\n"
70 " -sort-contexts\n"
71 " Sort contexts in output TS file alphabetically.\n\n"
72 " -locations {absolute|relative|none}\n"
73 " Override how source code references are saved in TS files.\n"
74 " Default is absolute.\n\n"
75 " -no-ui-lines\n"
76 " Drop line numbers from references to UI files.\n\n"
77 " -pluralonly\n"
78 " Drop non-plural form messages.\n\n"
79 " -verbose\n"
80 " be a bit more verbose\n\n"
81 "Long options can be specified with only one leading dash, too.\n\n"
82 "Return value:\n"
83 " 0 on success\n"
84 " 1 on command line parse failures\n"
85 " 2 on read failures\n"
86 " 3 on write failures\n").arg(loaders));
87 return 1;
88}
89
95
96int main(int argc, char *argv[])
97{
98 QCoreApplication app(argc, argv);
99#ifndef QT_BOOTSTRAPPED
100#ifndef Q_OS_WIN32
101 QTranslator translator;
102 QTranslator qtTranslator;
103 QString sysLocale = QLocale::system().name();
104 QString resourceDir = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
105 if (translator.load(QLatin1String("linguist_") + sysLocale, resourceDir)
106 && qtTranslator.load(QLatin1String("qt_") + sysLocale, resourceDir)) {
107 app.installTranslator(&translator);
108 app.installTranslator(&qtTranslator);
109 }
110#endif // Q_OS_WIN32
111#endif
112
113 QStringList args = app.arguments();
114 QList<File> inFiles;
115 QString inFormat(QLatin1String("auto"));
116 QString outFileName;
117 QString outFormat(QLatin1String("auto"));
118 QString targetLanguage;
119 QString sourceLanguage;
120 bool dropTranslations = false;
121 bool noObsolete = false;
122 bool noFinished = false;
123 bool noUntranslated = false;
124 bool verbose = false;
125 bool noUiLines = false;
126 bool pluralOnly = false;
128
130 Translator tr;
131
132 for (int i = 1; i < args.size(); ++i) {
133 if (args[i].startsWith(QLatin1String("--")))
134 args[i].remove(0, 1);
135 if (args[i] == QLatin1String("-o")
136 || args[i] == QLatin1String("-output-file")) {
137 if (++i >= args.size())
138 return usage(args);
139 outFileName = args[i];
140 } else if (args[i] == QLatin1String("-of")
141 || args[i] == QLatin1String("-output-format")) {
142 if (++i >= args.size())
143 return usage(args);
144 outFormat = args[i];
145 } else if (args[i] == QLatin1String("-i")
146 || args[i] == QLatin1String("-input-file")) {
147 if (++i >= args.size())
148 return usage(args);
149 File file;
150 file.name = args[i];
151 file.format = inFormat;
152 inFiles.append(file);
153 } else if (args[i] == QLatin1String("-if")
154 || args[i] == QLatin1String("-input-format")) {
155 if (++i >= args.size())
156 return usage(args);
157 inFormat = args[i];
158 } else if (args[i] == QLatin1String("-drop-tag") || args[i] == QLatin1String("-drop-tags")) {
159 if (++i >= args.size())
160 return usage(args);
161 cd.m_dropTags.append(args[i]);
162 } else if (args[i] == QLatin1String("-drop-translations")) {
163 dropTranslations = true;
164 } else if (args[i] == QLatin1String("-target-language")) {
165 if (++i >= args.size())
166 return usage(args);
167 targetLanguage = args[i];
168 } else if (args[i] == QLatin1String("-source-language")) {
169 if (++i >= args.size())
170 return usage(args);
171 sourceLanguage = args[i];
172 } else if (args[i].startsWith(QLatin1String("-h"))) {
173 usage(args);
174 return 0;
175 } else if (args[i] == QLatin1String("-no-obsolete")) {
176 noObsolete = true;
177 } else if (args[i] == QLatin1String("-no-finished")) {
178 noFinished = true;
179 } else if (args[i] == QLatin1String("-no-untranslated")) {
180 noUntranslated = true;
181 } else if (args[i] == QLatin1String("-sort-contexts")) {
182 cd.m_sortContexts = true;
183 } else if (args[i] == QLatin1String("-locations")) {
184 if (++i >= args.size())
185 return usage(args);
186 if (args[i] == QLatin1String("none"))
187 locations = Translator::NoLocations;
188 else if (args[i] == QLatin1String("relative"))
190 else if (args[i] == QLatin1String("absolute"))
192 else
193 return usage(args);
194 } else if (args[i] == QLatin1String("-no-ui-lines")) {
195 noUiLines = true;
196 } else if (args[i] == QLatin1String("-pluralonly")) {
197 pluralOnly = true;
198 } else if (args[i] == QLatin1String("-verbose")) {
199 verbose = true;
200 } else if (args[i].startsWith(QLatin1Char('-'))) {
201 return usage(args);
202 } else {
203 File file;
204 file.name = args[i];
205 file.format = inFormat;
206 inFiles.append(file);
207 }
208 }
209
210 if (inFiles.isEmpty())
211 return usage(args);
212
213 tr.setLanguageCode(Translator::guessLanguageCodeFromFileName(inFiles[0].name));
214
215 if (!tr.load(inFiles[0].name, cd, inFiles[0].format)) {
216 std::cerr << qPrintable(cd.error());
217 return 2;
218 }
219 tr.reportDuplicates(tr.resolveDuplicates(), inFiles[0].name, verbose);
220
221 for (int i = 1; i < inFiles.size(); ++i) {
222 Translator tr2;
223 if (!tr2.load(inFiles[i].name, cd, inFiles[i].format)) {
224 std::cerr << qPrintable(cd.error());
225 return 2;
226 }
227 tr2.reportDuplicates(tr2.resolveDuplicates(), inFiles[i].name, verbose);
228 for (int j = 0; j < tr2.messageCount(); ++j)
230 }
231
232 if (!targetLanguage.isEmpty())
233 tr.setLanguageCode(targetLanguage);
234 if (!sourceLanguage.isEmpty())
235 tr.setSourceLanguageCode(sourceLanguage);
236 if (noObsolete)
238 if (noFinished)
240 if (noUntranslated)
242 if (dropTranslations)
244 if (noUiLines)
246 if (pluralOnly)
248 if (locations != Translator::DefaultLocations)
249 tr.setLocationsType(locations);
250
252 if (!cd.errors().isEmpty()) {
253 std::cerr << qPrintable(cd.error());
255 }
256 if (!tr.save(outFileName, cd, outFormat)) {
257 std::cerr << qPrintable(cd.error());
258 return 3;
259 }
260 return 0;
261}
int main(int argc, char *argv[])
[2]
Definition buffer.cpp:77
void clearErrors()
Definition translator.h:48
bool m_sortContexts
Definition translator.h:67
void stripObsoleteMessages()
void dropTranslations()
void dropUiLines()
void stripUntranslatedMessages()
void stripFinishedMessages()
void replaceSorted(const TranslatorMessage &msg)
void stripNonPluralForms()
void setLocationsType(LocationsType lt)
Definition translator.h:132
@ RelativeLocations
Definition translator.h:131
@ AbsoluteLocations
Definition translator.h:131
int messageCount() const
Definition translator.h:146
Duplicates resolveDuplicates()
void normalizeTranslations(ConversionData &cd)
TranslatorMessage & message(int i)
Definition translator.h:147
static QT_USE_NAMESPACE int usage(const QStringList &args)
Definition main.cpp:17
Definition main.cpp:91
QString format
Definition main.cpp:93
QString name
Definition main.cpp:92