13 #include <QTextStream>
23 QString out = str.trimmed();
24 out = out.replace(
"\"",
"");
33 if (str.contains(
"#"))
34 return str.section(
"#", 0, 0);
44 QString out = msg.trimmed();
46 out.replace(
"\"\n\"",
"");
47 if (out.startsWith(
"\""))
48 out = out.remove(0, 1);
49 if (out.endsWith(
"\""))
51 out.replace(
"\\\"",
"\"");
52 out.replace(
"\\n",
"\\r\\n");
53 out.replace(
"$\\r\\n",
"$\\r$\\n");
61 stream->skipWhiteSpace();
62 return stream->readLine().append(
"\n");
75 line = po->readLine();
76 while (!po->atEnd() && !line.isEmpty())
77 line = po->readLine();
85 po2nsh(QTextStream *po, QString *nsh,
const QString &language,
86 QString *errorMessage)
89 QString msgctxt, msgid, msgstr;
90 QHash<QString,QString> header;
96 Q_ASSERT(errorMessage);
100 while (!po->atEnd()) {
102 while (line.startsWith(
"#")) {
103 if (line.startsWith(
"#:")) {
105 msgctxt = line.section(
" ", 1);
113 if (line.startsWith(
"msgctxt ")) {
114 msgctxt = line.section(
" ", 1);
120 if (!line.startsWith(
"msgid ")) {
121 *errorMessage =
"expected 'msgid' line";
124 msgid = line.section(
" ", 1);
127 while (line.startsWith(
"\"")) {
134 if (!line.startsWith(
"msgstr ")) {
135 *errorMessage =
"expected 'msgstr' line";
138 msgstr = line.section(
" ", 1);
141 while (line.startsWith(
"\"")) {
148 nsh->append(QString(
"LangString "));
149 nsh->append(msgctxt);
150 nsh->append(QString(
" ${LANG_%1} ").arg(language));
151 if (msgstr.isEmpty())
152 nsh->append(
"\"" + msgid +
"\"");
154 nsh->append(
"\"" + msgstr +
"\"");
166 QTextStream
error(stderr);
167 error <<
"usage: po2nsh [-q] -i <infile.po> -o <outfile.nsh> "
168 "-l <language> [-f <from-encoding>] [-t <to-encoding>]\n";
169 error <<
" -q (optional) Quiet mode (errors are still displayed)\n";
170 error <<
" -i <infile.po> Input .po file\n";
171 error <<
" -o <outfile.nsh> Output .nsh file\n";
172 error <<
" -l <language> NSIS language table name\n";
173 error <<
" -f <from-encoding> .po file encoding (default: utf-8)\n";
174 error <<
" -t <to-encoding> .nsh file encoding (default: iso-8859-1)\n";
182 QTextStream
error(stderr);
183 QString language, errorMessage;
184 char *infile, *outfile;
186 QTextCodec *from_codec = QTextCodec::codecForName(
"utf-8");
187 QTextCodec *to_codec = QTextCodec::codecForName(
"iso-8859-1");
190 if (argc < 7 || argc > 12)
192 for (
int i = 1;
i < argc;
i++) {
193 QString arg(argv[
i]);
194 if (!arg.compare(
"-q", Qt::CaseInsensitive))
196 else if (!arg.compare(
"-i", Qt::CaseInsensitive) && ++i < argc)
198 else if (!arg.compare(
"-o", Qt::CaseInsensitive) && ++i < argc)
200 else if (!arg.compare(
"-l", Qt::CaseInsensitive) && ++i < argc)
201 language = QString(argv[i]).toUpper();
202 else if (!arg.compare(
"-f", Qt::CaseInsensitive) && ++i < argc) {
203 from_codec = QTextCodec::codecForName(argv[i]);
205 error <<
"Invalid input encoding: " << argv[
i] <<
"\n";
208 }
else if (!arg.compare(
"-t", Qt::CaseInsensitive) && ++i < argc) {
209 to_codec = QTextCodec::codecForName(argv[i]);
211 error <<
"Invalid output encoding: " << argv[
i] <<
"\n";
219 QFile poFile(infile);
220 if (!poFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
221 error << QString(
"Unable to open '%1' for reading: %2\n").arg(infile)
222 .arg(poFile.errorString());
227 QTextStream po(&poFile);
228 po.setCodec(from_codec);
229 int n_strings =
po2nsh(&po, &nsh, language, &errorMessage);
231 error << QString(
"Unable to convert '%1': %2\n").arg(infile)
237 QFile nshFile(outfile);
238 if (!nshFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
239 error << QString(
"Unable to open '%1' for writing: %2\n").arg(outfile)
240 .arg(nshFile.errorString());
245 QTextStream out(&nshFile);
246 out.setCodec(to_codec);
250 QTextStream results(stdout);
251 results << QString(
"Converted %1 strings from %2 to %3.\n").arg(n_strings)
DebugMessage error(const QString &fmt)
int po2nsh(QTextStream *po, QString *nsh, const QString &language, QString *errorMessage)
int main(int argc, char *argv[])
QString parse_message_string(const QString &msg)
void skip_po_header(QTextStream *po)
void print_usage_and_exit()
QString parse_message_context_lame(const QString &str)
QString read_next_line(QTextStream *stream)
QString parse_message_context(const QString &str)