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
releasehelper.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
5#include "translator.h"
6#include "validator.h"
7#include "linguistproject/projectdescriptionreader.h"
8
9#include <QtCore/qfile.h>
10#include <QtCore/qfileinfo.h>
11
12#include <iostream>
13
14using namespace Qt::StringLiterals;
15
17
18void printOut(const QString &out)
19{
20 std::cout << qPrintable(out);
21}
22
23void printErr(const QString &out)
24{
25 std::cerr << qPrintable(out);
26}
27
28bool loadTsFile(Translator &tor, const QString &tsFileName)
29{
31 bool ok = tor.load(tsFileName, cd, "auto"_L1);
32 if (!ok) {
33 printErr("lrelease error: %1"_L1.arg(cd.error()));
34 } else {
35 if (!cd.errors().isEmpty())
36 printOut(cd.error());
37 }
39 return ok;
40}
41
42bool releaseTranslator(Translator &tor, const QString &qmFileName, ConversionData &cd,
43 ParamFlags params)
44{
46 printErr("lrelease error: cannot create '%1': existing unfinished translation(s) "
47 "found (-fail-on-unfinished)"_L1.arg(qmFileName));
48 return false;
49 }
50
51 if (params.failOnInvalid) {
52 Validator::Checks checks{ true, true, true, true };
53 QLocale::Language sourceLang;
54 QLocale::Language targetLang;
55 QLocale::Territory targetTerritory;
56 QList<bool> countRefNeeds;
57 tor.languageAndTerritory(tor.sourceLanguageCode(), &sourceLang, nullptr);
58 tor.languageAndTerritory(tor.languageCode(), &targetLang, &targetTerritory);
59 QMap<Validator::ErrorType, QString> errors;
60 if (getCountNeed(targetLang, targetTerritory, countRefNeeds, nullptr))
61 for (const TranslatorMessage &msg : tor.messages()) {
62 if (msg.isTranslated() && msg.type() != TranslatorMessage::Finished) {
63 Validator validator =
64 Validator::fromSource(msg.sourceText(), checks, sourceLang, {});
65 errors.insert(
66 validator.validate(msg.translations(), msg, targetLang, countRefNeeds));
67 }
68 }
69 else
70 printErr("Could not get numerus info"_L1);
71 if (cd.isVerbose())
72 for (const QString &trs : errors)
73 printErr("Validation error for translation '%1'"_L1.arg(trs));
74 if (!errors.empty())
75 return false;
76 }
77
78 tor.reportDuplicates(tor.resolveDuplicates(), qmFileName, cd.isVerbose());
79
80 if (cd.isVerbose())
81 printOut("Updating '%1'...\n"_L1.arg(qmFileName));
82 if (params.removeIdentical) {
83 if (cd.isVerbose())
84 printOut("Removing translations equal to source text in '%1'...\n"_L1.arg(qmFileName));
86 }
87
88 QFile file(qmFileName);
89 if (!file.open(QIODevice::WriteOnly)) {
90 printErr("lrelease error: cannot create '%1': %2\n"_L1.arg(qmFileName, file.errorString()));
91 return false;
92 }
93
95 bool ok = saveQM(tor, file, cd);
96 file.close();
97
98 if (!ok) {
99 printErr("lrelease error: cannot save '%1': %2"_L1.arg(qmFileName, cd.error()));
100 } else if (!cd.errors().isEmpty()) {
101 printOut(cd.error());
102 }
104 return ok;
105}
106
107bool releaseTsFile(const QString &tsFileName, ConversionData &cd, ParamFlags params)
108{
109 Translator tor;
110 if (!loadTsFile(tor, tsFileName))
111 return false;
112
113 QString qmFileName = tsFileName;
114 for (const Translator::FileFormat &fmt : std::as_const(Translator::registeredFileFormats())) {
115 if (qmFileName.endsWith(u'.' + fmt.extension)) {
116 qmFileName.chop(fmt.extension.size() + 1);
117 break;
118 }
119 }
120 qmFileName += ".qm"_L1;
121
122 return releaseTranslator(tor, qmFileName, cd, params);
123}
124
125QStringList translationsFromProject(const Project &project, bool topLevel)
126{
127 QStringList result;
128 if (project.translations)
129 result = *project.translations;
130 result << translationsFromProjects(project.subProjects, false);
131 if (topLevel && result.isEmpty()) {
132 printErr("lrelease warning: Met no 'TRANSLATIONS' entry in project file '%1'\n"_L1.arg(
133 project.filePath));
134 }
135 return result;
136}
137
138QStringList translationsFromProjects(const Projects &projects, bool topLevel)
139{
140 QStringList result;
141 for (const Project &p : projects)
142 result << translationsFromProject(p, topLevel);
143 return result;
144}
145
146QT_END_NAMESPACE
void clearErrors()
Definition translator.h:40
bool isVerbose() const
Definition translator.h:32
bool unfinishedTranslationsExist() const
void stripIdenticalSourceTranslations()
Duplicates resolveDuplicates()
void normalizeTranslations(ConversionData &cd)
std::vector< Project > Projects
QStringList translationsFromProjects(const Projects &projects, bool topLevel=true)
bool loadTsFile(Translator &tor, const QString &tsFileName)
void printOut(const QString &out)
Definition main.cpp:25
bool releaseTranslator(Translator &tor, const QString &qmFileName, ConversionData &cd, ParamFlags params)
bool releaseTsFile(const QString &tsFileName, ConversionData &cd, ParamFlags params)
QStringList translationsFromProject(const Project &project, bool topLevel)
void printErr(const QString &out)
Definition main.cpp:30
bool failOnInvalid
bool failOnUnfinished
bool removeIdentical