Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 284 lines (237 sloc) 8.293 kB
a8e907c @stuartm Quick hack to let themestringtools build in trunk. Needs porting to QT4.
stuartm authored
1 #include <QApplication>
2 #include <QDomDocument>
3 #include <QString>
4 #include <QMap>
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
5 #include <QMultiMap>
a8e907c @stuartm Quick hack to let themestringtools build in trunk. Needs porting to QT4.
stuartm authored
6 #include <QStringList>
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
7 #include <QTextStream>
8 #include <QFile>
9 #include <QDir>
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
10 #include <QtGui>
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
11
3e5e19d Initial revision
Isaac Richards authored
12 #include <iostream>
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
13 #include <cstdlib>
3e5e19d Initial revision
Isaac Richards authored
14
15 using namespace std;
16
17 QString getFirstText(QDomElement element)
18 {
19 for (QDomNode dname = element.firstChild(); !dname.isNull();
20 dname = dname.nextSibling())
21 {
22 QDomText t = dname.toText();
23 if (!t.isNull())
24 return t.data();
25 }
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
26 return QString();
3e5e19d Initial revision
Isaac Richards authored
27 }
28
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
29 QString indir;
30 QString outfilebase;
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
31 QString outfile;
3e5e19d Initial revision
Isaac Richards authored
32 QFile fstringout;
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
33 QTextStream fdataout;
34 QMap<QString, QFile *> transFiles;
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
35 QMultiMap<QString, QString> translatedStrings;
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
36 QString laststring;
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
37 int totalStringCount = 0;
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
38 int stringCount = 0;
39 QStringList strings;
3e5e19d Initial revision
Isaac Richards authored
40
41 void parseElement(QDomElement &element)
42 {
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
43 laststring.clear();
3e5e19d Initial revision
Isaac Richards authored
44
45 for (QDomNode child = element.firstChild(); !child.isNull();
46 child = child.nextSibling())
47 {
48 QDomElement info = child.toElement();
49 if (!info.isNull())
50 {
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
51 if (info.tagName() == "value" || // UI theme
52 info.tagName() == "template" || // UI theme
53 info.tagName() == "helptext" || // UI theme
54 info.tagName() == "text" || // Menu theme
55 info.tagName() == "description") // Menu theme
3e5e19d Initial revision
Isaac Richards authored
56 {
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
57 if (info.attribute("lang", "").isEmpty())
3e5e19d Initial revision
Isaac Richards authored
58 {
59 laststring = getFirstText(info);
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
60 if (!laststring.trimmed().isEmpty())
3e5e19d Initial revision
Isaac Richards authored
61 {
66535a4 @stuartm Deal with newlines and whitespace added through xml formatting when p…
stuartm authored
62 // Escape quotes
63 laststring.replace("\"", QString("\\\""));
64 // Escape xml-escaped newline
65 laststring.replace("\\n", QString("<newline>"));
66
67 // Remove newline whitespace added by
68 // xml formatting
69 QStringList lines = laststring.split('\n');
70 QStringList::iterator lineIt;
71 for (lineIt = lines.begin(); lineIt != lines.end();
72 ++lineIt)
73 {
74 (*lineIt) = (*lineIt).trimmed();
75 }
76 laststring = lines.join(" ");
77
78 laststring.replace(QString("<newline>"), QString("\\n"));
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
79
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
80 if (!strings.contains(laststring))
81 strings << laststring;
82 ++stringCount;
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
83 ++totalStringCount;
3e5e19d Initial revision
Isaac Richards authored
84 }
85 }
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
86 else
87 {
88 QString language = info.attribute("lang", "").toLower();
89 translatedStrings.insert(laststring, language + "{}" + getFirstText(info));
90 }
3e5e19d Initial revision
Isaac Richards authored
91 }
92 else
93 parseElement(info);
94 }
95 }
96 }
97
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
98 void parseDirectory(QString dir)
99 {
100 QDir themeDir(dir);
101
102 cout << "Searching directory: " << qPrintable(themeDir.path()) << endl;
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
103
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
104 themeDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
105 themeDir.setSorting(QDir::DirsFirst);
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
106
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
107 QDomDocument doc;
108 QFileInfoList themeFiles = themeDir.entryInfoList();
109 QFileInfoList::const_iterator it;
110 for (it = themeFiles.begin(); it != themeFiles.end(); ++it)
111 {
112 if ((*it).isDir())
113 {
66535a4 @stuartm Deal with newlines and whitespace added through xml formatting when p…
stuartm authored
114 parseDirectory((*it).filePath());
115 continue;
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
116 }
117
118 if ((*it).suffix() != "xml")
119 continue;
120
121 cout << " Found: " << qPrintable((*it).filePath()) << endl;
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
122
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
123 QFile fin((*it).absoluteFilePath());
124
125 if (!fin.open(QIODevice::ReadOnly))
126 {
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
127 cerr << "Can't open " << qPrintable((*it).absoluteFilePath()) << endl;
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
128 continue;
129 }
130
131 QString errorMsg;
132 int errorLine = 0;
133 int errorColumn = 0;
134
135 if (!doc.setContent(&fin, false, &errorMsg, &errorLine, &errorColumn))
136 {
137 cerr << "Error parsing: " << qPrintable((*it).absoluteFilePath()) << endl;
138 cerr << "at line: " << errorLine << " column: "
139 << errorColumn << endl;
140 cerr << qPrintable(errorMsg) << endl;
141 fin.close();
142 continue;
143 }
144
145 fin.close();
146
147 stringCount = 0;
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
148
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
149 QDomElement docElem = doc.documentElement();
150 QDomNode n = docElem.firstChild();
151 while (!n.isNull())
152 {
153 QDomElement e = n.toElement();
154 if (!e.isNull())
155 {
156 parseElement(e);
157 }
158 n = n.nextSibling();
159 }
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
160
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
161 cout << " Contains " << stringCount << " total strings" << endl;
162 }
163
164 }
a8e907c @stuartm Quick hack to let themestringtools build in trunk. Needs porting to QT4.
stuartm authored
165
3e5e19d Initial revision
Isaac Richards authored
166 int main(int argc, char *argv[])
167 {
168 QApplication a(argc, argv, false);
169
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
170 if (a.argc() < 2)
3e5e19d Initial revision
Isaac Richards authored
171 {
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
172 cerr << "You must specify at least a starting directory." << endl;
173 a.exit(-1);
3e5e19d Initial revision
Isaac Richards authored
174 }
175
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
176 indir = a.argv()[1];
177 if (a.argc() == 3)
178 outfilebase = a.argv()[2];
179 else
180 outfilebase = indir;
3e5e19d Initial revision
Isaac Richards authored
181
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
182 if (indir.isEmpty() || outfilebase.isEmpty())
3e5e19d Initial revision
Isaac Richards authored
183 {
184 cerr << "no filenames\n";
185 exit(-1);
186 }
187
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
188 QDir themeDir(indir);
189 if (!themeDir.exists())
3e5e19d Initial revision
Isaac Richards authored
190 {
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
191 cerr << "Starting directory does not exist\n";
3e5e19d Initial revision
Isaac Richards authored
192 exit(-1);
193 }
194
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
195 outfile = outfilebase + '/' + "themestrings.h";
196
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
197 parseDirectory(indir);
a8e907c @stuartm Quick hack to let themestringtools build in trunk. Needs porting to QT4.
stuartm authored
198
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
199 fstringout.setFileName(outfile);
3e5e19d Initial revision
Isaac Richards authored
200
a8e907c @stuartm Quick hack to let themestringtools build in trunk. Needs porting to QT4.
stuartm authored
201 if (!fstringout.open(QIODevice::WriteOnly))
3e5e19d Initial revision
Isaac Richards authored
202 {
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
203 cerr << "can't open " << qPrintable(outfile) << " for writing\n";
3e5e19d Initial revision
Isaac Richards authored
204 exit(-1);
205 }
206
207 fdataout.setDevice(&fstringout);
64f9423 @kenni Fix UTF-8 encoded output in the themestringstool.
kenni authored
208 fdataout.setCodec("UTF-8");
3e5e19d Initial revision
Isaac Richards authored
209
56336a5 @stuartm Insert a 'Do not edit' warning comment at the start of themestrings.h
stuartm authored
210 fdataout << QString("// This is an automatically generated file\n");
211 fdataout << QString("// Do not edit\n\n");
a8e907c @stuartm Quick hack to let themestringtools build in trunk. Needs porting to QT4.
stuartm authored
212 fdataout << QString("void strings_null() {\n");
3e5e19d Initial revision
Isaac Richards authored
213
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
214 int lineCount = 2;
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
215 QStringList::const_iterator strit;
216 for (strit = strings.begin(); strit != strings.end(); ++strit)
3e5e19d Initial revision
Isaac Richards authored
217 {
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
218 QString string = (*strit);
219 if (string.contains("%n"))
220 fdataout << QString(" ThemeUI::tr(\"%1\", 0, 1);\n")
64f9423 @kenni Fix UTF-8 encoded output in the themestringstool.
kenni authored
221 .arg(string);
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
222 else
223 fdataout << QString(" ThemeUI::tr(\"%1\");\n")
64f9423 @kenni Fix UTF-8 encoded output in the themestringstool.
kenni authored
224 .arg(string);
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
225
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
226 #if 0
227 if (translatedStrings.contains(*strit))
228 {
229 QStringList prevSeenLanguages;
230 QList<QString> values = translatedStrings.values(*strit);
231 for (int i = 0; i < values.size(); ++i)
232 {
233 QString language = values.at(i).section("{}", 0, 0);
234 if (prevSeenLanguages.contains(language))
235 continue;
236 prevSeenLanguages << language;
237
238 QString translation = values.at(i).section("{}", 1, 1);
239 if (!transFiles.contains(language))
240 {
241 QFile *tmp = new QFile(outfile + '_' + language + ".ts");
242 if (!tmp->open(QIODevice::WriteOnly))
243 {
244 cerr << "couldn't open language file\n";
245 exit(-1);
246 }
247
248 transFiles[language] = tmp;
249 }
250
251 QTextStream dstream(transFiles[language]);
252 dstream.setCodec("UTF-8");
253
254 dstream << " <message>\n"
255 << " <location filename=\"" << qPrintable(outfile) << "\" line=\"" << lineCount << "\"/>\n"
256 << " <source>" << Qt::escape(*strit).replace("\"", "&quot;") << "<source>\n"
257 << " <translation>" << Qt::escape(translation).replace("\"", "&quot;") << "<translation>\n"
258 << " <message>\n";
259 }
260 }
261 #endif
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
262 ++lineCount;
3e5e19d Initial revision
Isaac Richards authored
263 }
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
264
a8e907c @stuartm Quick hack to let themestringtools build in trunk. Needs porting to QT4.
stuartm authored
265 fdataout << QString("}\n");
3e5e19d Initial revision
Isaac Richards authored
266 fstringout.close();
267
8b9c0ba @Beirdo Change cout to logging where appropriate
Beirdo authored
268 #if 0
269 QMap<QString, QFile *>::Iterator it;
270 for (it = transFiles.begin(); it != transFiles.end(); ++it)
271 {
272 it.value()->close();
273 }
274 #endif
3e5e19d Initial revision
Isaac Richards authored
275
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
276 cout << endl;
277 cout << "---------------------------------------" << endl;
96e884f @stuartm Fix numerous form strings extracted from xml not appearing as such in…
stuartm authored
278 cout << "Found " << totalStringCount << " total strings" << endl;
8dbd866 @stuartm Update themestringtool so that it will pull translatable strings from…
stuartm authored
279 cout << strings.count() << " unique" << endl;
280
3e5e19d Initial revision
Isaac Richards authored
281 return 0;
282 }
283
Something went wrong with that request. Please try again.