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
fileverifier.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 "fileverifier.h"
5#include "utils.h"
7#include <translator.h>
8#include <trlib/trparser.h>
9
10#include <QFileInfo>
11#include <QMap>
12
13using namespace Qt::StringLiterals;
14using namespace Utils;
15
16QT_BEGIN_NAMESPACE
17
18FileVerifier::FileVerifier(const RecordDirectory &records, bool quiet)
19 : m_records(records), m_quiet(quiet)
20{
21}
22
23bool FileVerifier::verifyTs(const QString &tsFile, QSet<QString> &transformedIds)
24{
25 bool verifyFail = false;
27 Translator verifyTor;
28 verifyTor.load(tsFile, cd, "ts");
29 verifyTor.makeFileNamesAbsolute(QFileInfo(tsFile).absoluteDir());
30
31 if (!cd.errors().empty()) {
32 printErr("ltext2id error: cannot load the TS file %1 after transformation. "
33 "Manual fix is necessary."_L1.arg(tsFile));
34 verifyFail = true;
35 }
36
37 for (qsizetype i = 0; i < verifyTor.messageCount(); i++) {
38 const TranslatorMessage &msg = verifyTor.message(i);
39 if (!msg.context().isEmpty()
40 && !m_records.isNonSupported(msg.fileName(), msg.lineNumber())) {
41 qWarning()
42 << "ltext2id error: failed to transform message with context '%1' and source '%2' in TS file %3"_L1
43 .arg(msg.context(), msg.sourceText(), tsFile);
44 verifyFail = true;
45 } else
46 transformedIds.remove(msg.id());
47 }
48 for (const QString &missingId : transformedIds) {
49 printErr("ltext2id error: missing id '%1' from the TS file %2."_L1.arg(missingId).arg(
50 tsFile));
51 verifyFail = true;
52 }
53 return !verifyFail;
54}
55
56void FileVerifier::verifySources(const QStringList &sources, ConversionData &cd)
57{
58 Translator verifyTor;
59 processSources(verifyTor, sources, cd);
60
61 RecordDirectory verifyRecords;
62 for (const TranslatorMessage &msg : verifyTor.messages())
63 verifyRecords.recordMessage(msg);
64
65 for (const auto &[filename, messages] : m_records.messageLocations().asKeyValueRange()) {
66 if (!m_quiet)
67 printOut("ltext2id: verifying source file %1"_L1.arg(filename));
68 const auto &verifyMessages = verifyRecords.messageLocations()[filename];
69 auto transformedItr = verifyMessages.cbegin();
70 for (const std::shared_ptr<MessageItem> &original : messages) {
71 if (m_records.isNonSupported(filename, original->lineNo)
72 && transformedItr != verifyMessages.cend()) {
73 transformedItr++;
74 continue;
75 }
76 // fast forward the ignored messages or the ones that were
77 // already id based before transformation
78 while (transformedItr != verifyMessages.cend()
79 && transformedItr->get()->lineNo < original->lineNo)
80 transformedItr++;
81
82 if (transformedItr == verifyMessages.cend())
83 const_cast<RecordDirectory &>(m_records).recordError(
84 filename, original->lineNo,
85 "Missing translation call with the expected id '%1' after transformation"_L1
86 .arg(original->id));
87 else {
88 const MessageItem &transformed = *transformedItr->get();
89 if (original->sourceText != transformed.sourceText || original->id != transformed.id
90 || original->lineNo != transformed.lineNo
91 || original->plural != transformed.plural) {
92 const_cast<RecordDirectory &>(m_records).recordError(filename, original->lineNo,
93 original->id);
94 } else
95 transformedItr++;
96 }
97 }
98 }
99
100 for (const auto &[fileName, fileErrors] : m_records.errors().asKeyValueRange()) {
101 QStringList lines = readLines(fileName);
102 for (const auto &[lineNo, error] : fileErrors.asKeyValueRange()) {
103 auto &line = lines[lineNo - 1];
104 const QString indentation = getIndentation(line);
105 line = indentation + error + '\n' + line;
106 }
107 writeLines(fileName, lines);
108 }
109}
110
111QT_END_NAMESPACE
void verifySources(const QStringList &sources, ConversionData &cd)
bool verifyTs(const QString &tsFile, QSet< QString > &transformedIds)
int messageCount() const
Definition translator.h:137