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