27int main(
int argc,
char **argv)
29 QCoreApplication app(argc, argv);
30 QCoreApplication::setApplicationName(u"lcheck"_s);
31 QCoreApplication::setApplicationVersion(QLatin1StringView(QT_VERSION_STR));
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();
56 QCommandLineOption noAcceleratorOption(u"no-accelerator"_s, u"Disable the accelerator check"_s);
57 parser.addOption(noAcceleratorOption);
59 QCommandLineOption noPunctuationOption(u"no-punctuation"_s, u"Disable the punctuation check"_s);
60 parser.addOption(noPunctuationOption);
62 QCommandLineOption noPlaceMarkerOption(u"no-place-marker"_s,
63 u"Disable the place marker check"_s);
64 parser.addOption(noPlaceMarkerOption);
66 QCommandLineOption noWhitespacesOption(u"no-whitespaces"_s,
67 u"Disable the check for surrounding white spaces"_s);
68 parser.addOption(noWhitespacesOption);
70 QCommandLineOption checkFinishedOption(
72 u"Enable check for translations marked as finished.\n"
73 "By default, the finished translations are not checked."_s);
74 parser.addOption(checkFinishedOption);
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,
81 parser.addOption(outputOption);
83 parser.addPositionalArgument(u"ts-file"_s, u"TS file to check"_s);
87 const QStringList args = parser.positionalArguments();
92 const QString tsFile = args.first();
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);
100 bool checkFinished = parser.isSet(checkFinishedOption);
101 std::optional<QString> output;
102 if (parser.isSet(outputOption))
103 output = parser.value(outputOption);
107 bool ok = tor.load(tsFile, cd,
"auto"_L1);
109 printErr(
"lcheck error: %1"_L1.arg(cd.error()));
113 if (!cd.errors().isEmpty())
116 QLocale::Language sourceLang;
117 QLocale::Language targetLang;
118 QLocale::Territory targetTerritory;
119 QList<
bool> countRefNeeds;
120 QMap<Validator::ErrorType, QString> errors;
122 tor.languageAndTerritory(tor.sourceLanguageCode(), &sourceLang,
nullptr);
123 tor.languageAndTerritory(tor.languageCode(), &targetLang, &targetTerritory);
125 if (checks.placeMarker && !getCountNeed(targetLang, targetTerritory, countRefNeeds,
nullptr)) {
126 printErr(
"Could not get numerus info");
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));
137 QTextStream stream(stderr);
140 f.setFileName(*output);
141 if (f.open(QIODevice::WriteOnly | QIODevice::Text))
142 stream.setDevice(&f);
144 printErr(
"Could not open the output file %1 for writing."_L1.arg(*output));
149 for (
const QString &trs : errors)
150 stream <<
"Validation error for translation '%1'\n"_L1.arg(trs);
152 printOut(
"Finished batch checks.");