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) 2018 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 <linguistproject/profileutils.h>
5#include <linguistproject/projsongenerator.h>
6#include <linguistproject/projectdescriptionreader.h>
7#include <releasehelper.h>
8#include <translator.h>
9
10#include <QtCore/qcoreapplication.h>
11#include <QtCore/qdebug.h>
12#include <QtCore/qdir.h>
13#include <QtCore/qfileinfo.h>
14#include <QtCore/qlibraryinfo.h>
15#include <QtCore/qtemporaryfile.h>
16#include <QtCore/qtranslator.h>
17
18#include <iostream>
19
20QT_USE_NAMESPACE
21
22using namespace Qt::StringLiterals;
23
24static void printUsage()
25{
26 printOut(uR"(
27Usage:
28 lrelease-pro [options] [project-file]...
29lrelease-pro is part of Qt's Linguist tool chain. It extracts project
30information from qmake projects and passes it to lrelease.
31All command line options that are not consumed by lrelease-pro are
32passed to lrelease.
33
34Options:
35 -help Display this information and exit
36 -dump-json <file>
37 Only generate the project description json file.
38 -fail-on-invalid
39 Fail if translations failing the following checks are found:
40 validity check of accelerators
41 validity check of surrounding whitespaces
42 validity check of ending punctuation
43 validity check of place markers
44 To get more details refer to Qt Linguist help
45 -silent
46 Do not explain what is being done
47 -version
48 Display the version of lrelease-pro and exit
49)"_s);
50}
51
52int main(int argc, char **argv)
53{
54 QCoreApplication app(argc, argv);
55#ifndef QT_BOOTSTRAPPED
56#ifndef Q_OS_WIN32
57 QTranslator translator;
58 QTranslator qtTranslator;
59 QString sysLocale = QLocale::system().name();
60 QString resourceDir = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
61 if (translator.load("linguist_"_L1 + sysLocale, resourceDir)
62 && qtTranslator.load("qt_"_L1 + sysLocale, resourceDir)) {
63 app.installTranslator(&translator);
64 app.installTranslator(&qtTranslator);
65 }
66#endif // Q_OS_WIN32
67#endif // QT_BOOTSTRAPPED
68
69 std::optional<QString> dumpJsonFile;
70 QStringList inputFiles;
71
72 ParamFlags params;
73 QString markUntranslated;
75
76 for (int i = 1; i < argc; ++i) {
77 QString arg = QString::fromLocal8Bit(argv[i]);
78 if (arg == "-silent"_L1) {
79 cd.m_verbose = false;
80 } else if (arg == "-verbose"_L1) {
81 cd.m_verbose = true;
82 } else if (arg == "-version"_L1) {
83 printOut(QStringLiteral("lrelease-pro version %1\n")
84 .arg(QLatin1String(QT_VERSION_STR)));
85 return 0;
86 } else if (arg == "-help"_L1) {
88 return 0;
89 } else if (arg == "-dump-json"_L1) {
90 ++i;
91 if (i == argc) {
92 printErr(u"The -dump-json option should be followed by a file name.\n"_s);
93 return 1;
94 }
95 dumpJsonFile.emplace(QFileInfo(QLatin1String(argv[i])).absoluteFilePath());
96 } else if (arg == "-removeidentical"_L1) {
97 params.removeIdentical = true;
98 } else if (arg == "-fail-on-unfinished"_L1) {
99 params.failOnUnfinished = true;
100 } else if (arg == "-fail-on-invalid"_L1) {
101 params.failOnInvalid = true;
102 } else if (arg == "-nounfinished"_L1) {
103 cd.m_ignoreUnfinished = true;
104 } else if (arg == "-markuntranslated"_L1) {
105 ++i;
106 if (i < argc)
107 markUntranslated = QString::fromLocal8Bit(argv[i]);
108 } else if (arg == "-idbased"_L1) {
109 printOut("The flag -idbased is deprecated and not required anymore."
110 "It will be removed in a future version\n"_L1);
111 } else if (!arg.startsWith(u'-')) {
112 inputFiles << arg;
113 }
114 }
115
116 if (inputFiles.isEmpty()) {
118 return 1;
119 }
120
121 const QStringList proFiles = extractProFiles(&inputFiles);
122 if (proFiles.isEmpty()) {
123 printErr(u"lrelease-pro: No .pro/.pri files given.\n"_s);
124 return 1;
125 }
126 if (!inputFiles.isEmpty()) {
127 printErr(QStringLiteral("lrelease-pro: Only .pro/.pri files are supported. "
128 "Offending files:\n %1\n")
129 .arg(inputFiles.join("\n "_L1)));
130 return 1;
131 }
132
133 QStringList translationsVariables = { "TRANSLATIONS"_L1, "EXTRA_TRANSLATIONS"_L1 };
134 QString outDir = QDir::currentPath();
135 QHash<QString, QString> outDirMap;
136
137 QStringList cleanProFiles;
138 for (const QString &proFile : proFiles) {
139 QString cleanFile = QDir::cleanPath(QFileInfo(proFile).absoluteFilePath());
140 cleanProFiles << cleanFile;
141 outDirMap[cleanFile] = outDir;
142 }
143
144 QString errorString;
145 QJsonArray results;
146 Projects projects = generateProjects(cleanProFiles, translationsVariables, outDirMap, 0,
147 cd.m_verbose, &errorString, &results);
148
149 if (dumpJsonFile) {
150 QFile projectDescriptionFile(*dumpJsonFile);
151 if (!projectDescriptionFile.open(QIODevice::WriteOnly))
152 return 1;
153
154 const QByteArray output = QJsonDocument(results).toJson(QJsonDocument::Compact);
155 projectDescriptionFile.write(output);
156 projectDescriptionFile.write("\n");
157 projectDescriptionFile.close();
158 printOut("Project description saved to: %1\n"_L1.arg(projectDescriptionFile.fileName()));
159 return 0;
160 }
161
162 if (!errorString.isEmpty()) {
163 printErr(QStringLiteral("lrelease-pro error: %1\n").arg(errorString));
164 return 1;
165 }
166 if (projects.empty()) {
167 printErr(u"lrelease-pro error: No projects found in project description\n"_s);
168 return 1;
169 }
170
171 QStringList tsFiles = translationsFromProjects(projects);
172 if (tsFiles.isEmpty()) {
173 printErr(u"lrelease-pro error: no TS files specified.\n"_s);
174 return 1;
175 }
176
177 if (!markUntranslated.isEmpty())
178 cd.m_unTrPrefix = markUntranslated;
179
180 bool fail = false;
181 for (const QString &tsFile : std::as_const(tsFiles)) {
182 if (!releaseTsFile(tsFile, cd, params))
183 fail = true;
184 }
185
186 return fail ? 1 : 0;
187}
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