-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinvertapplication.cpp
More file actions
244 lines (237 loc) · 7.91 KB
/
Copy pathlinvertapplication.cpp
File metadata and controls
244 lines (237 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/****************************************************************************
**
** Copyright (C) 2015 Mikhail Y. Zvyozdochkin aka DarkHobbit
** Contact: pub@zvyozdochkin.ru
**
** This file is part of the LInvert utility for Qt
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public License
** version 2.1 or version 3 as published by the Free Software Foundation.
** Please review the ** following information to ensure the
** GNU Lesser General Public License requirements will be met:
** https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
****************************************************************************/
#include <QFile>
#include <QStringList>
#include "linvertapplication.h"
#include "liuifile.h"
#include "licppfile.h"
LInvertApplication::LInvertApplication(int &argc, char **argv)
: QCoreApplication(argc, argv),
out(stdout),
ignoreDefault(false)
{
}
// Main program loop
int LInvertApplication::start()
{
out << tr("LInvert for Qt Utility by Mikhail Y. Zvyozdochkin\n");
// Parse command line
if (arguments().count()<2) {
printUsage();
return 1;
}
QStringList tsFiles;
QString langCode;
for (int i=1; i<arguments().count(); i++) {
// Option: language code
if (arguments()[i]=="-l") {
i++;
if (i==arguments().count()) {
out << tr("linvert error: -l option present, but lang code is missing\n");
printUsage();
return 2;
}
langCode = arguments()[i];
continue;
}
// Option: process messages by default
else if (arguments()[i]=="-p") {
ignoreDefault = false;
continue;
}
// Option: ignore messages by default
else if (arguments()[i]=="-i") {
ignoreDefault = true;
continue;
}
// Otherwise, argument must be a .ts file name
QString fileName = arguments()[i];
if (fileName.right(3)!=".ts")
fileName.append(".ts");
if (!QFile(fileName).exists()) {
out << tr("linvert error: File %1 not exists\n").arg(fileName);
printUsage();
return 3;
}
tsFiles.push_back(fileName);
}
if (langCode.length()==0) {
out << tr("linvert error: -l option is missing\n");
printUsage();
return 4;
}
srcFiles.clear();
// Process found TS files
for (int i=0; i<tsFiles.count(); i++)
if (!processTS(tsFiles[i], langCode))
return 5;
// Process source files
for (int i=0; i<srcFiles.count(); i++)
if (!srcFiles[i]->process()) {
for (int j=0; j<=i; j++)
srcFiles[j]->rollBack();
return 6;
}
return 0;
}
// Print program usage if error occured
void LInvertApplication::printUsage()
{
out << tr(
"Usage:\n" \
" linvert -l LANG_CODE [-p] [-i] ts-file [ts-file]...\n" \
"\n" \
"-l sets a language code, used as suffix for output .ts file\n" \
"-p process messages where <message linvert=...> is not set (DEFAULT)\n" \
"-i ignore messages where <message linvert=...> is not set\n"
);
}
// Process one .ts file
bool LInvertApplication::processTS(const QString& fileName, const QString& langCode)
{
out << tr("Processing %1:\n").arg(fileName);
// Open
QFile file(fileName);
if (!file.open( QIODevice::ReadOnly)) {
out << tr("linvert error: Can't open file %1\n").arg(fileName);
return false;
}
// Read to document
QDomDocument ts("TS");
QString err_msg;
int err_line;
int err_col;
if (!ts.setContent(&file, &err_msg, &err_line, &err_col)) {
out << tr("linvert error: Can't read content from file %1\n" \
"%2\nline %3, col %4\n"
).arg(fileName).arg(err_msg).arg(err_line).arg(err_col);
file.close();
return false;
}
file.close();
// Parse
QDomElement root = ts.documentElement();
if (root.nodeName()!="TS") {
out << tr("linvert error: Root node is not 'TS' at file %1\n").arg(fileName);
return false;
}
root.setAttribute("language", langCode);
QDomElement c = root.firstChildElement();
while (!c.isNull()) {
if (c.nodeName()=="context") {
QDomElement msg = c.firstChildElement();
int msgCount = 0;
while (!msg.isNull()) {
if (msg.nodeName()=="name")
out << tr("Context: ") << msg.text() << "\n";
else if (msg.nodeName()=="message") {
if (!processMessageNode(fileName, msg))
return false;
msgCount++;
}
else
out << tr("linvert warning: unknown element %1\n").arg(msg.nodeName());
msg = msg.nextSiblingElement();
}
out << tr("Found %1 messages in context\n").arg(msgCount);
}
c = c.nextSiblingElement();
}
// Write modified file
QString outFileName = fileName;
outFileName.replace(".ts", QString("_%1.ts").arg(langCode));
QFile outFile(outFileName);
if (!outFile.open( QIODevice::WriteOnly)) {
out << tr("linvert error: Can't write file %1\n").arg(outFileName);
return false;
}
QTextStream out(&outFile);
ts.save(out, 4);
outFile.close();
// Bye-bye
return true;
}
// Process one <message> record
bool LInvertApplication::processMessageNode(const QString& fileName, QDomElement &msg)
{
QString mSrc = "";
QString mTr = "";
QString srcName = "";
uint srcLine = 0;
QDomElement eSrc, eTr;
// Parse node
// Ignore nodes marked as <message linvert="false">
bool ignore = ignoreDefault;
if (msg.attribute("linvert")=="true")
ignore = false;
if (msg.attribute("linvert")=="false")
ignore = true;
if (ignore) return true;
QDomElement ch = msg.firstChildElement();
while (!ch.isNull()) {
if (ch.nodeName()=="source") {
mSrc = ch.text();
eSrc = ch;
}
else if (ch.nodeName()=="translation") {
mTr = ch.text();
eTr = ch;
}
else if (ch.nodeName()=="location") {
srcName = ch.attribute("filename", "");
srcLine = ch.attribute("line").toUInt();
}
ch = ch.nextSiblingElement();
}
if ((mSrc=="")||(mTr=="")) {
out << tr(
"linvert error: Incomplete message at file %1, source: %2.\nProcessing may corrupt source files\n")
.arg(fileName).arg(mSrc);
// TODO maybe here add pseudo-number to untranslated messages?
return false;
}
/*out << "Source: " << mSrc << "\n";
out << "Translation: " << mTr << "\n";*/
// Swap source and translation
msg.removeChild(eSrc);
msg.removeChild(eTr);
eSrc = msg.ownerDocument().createElement("source");
eSrc.appendChild(msg.ownerDocument().createTextNode(mTr));
msg.appendChild(eSrc);
eTr = msg.ownerDocument().createElement("translation");
eTr.appendChild(msg.ownerDocument().createTextNode(mSrc));
msg.appendChild(eTr);
msg.removeAttribute("utf8");
// Mark node for change in source file
if ((srcName.length()==0)||(srcLine==0)) {
out << tr(
"linvert warning: unknown file or line for message '%1' in file %2\nSource will not modified")
.arg(mSrc).arg(fileName);
}
else {
LISourceFile* srcFile = srcFiles.findByFileName(srcName);
if (!srcFile) {
if (srcName.contains(".ui"))
srcFile = new LIUIFile(srcName, out);
else
srcFile = new LICPPFile(srcName, out);
srcFiles.push_back(srcFile);
}
srcFile->addMessage(mSrc, mTr, srcLine);
}
return true;
}