From bd9e6617827818fd043452c08c606f07b78014a0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdesdk@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- poxml/po2xml.cpp | 261 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 poxml/po2xml.cpp (limited to 'poxml/po2xml.cpp') diff --git a/poxml/po2xml.cpp b/poxml/po2xml.cpp new file mode 100644 index 00000000..9e8bc1a5 --- /dev/null +++ b/poxml/po2xml.cpp @@ -0,0 +1,261 @@ + // #define POXML_DEBUG + +#include "parser.h" +#include +#include +#include +#include + +#include +#include "GettextLexer.hpp" +#include "GettextParser.hpp" +#include "antlr/AST.hpp" +#include "antlr/CommonAST.hpp" + +using namespace std; + +QString translate(QString xml, QString orig, QString translation) +{ + QString prefix; + while (xml.at(0) == '<' && orig.at(0) != '<') { + // a XML tag as prefix + int index = xml.find('>'); + assert(index != -1); + index++; + while (xml.at(index) == ' ') + index++; + prefix = prefix + xml.left(index); + xml = xml.mid(index, xml.length()); + } + + int index = xml.find(orig); + if (index == -1) { + qWarning("can't find\n%s\nin\n%s", orig.latin1(), xml.latin1()); + exit(1); + } + if (!translation.isEmpty()) + xml.replace(index, orig.length(), translation); + return prefix + xml; +} + +int main( int argc, char **argv ) +{ + if (argc != 3) { + qWarning("usage: %s english-XML translated-PO", argv[0]); + ::exit(1); + } + + MsgList english = parseXML(argv[1]); + MsgList translated; + + try { + ifstream s(argv[2]); + GettextLexer lexer(s); + GettextParser parser(lexer); + translated = parser.file(); + + } catch(exception& e) { + cerr << "exception: " << e.what() << endl; + return 1; + } + + QMap translations; + for (MsgList::ConstIterator it = translated.begin(); + it != translated.end(); ++it) + { + QString msgstr; + QString msgid = escapePO((*it).msgid); + if ((*it).comment.find("fuzzy") < 0) + msgstr = escapePO((*it).msgstr); + +#ifdef POXML_DEBUG + qDebug("inserting translations '%s' -> '%s'", msgid.latin1(),msgstr.latin1()); +#endif + translations.insert(msgid, msgstr); + } + + QFile xml(argv[1]); + xml.open(IO_ReadOnly); + QTextStream ds(&xml); + ds.setEncoding(QTextStream::UnicodeUTF8); + QString xml_text = ds.read(); + xml.close(); + QString output; + QTextStream ts(&output, IO_WriteOnly); + StructureParser::cleanupTags(xml_text); + + QValueList line_offsets; + line_offsets.append(0); + int index = 0; + while (true) { + index = xml_text.find('\n', index) + 1; + if (index <= 0) + break; + line_offsets.append(index); + } + + int old_start_line = -1, old_start_col = -1; + QString old_text; + MsgList::Iterator old_it = english.end(); + + for (MsgList::Iterator it = english.begin(); + it != english.end(); ++it) + { + BlockInfo bi = (*it).lines.first(); + int start_pos = line_offsets[bi.start_line - 1] + bi.start_col; + if (!bi.end_line) + continue; + int end_pos = line_offsets[bi.end_line - 1] + bi.end_col - 1; + + (*it).start = start_pos; + if (old_start_line == bi.start_line && + old_start_col == bi.start_col) + { + (*old_it).end = bi.offset; + (*it).end = end_pos; + } else { + (*it).lines.first().offset = 0; + (*it).end = 0; + } + + old_start_line = bi.start_line; + old_start_col = bi.start_col; + old_it = it; + } + + int old_pos = 0; + + for (MsgList::Iterator it = english.begin(); + it != english.end(); ++it) + { + BlockInfo bi = (*it).lines.first(); + int start_pos = line_offsets[bi.start_line - 1] + bi.start_col; + if (!bi.end_line) + continue; + int end_pos = line_offsets[bi.end_line - 1] + bi.end_col - 1; + + QString xml = xml_text.mid(start_pos, end_pos - start_pos); + int index = 0; + while (true) { + index = xml.find("