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"
6
7#include <linguistproject/profileutils.h>
8#include <linguistproject/projectdescriptionreader.h>
9#include <linguistproject/projsongenerator.h>
10
11#ifndef QT_BOOTSTRAPPED
12#include <QtCore/QCoreApplication>
13#include <QtCore/QTranslator>
14#endif
15#include <QtCore/QDebug>
16#include <QtCore/QDir>
17#include <QtCore/QFile>
18#include <QtCore/QFileInfo>
19#include <QtCore/QString>
20#include <QtCore/QStringList>
21#include <QtCore/QTextStream>
22#include <QtCore/QLibraryInfo>
23
24QT_USE_NAMESPACE
25
26using namespace Qt::StringLiterals;
27
28static void printUsage()
29{
30 printOut(uR"(Usage:
31 lrelease [options] -project project-file
32 lrelease [options] ts-files [-qm qm-file]
34lrelease is part of Qt's Linguist tool chain. It can be used as a
35stand-alone tool to convert XML-based translations files in the TS
36format into the 'compiled' QM format used by QTranslator objects.
37
38Passing .pro files to lrelease is deprecated.
39Please use the lrelease-pro tool instead, or use qmake's lrelease.prf
40feature.
41
42Options:
43 -help Display this information and exit
44 -idbased
45 Deprecated. The flag is not required anymore and will be removed
46 in a future version. It was used to enable ID based translation.
47 -compress
48 Compress the QM files
49 -nounfinished
50 Do not include unfinished translations
51 -fail-on-unfinished
52 Generate an error if unfinished translations are found
53 -fail-on-invalid
54 Fail if translations failing the following checks are found:
55 validity check of accelerators
56 validity check of surrounding whitespaces
57 validity check of ending punctuation
58 validity check of place markers
59 To get more details refer to Qt Linguist help
60 -removeidentical
61 If the translated text is the same as
62 the source text, do not include the message
63 -markuntranslated <prefix>
64 If a message has no real translation, use the source text
65 prefixed with the given string instead
66 -project <filename>
67 Name of a file containing the project's description in JSON format.
68 Such a file may be generated from a .pro file using lupdate-pro -dump-json.
69 -silent
70 Do not explain what is being done
71 -verbose
72 Explain what is being done (default)
73 -version
74 Display the version of lrelease and exit
75)"_s);
76}
77
78int main(int argc, char **argv)
79{
80 QCoreApplication app(argc, argv);
81
83 cd.m_verbose = true; // the default is true starting with Qt 4.2
84 ParamFlags params;
85 Translator tor;
86 QStringList inputFiles;
87 QString outputFile;
88 QString projectDescriptionFile;
89
90 for (int i = 1; i < argc; ++i) {
91 const char *arg = argv[i];
92 if (!strcmp(arg, "-compress")) {
94 continue;
95 } else if (!strcmp(arg, "-idbased")) {
96 printOut("The flag -idbased is deprecated and not required anymore."
97 "It will be removed in a future version"_L1);
98 continue;
99 } else if (!strcmp(arg, "-nocompress")) {
101 continue;
102 } else if (!strcmp(arg, "-removeidentical")) {
103 params.removeIdentical = true;
104 continue;
105 } else if (!strcmp(arg, "-nounfinished")) {
106 cd.m_ignoreUnfinished = true;
107 continue;
108 } else if (!strcmp(arg, "-fail-on-unfinished")) {
109 params.failOnUnfinished = true;
110 continue;
111 } else if (!strcmp(arg, "-fail-on-invalid")) {
112 params.failOnInvalid = true;
113 continue;
114 } else if (!strcmp(arg, "-markuntranslated")) {
115 if (i == argc - 1) {
117 return 1;
118 }
119 cd.m_unTrPrefix = QString::fromLocal8Bit(argv[++i]);
120 } else if (!strcmp(arg, "-project")) {
121 if (i == argc - 1) {
122 printErr("The option -project requires a parameter.\n"_L1);
123 return 1;
124 }
125 if (!projectDescriptionFile.isEmpty()) {
126 printErr("The option -project must appear only once.\n"_L1);
127 return 1;
128 }
129 projectDescriptionFile = QString::fromLocal8Bit(argv[++i]);
130 } else if (!strcmp(arg, "-silent")) {
131 cd.m_verbose = false;
132 continue;
133 } else if (!strcmp(arg, "-verbose")) {
134 cd.m_verbose = true;
135 continue;
136 } else if (!strcmp(arg, "-version")) {
137 printOut("lrelease version %1\n"_L1.arg(QLatin1StringView(QT_VERSION_STR)));
138 return 0;
139 } else if (!strcmp(arg, "-qm")) {
140 if (i == argc - 1) {
142 return 1;
143 }
144 outputFile = QString::fromLocal8Bit(argv[++i]);
145 } else if (!strcmp(arg, "-help")) {
147 return 0;
148 } else if (arg[0] == '-') {
150 return 1;
151 } else {
152 inputFiles << QString::fromLocal8Bit(arg);
153 }
154 }
155
156 if (inputFiles.isEmpty() && projectDescriptionFile.isEmpty()) {
158 return 1;
159 }
160
161 QString errorString;
162 Projects projectDescription;
163 const QStringList proFiles = extractProFiles(&inputFiles);
164
165 if (!proFiles.isEmpty()) {
166 QStringList translationsVariables = { u"TRANSLATIONS"_s, u"EXTRA_TRANSLATIONS"_s };
167 QHash<QString, QString> outDirMap;
168 QString outDir = QDir::currentPath();
169 QStringList cleanProFiles;
170 cleanProFiles.reserve(proFiles.size());
171 for (const QString &proFile : std::as_const(proFiles)) {
172 const QString cleanFile = QDir::cleanPath(QFileInfo(proFile).absoluteFilePath());
173 cleanProFiles << cleanFile;
174 outDirMap[cleanFile] = outDir;
175 }
176
177 projectDescription = generateProjects(cleanProFiles, translationsVariables, outDirMap, 0,
178 true, &errorString);
179 if (!errorString.isEmpty()) {
180 printErr("lrelease error: %1\n"_L1.arg(errorString));
181 return 1;
182 }
183 if (projectDescription.empty()) {
184 printErr(u"lrelease error: No projects found in .pro files\n"_s);
185 return 1;
186 }
187 inputFiles = translationsFromProjects(projectDescription);
188 } else if (!projectDescriptionFile.isEmpty()) {
189 if (!inputFiles.isEmpty()) {
190 printErr(QLatin1String(
191 "lrelease error: Do not specify TS files if -project is given.\n"));
192 return 1;
193 }
194 projectDescription = projectDescriptionFromFile(projectDescriptionFile, &errorString);
195 if (!errorString.isEmpty()) {
196 printErr("lrelease error: %1\n"_L1.arg(errorString));
197 return 1;
198 }
199 inputFiles = translationsFromProjects(projectDescription);
200 }
201
202 for (const QString &inputFile : std::as_const(inputFiles)) {
203 if (outputFile.isEmpty()) {
204 if (!releaseTsFile(inputFile, cd, params))
205 return 1;
206 } else {
207 if (!loadTsFile(tor, inputFile))
208 return 1;
209 }
210 }
211
212 if (!outputFile.isEmpty())
213 return releaseTranslator(tor, outputFile, cd, params) ? 0 : 1;
214
215 return 0;
216}
TranslatorSaveMode m_saveMode
Definition translator.h:61
bool m_ignoreUnfinished
Definition translator.h:57
std::vector< Project > Projects
static void printUsage()
Definition main.cpp:24
int main(int argc, char *argv[])
[ctor_close]
bool failOnInvalid
bool failOnUnfinished
bool removeIdentical
@ SaveStripped
@ SaveEverything