summaryrefslogtreecommitdiff
path: root/lang/txload.cpp
blob: 36e0f71a27d9b81caf0e51e88da0ead031f40973 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* === This file is part of Calamares - <https://github.com/calamares> ===
 *
 *   Copyright 2018, Adriaan de Groot <groot@kde.org>
 *
 *   Calamares is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Calamares is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Tool to find differences between translations (can be used to help
 * merging them into one). See usage string, below, for details.
 */

#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QList>

#include <QDomDocument>

static const char usage[] = "Usage: txload <master> [<subsidiary> ...]\n"
    "\n"
    "Reads a .ts source file <master> and zero or more .ts <subsidiary>\n"
    "files, and does a comparison between the translations. Source (English)\n"
    "strings that are untranslated are flagged in each of the translation\n"
    "files, while differences in the translations are themselves also shown.\n"
    "\n"
    "Outputs to stdout a human-readable list of differences between the\n"
    "translations.\n";

bool load_file(const char* filename, QDomDocument& doc)
{
    QFile file(filename);
    QString err;
    int err_line, err_column;
    if (!file.open(QIODevice::ReadOnly))
    {
        qDebug() << "Could not open" << filename;
        return false;
    }
    QByteArray ba( file.read(1024 * 1024) );
    qDebug() << "Read" << ba.length() << "bytes from" << filename;

    if (!doc.setContent(ba, &err, &err_line, &err_column)) {
        qDebug() << "Could not read" << filename << ':' << err_line << ':' << err_column << ' ' << err;
        file.close();
        return false;
    }
    file.close();

    return true;
}

QDomElement find_context(QDomDocument& doc, const QString& name)
{
    QDomElement top = doc.documentElement();
    QDomNode n = top.firstChild();
    while (!n.isNull()) {
        if (n.isElement()) {
            QDomElement e = n.toElement();
            if ( ( e.tagName() == "context" ) && ( e.firstChildElement( "name" ).text() == name ) )
                return e;
        }
        n = n.nextSibling();
    }

    return QDomElement();
}

QDomElement find_message(QDomElement& context, const QString& source)
{
    QDomNode n = context.firstChild();
    while (!n.isNull()) {
        if (n.isElement()) {
            QDomElement e = n.toElement();
            if ( e.tagName() == "message" )
            {
                QString msource = e.firstChildElement( "source" ).text();
                if ( msource == source )
                    return e;
            }
        }
        n = n.nextSibling();
    }
    return QDomElement();
}

bool merge_into(QDomElement& master, QDomElement& sub)
{
    QDomNode n = sub.firstChild();
    while (!n.isNull()) {
        if (n.isElement()) {
            QDomElement e = n.toElement();
            if ( e.tagName() == "message" )
            {
                QString source = e.firstChildElement( "source" ).text();
                QString translation = e.firstChildElement( "translation" ).text();
                QDomElement masterTranslation = find_message( master, source );
                if ( masterTranslation.isNull() )
                {
                    qDebug() << "No master translation for" << source;
                    return false;
                }

                QString msource = masterTranslation.firstChildElement( "source" ).text();
                QString mtranslation = masterTranslation.firstChildElement( "translation" ).text();

                if ( source != msource )
                {
                    qDebug() << "Mismatch for messages\n" << source << '\n' << msource;
                    return false;
                }
                if ( !translation.isEmpty() && ( translation != mtranslation ) )
                {
                    qDebug() << "\n\n\nSource:" << source << "\nTL1:" << mtranslation << "\nTL2:" << translation;
                }
            }
        }
        n = n.nextSibling();
    }

    return true;
}



bool merge_into(QDomDocument& master, QDomElement& context)
{
    QDomElement name = context.firstChildElement( "name" );
    if ( name.isNull() )
        return false;

    QString contextname = name.text();
    QDomElement masterContext = find_context( master, contextname );
    if ( masterContext.isNull() )
    {
        qDebug() << "Master document has no context" << contextname;
        return false;
    }

    return merge_into( masterContext, context );
}

bool merge_into(QDomDocument& master, QDomDocument& sub)
{
    QDomElement top = sub.documentElement();
    QDomNode n = top.firstChild();
    while (!n.isNull()) {
        if (n.isElement()) {
            QDomElement e = n.toElement();
            if ( e.tagName() == "context" )
                if ( !merge_into( master, e ) )
                    return false;
        }
        n = n.nextSibling();
    }

    return true;
}

int main(int argc, char** argv)
{
    QCoreApplication a(argc, argv);

    if (argc < 2)
    {
        qWarning() << usage;
        return 1;
    }

    QDomDocument doc("master");
    if ( !load_file(argv[1], doc) )
        return 1;

    for (int i = 2; i < argc; ++i)
    {
        QDomDocument subdoc("sub");
        if ( !load_file(argv[i], subdoc) )
            return 1;
        if ( !merge_into( doc, subdoc ) )
            return 1;
    }

    QString outfilename( argv[1] );
    outfilename.append( ".new" );
    QFile outfile(outfilename);
    if (!outfile.open(QIODevice::WriteOnly))
    {
        qDebug() << "Could not open" << outfilename;
        return 1;
    }

    outfile.write( doc.toString(4).toUtf8() );
    outfile.close();

    return 0;
}