// #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; TQString translate(TQString xml, TQString orig, TQString translation) { TQString 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) { tqWarning("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) { tqWarning("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; } TQMap translations; for (MsgList::ConstIterator it = translated.begin(); it != translated.end(); ++it) { TQString msgstr; TQString msgid = escapePO((*it).msgid); if ((*it).comment.find("fuzzy") < 0) msgstr = escapePO((*it).msgstr); #ifdef POXML_DEBUG tqDebug("inserting translations '%s' -> '%s'", msgid.latin1(),msgstr.latin1()); #endif translations.insert(msgid, msgstr); } TQFile xml(argv[1]); xml.open(IO_ReadOnly); TQTextStream ds(&xml); ds.setEncoding(TQTextStream::UnicodeUTF8); TQString xml_text = ds.read(); xml.close(); TQString output; TQTextStream ts(&output, IO_WriteOnly); StructureParser::cleanupTags(xml_text); TQValueList 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; TQString 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; TQString xml = xml_text.mid(start_pos, end_pos - start_pos); int index = 0; while (true) { index = xml.find("