summaryrefslogtreecommitdiffstats
path: root/poxml/split.cpp
blob: 29104d3ec0cadc110f4e69774f1942e9ee8dd762 (plain)
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
#include "parser.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

int main( int argc, char **argv )
{
    bool report_mismatches = qstrcmp(getenv("REPORT_MISMATCHES"), "no");

    if (argc != 3) {
        qWarning("usage: %s english-XML translated-XML", argv[0]);
        exit(1);
    }

    MsgList english = parseXML(argv[1]);
    MsgList translated = parseXML(argv[2]);

    TQMap<TQString, int>::ConstIterator eit2 = english.pc.anchors.begin();

    TQMap<int, TQString> errors;

    while (eit2 != english.pc.anchors.end())
    {
        if (eit2.data() == translated.pc.anchors[eit2.key()]) {
            TQString key = eit2.key();
            eit2++;
            translated.pc.anchors.remove(key);
            english.pc.anchors.remove(key);
        } else {
            errors[eit2.data()] = eit2.key();
            eit2++;
        }
    }

    if (report_mismatches && errors.count()) {
        for (TQMap<int, TQString>::ConstIterator it = errors.begin(); it != errors.end(); ++it)
        {
            if (translated.pc.anchors.contains(it.data()))
                fprintf(stderr, "id=\"%s\" not in the same paragraphs (%d vs %d)\n", it.data().latin1(),
                        english.pc.anchors[it.data()], translated.pc.anchors[it.data()]);
            else {
                fprintf(stderr, "id=\"%s\" not in the translated paragraphs (it's in paragraph %d in english)\n",
                        it.data().latin1(), english.pc.anchors[it.data()]);
            }
        }
        ::exit(1);
    }

    MsgList::ConstIterator tit = translated.begin();
    for (MsgList::Iterator it = english.begin();
         it != english.end() && tit != translated.end();
         ++tit, ++it)
    {
        (*it).msgstr = (*tit).msgid;
    }

    bool have_roles_of_translators = false;
    bool have_credit_for_translators = false;

    TQMap<TQString, int> msgids;
    int index = 0;

    for (MsgList::Iterator it = english.begin();
         it != english.end(); )
    {
	if ((*it).msgid == "ROLES_OF_TRANSLATORS") {
            if ((*it).msgstr.length() && !(*it).msgstr.contains("ROLES_OF_TRANSLATORS")) {
	        have_roles_of_translators = true; 
            }
            else {
                MsgList::Iterator tmp = it;
	        ++it;
	        english.remove(tmp);
            }
            continue;
	}

        if ((*it).msgid == "CREDIT_FOR_TRANSLATORS") {
            if ((*it).msgstr.length() && !(*it).msgstr.contains("CREDIT_FOR_TRANSLATORS")) {
	        have_credit_for_translators = true; 
            }
            else {
                MsgList::Iterator tmp = it;
	        ++it;
	        english.remove(tmp);
            }
            continue;
	}

        if (msgids.contains((*it).msgid)) {
            english[msgids[(*it).msgid]].lines += (*it).lines;
            if (english[msgids[(*it).msgid]].msgstr != (*it).msgstr) {
                fprintf(stderr, "two different translations for \"%s\" (\"%s\" and \"%s\") - choosing first one\n",
                        (*it).msgid.latin1(),
                        english[msgids[(*it).msgid]].msgstr.local8Bit().data(),
                        (*it).msgstr.local8Bit().data());

            }
            MsgList::Iterator tmp = it;
            it++;
            english.remove(tmp);
        } else {
            msgids.insert((*it).msgid, index);
            index++;
            it++;
        }
    }

    int counter = 1;

    while (tit != translated.end())
    {
        MsgBlock mb;
        mb.msgid = TQString::fromLatin1("appended paragraph %1").arg(counter++);
        mb.msgstr = (*tit).msgid;
        mb.lines += (*tit).lines;
        english.append(mb);
        tit++;
    }

    cout << "#, fuzzy\n";
    cout << "msgid \"\"\n";
    cout << "msgstr \"\"\n";
    cout << "\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n";
    cout << "\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"\n";
    cout << "\"Content-Type: text/plain; charset=utf-8\\n\"\n";

    for (MsgList::ConstIterator it = english.begin();
         it != english.end(); ++it)
    {
        cout << "#: ";
        for (TQValueList<BlockInfo>::ConstIterator it2 =
                 (*it).lines.begin(); it2 != (*it).lines.end(); it2++) {
            if (it2 != (*it).lines.begin())
                cout << ", ";
            cout << "index.docbook:" << (*it2).start_line;

        }
        cout << "\n";
        outputMsg("msgid", StructureParser::descapeLiterals( (*it).msgid ));
        outputMsg("msgstr", StructureParser::descapeLiterals( (*it).msgstr ));
        cout << "\n";
    }

    if ( !getenv( "NO_CREDITS" ) ) {

        if ( !have_roles_of_translators ) {
            outputMsg("msgid", "ROLES_OF_TRANSLATORS");
            outputMsg("msgstr", "<!--TRANS:ROLES_OF_TRANSLATORS-->");
            cout << "\n";
        }

	if ( !have_credit_for_translators) {
           outputMsg("msgid", "CREDIT_FOR_TRANSLATORS");
           outputMsg("msgstr", "<!--TRANS:CREDIT_FOR_TRANSLATORS-->");
           cout << "\n";
        }
    }

    return 0;
}