33 case Validator::SuperfluousAccelerator:
34 return u"Accelerator possibly superfluous in translation"_s;
35 case Validator::MissingAccelerator:
36 return u"Accelerator possibly missing in translation"_s;
37 case Validator::SurroundingWhitespaceDiffers:
38 return u"Translation does not have the same surrounding whitespace as the source text"_s;
39 case Validator::PunctuationDiffers:
40 return u"Translation does not end with the same punctuation as the source text"_s;
41 case Validator::PlaceMarkersDiffer:
42 return u"Translation does not refer to the same place markers as the source text"_s;
43 case Validator::NumerusMarkerMissing:
44 return u"Translation does not contain the necessary %n/%Ln place marker"_s;
45 case Validator::IgnoredPhrasebook:
46 return u"A phrase book suggestion was ignored"_s;
48 return u"Unknown error"_s;
51int main(
int argc,
char **argv)
53 QCoreApplication app(argc, argv);
54 QCoreApplication::setApplicationName(u"lcheck"_s);
55 QCoreApplication::setApplicationVersion(QLatin1StringView(QT_VERSION_STR));
57 QCommandLineParser parser;
58 parser.setApplicationDescription(
59 u"lcheck is part of Qt's Linguist tool chain. It can be used as a\n"
60 "stand-alone tool to perform batch checks on the translations of\n"
61 "TS files. By default, lcheck performs the following checks and\n"
62 "fails if at least one check fails:\n"
63 " Validity check of accelerators:\n"
64 " Whether the number of ampersands in the source\n"
65 " and translation text is the same.\n"
66 " Validity check of surrounding whitespaces:\n"
67 " Whether the source and translation texts have the\n"
68 " same surrounding whitespaces.\n"
69 " Validity check of ending punctuation:\n"
70 " Whether the source and translation texts have the\n"
71 " same ending punctuation.\n"
72 " Validity check of place markers:\n"
73 " Whether %1, %2, ... are used consistently in the\n"
74 " source text and translation text.\n"
75 "To get more details regarding the checks refer to Qt Linguist help.\n"
76 "Each check can be disabled using the arguments as explained below."_s);
77 parser.addHelpOption();
78 parser.addVersionOption();
80 QCommandLineOption noAcceleratorOption(u"no-accelerator"_s, u"Disable the accelerator check"_s);
81 parser.addOption(noAcceleratorOption);
83 QCommandLineOption noPunctuationOption(u"no-punctuation"_s, u"Disable the punctuation check"_s);
84 parser.addOption(noPunctuationOption);
86 QCommandLineOption noPlaceMarkerOption(u"no-place-marker"_s,
87 u"Disable the place marker check"_s);
88 parser.addOption(noPlaceMarkerOption);
90 QCommandLineOption noWhitespacesOption(u"no-whitespaces"_s,
91 u"Disable the check for surrounding white spaces"_s);
92 parser.addOption(noWhitespacesOption);
94 QCommandLineOption checkFinishedOption(
96 u"Enable check for translations marked as finished.\n"
97 "By default, the finished translations are not checked."_s);
98 parser.addOption(checkFinishedOption);
100 QCommandLineOption outputOption(QStringList() << u"o"_s << u"output"_s,
101 u"The output file to generate the report to. If\n"
102 "nothing is specified, the report is written to\n"
103 "the standard error stream."_s,
105 parser.addOption(outputOption);
107 parser.addPositionalArgument(u"ts-file"_s, u"TS file to check"_s);
111 const QStringList args = parser.positionalArguments();
112 if (args.isEmpty()) {
116 const QString tsFile = args.first();
118 Validator::Checks checks;
119 checks.accelerator = !parser.isSet(noAcceleratorOption);
120 checks.punctuation = !parser.isSet(noPunctuationOption);
121 checks.placeMarker = !parser.isSet(noPlaceMarkerOption);
122 checks.surroundingWhiteSpace = !parser.isSet(noWhitespacesOption);
124 bool checkFinished = parser.isSet(checkFinishedOption);
125 std::optional<QString> output;
126 if (parser.isSet(outputOption))
127 output = parser.value(outputOption);
131 bool ok = tor.load(tsFile, cd,
"auto"_L1);
133 printErr(
"lcheck error: %1"_L1.arg(cd.error()));
137 if (!cd.errors().isEmpty())
140 QLocale::Language sourceLang;
141 QLocale::Language targetLang;
142 QLocale::Territory targetTerritory;
143 QList<
bool> countRefNeeds;
145 tor.languageAndTerritory(tor.sourceLanguageCode(), &sourceLang,
nullptr);
146 tor.languageAndTerritory(tor.languageCode(), &targetLang, &targetTerritory);
148 if (checks.placeMarker && !getCountNeed(targetLang, targetTerritory, countRefNeeds,
nullptr)) {
149 printErr(
"Could not get numerus info");
153 QTextStream stream(stderr);
156 f.setFileName(*output);
157 if (f.open(QIODevice::WriteOnly | QIODevice::Text))
158 stream.setDevice(&f);
160 printErr(
"Could not open the output file %1 for writing."_L1.arg(*output));
166 for (
const TranslatorMessage &msg : tor.messages()) {
167 if (!msg.isTranslated() || (!checkFinished && msg.type() == TranslatorMessage::Finished))
170 Validator validator = Validator::fromSource(msg.sourceText(), checks, sourceLang, {});
171 const QList<Validator::Error> errors =
172 validator.validate(msg.translations(), msg, targetLang, countRefNeeds);
173 if (errors.isEmpty())
176 QString location = msg.fileName();
177 if (!location.isEmpty()) {
178 if (msg.lineNumber() >= 0)
179 location +=
":"_L1 + QString::number(msg.lineNumber());
183 for (
const Validator::Error &error : errors) {
184 stream << location << checkDescription(error.type) << u'\n';
185 if (!msg.context().isEmpty())
186 stream <<
" Context: "_L1 << msg.context() << u'\n';
187 stream <<
" Source: "_L1 << msg.sourceText() << u'\n';
188 stream <<
" Translation: "_L1 << error.message << u'\n';
196 printOut(u"Finished batch checks. Found %1 validation error(s).\n"_s.arg(errorCount));