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) 2025 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 <QCoreApplication>
5#include <QCommandLineParser>
6#include <QMap>
7#include <QString>
8
9#include "translator.h"
10#include "validator.h"
11
12QT_USE_NAMESPACE
13
14using namespace Qt::StringLiterals;
15
16static void printErr(const QString &out)
17{
18 QTextStream stream(stderr);
19 stream << out;
20}
21
22static void printOut(const QString &out)
23{
24 QTextStream stream(stdout);
25 stream << out;
26}
27
28int main(int argc, char **argv)
29{
30 QCoreApplication app(argc, argv);
31 QCoreApplication::setApplicationName(u"lcheck"_s);
32 QCoreApplication::setApplicationVersion(QLatin1StringView(QT_VERSION_STR));
33
34 QCommandLineParser parser;
35 parser.setApplicationDescription(
36 u"lcheck is part of Qt's Linguist tool chain. It can be used as a\n"
37 "stand-alone tool to perform batch checks on the translations of\n"
38 "TS files. By default, lcheck performs the following checks and\n"
39 "fails if at least one check fails:\n"
40 " Validity check of accelerators:\n"
41 " Whether the number of ampersands in the source\n"
42 " and translation text is the same.\n"
43 " Validity check of surrounding whitespaces:\n"
44 " Whether the source and translation texts have the\n"
45 " same surrounding whitespaces.\n"
46 " Validity check of ending punctuation:\n"
47 " Whether the source and translation texts have the\n"
48 " same ending punctuation.\n"
49 " Validity check of place markers:\n"
50 " Whether %1, %2, ... are used consistently in the\n"
51 " source text and translation text.\n"
52 "To get more details regarding the checks refer to Qt Linguist help.\n"
53 "Each check can be disabled using the arguments as explained below."_s);
54 parser.addHelpOption();
55 parser.addVersionOption();
56
57 QCommandLineOption noAcceleratorOption(u"no-accelerator"_s, u"Disable the accelerator check"_s);
58 parser.addOption(noAcceleratorOption);
59
60 QCommandLineOption noPunctuationOption(u"no-punctuation"_s, u"Disable the punctuation check"_s);
61 parser.addOption(noPunctuationOption);
62
63 QCommandLineOption noPlaceMarkerOption(u"no-place-marker"_s,
64 u"Disable the place marker check"_s);
65 parser.addOption(noPlaceMarkerOption);
66
67 QCommandLineOption noWhitespacesOption(u"no-whitespaces"_s,
68 u"Disable the check for surrounding white spaces"_s);
69 parser.addOption(noWhitespacesOption);
70
71 QCommandLineOption checkFinishedOption(
72 u"check-finished"_s,
73 u"Enable check for translations marked as finished.\n"
74 "By default, the finished translations are not checked."_s);
75 parser.addOption(checkFinishedOption);
76
77 QCommandLineOption outputOption(QStringList() << u"o"_s << u"output"_s,
78 u"The output file to generate the report to. If\n"
79 "nothing is specified, the report is written to\n"
80 "the standard error stream."_s,
81 u"file"_s);
82 parser.addOption(outputOption);
83
84 parser.addPositionalArgument(u"ts-file"_s, u"TS file to check"_s);
85
86 parser.process(app);
87
88 const QStringList args = parser.positionalArguments();
89 if (args.isEmpty()) {
90 parser.showHelp(1);
91 }
92
93 const QString tsFile = args.first();
94
95 Validator::Checks checks;
96 checks.accelerator = !parser.isSet(noAcceleratorOption);
97 checks.punctuation = !parser.isSet(noPunctuationOption);
98 checks.placeMarker = !parser.isSet(noPlaceMarkerOption);
99 checks.surroundingWhiteSpace = !parser.isSet(noWhitespacesOption);
100
101 bool checkFinished = parser.isSet(checkFinishedOption);
102 std::optional<QString> output;
103 if (parser.isSet(outputOption))
104 output = parser.value(outputOption);
105
106 Translator tor;
108 bool ok = tor.load(tsFile, cd, "auto"_L1);
109 if (!ok) {
110 printErr("lcheck error: %1"_L1.arg(cd.error()));
111 return 1;
112 }
113
114 if (!cd.errors().isEmpty())
115 ok = false;
116
117 QLocale::Language sourceLang;
118 QLocale::Language targetLang;
119 QLocale::Territory targetTerritory;
120 QList<bool> countRefNeeds;
121 QMap<Validator::ErrorType, QString> errors;
122
123 tor.languageAndTerritory(tor.sourceLanguageCode(), &sourceLang, nullptr);
124 tor.languageAndTerritory(tor.languageCode(), &targetLang, &targetTerritory);
125
126 if (checks.placeMarker && !getCountNeed(targetLang, targetTerritory, countRefNeeds, nullptr)) {
127 printErr("Could not get numerus info");
128 ok = false;
129 }
130
131 for (const TranslatorMessage &msg : tor.messages()) {
132 if (msg.isTranslated() && (checkFinished || msg.type() != TranslatorMessage::Finished)) {
133 Validator validator = Validator::fromSource(msg.sourceText(), checks, sourceLang, {});
134 errors.insert(validator.validate(msg.translations(), msg, targetLang, countRefNeeds));
135 }
136 }
137
138 QTextStream stream(stderr);
139 QFile f;
140 if (output) {
141 f.setFileName(*output);
142 if (f.open(QIODevice::WriteOnly | QIODevice::Text))
143 stream.setDevice(&f);
144 else {
145 printErr("Could not open the output file %1 for writing."_L1.arg(*output));
146 return 1;
147 }
148 }
149
150 for (const QString &trs : errors)
151 stream << "Validation error for translation '%1'\n"_L1.arg(trs);
152
153 printOut("Finished batch checks.");
154
155 if (!errors.empty())
156 ok = false;
157
158 return ok ? 0 : 1;
159}
Definition qlist.h:81
static void printOut(const QString &out)
Definition main.cpp:22
static void printErr(const QString &out)
Definition main.cpp:16
int main(int argc, char *argv[])
[ctor_close]