Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
4
#
include
"releasehelper.h"
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
14
using
namespace
Qt::StringLiterals;
15
16
QT_BEGIN_NAMESPACE
17
18
void
printOut
(
const
QString &out)
19
{
20
std
::cout << qPrintable(out);
21
}
22
23
void
printErr
(
const
QString &out)
24
{
25
std
::cerr << qPrintable(out);
26
}
27
28
bool
loadTsFile
(
Translator
&tor,
const
QString &tsFileName)
29
{
30
ConversionData
cd;
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
}
38
cd
.
clearErrors
(
)
;
39
return
ok;
40
}
41
42
bool
releaseTranslator
(
Translator
&tor,
const
QString &qmFileName,
ConversionData
&cd,
43
ParamFlags
params)
44
{
45
if
(params
.
failOnUnfinished
&& tor
.
unfinishedTranslationsExist
(
)
) {
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
QList<Validator::Error> 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.append(
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
Validator::Error &error : errors)
73
printErr(
"Validation error for translation '%1'\n"_L1
.arg(error.message));
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));
85
tor
.
stripIdenticalSourceTranslations
(
)
;
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
94
tor
.
normalizeTranslations
(
cd
)
;
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
}
103
cd
.
clearErrors
(
)
;
104
return
ok;
105
}
106
107
bool
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
125
QStringList
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
138
QStringList
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
146
QT_END_NAMESPACE
ConversionData
Definition
translator.h:26
ConversionData::clearErrors
void clearErrors()
Definition
translator.h:40
ConversionData::isVerbose
bool isVerbose() const
Definition
translator.h:32
Translator
Definition
translator.h:79
Translator::unfinishedTranslationsExist
bool unfinishedTranslationsExist() const
Definition
translator.cpp:477
Translator::stripIdenticalSourceTranslations
void stripIdenticalSourceTranslations()
Definition
translator.cpp:503
Translator::resolveDuplicates
Duplicates resolveDuplicates()
Definition
translator.cpp:606
Translator::normalizeTranslations
void normalizeTranslations(ConversionData &cd)
Definition
translator.cpp:733
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
Projects
std::vector< Project > Projects
Definition
projectdescriptionreader.h:20
translationsFromProjects
QStringList translationsFromProjects(const Projects &projects, bool topLevel=true)
Definition
releasehelper.cpp:138
loadTsFile
bool loadTsFile(Translator &tor, const QString &tsFileName)
Definition
releasehelper.cpp:28
printOut
void printOut(const QString &out)
Definition
main.cpp:25
releaseTranslator
bool releaseTranslator(Translator &tor, const QString &qmFileName, ConversionData &cd, ParamFlags params)
Definition
releasehelper.cpp:42
releaseTsFile
bool releaseTsFile(const QString &tsFileName, ConversionData &cd, ParamFlags params)
Definition
releasehelper.cpp:107
translationsFromProject
QStringList translationsFromProject(const Project &project, bool topLevel)
Definition
releasehelper.cpp:125
printErr
void printErr(const QString &out)
Definition
main.cpp:30
ParamFlags
Definition
releasehelper.h:19
ParamFlags::failOnInvalid
bool failOnInvalid
Definition
releasehelper.h:21
ParamFlags::failOnUnfinished
bool failOnUnfinished
Definition
releasehelper.h:20
ParamFlags::removeIdentical
bool removeIdentical
Definition
releasehelper.h:22
Project
Definition
projectdescriptionreader.h:23
Validator::Checks
Definition
validator.h:28
Validator
Definition
validator.h:21
qttools
src
linguist
shared
releasehelper.cpp
Generated on
for Qt by
1.18.0