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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/* === This file is part of Calamares - <https://calamares.io> ===
*
* SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
/*
* Tool to find differences between translations (can be used to help
* merging them into one). See usage string, below, for details.
*
* The tool can be used when there are multiple translation files
* for a single language (e.g. Spanish) which need to be reconciled.
* Then running `txload file0.ts file1.ts ...` will produce a
* human-readable overview of what is translated and where the
* differences in translation are.
*/
#include "compat/Xml.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
// #include <QList>
#include <QDomDocument>
static const char usage[] = "Usage: txload <origin> [<alternate> ...]\n"
"\n"
"Reads a .ts source file <origin> and zero or more .ts <alternate>\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 );
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;
auto p = Calamares::setXmlContent( doc, ba );
if ( !p.errorMessage.isEmpty() )
{
qDebug() << "Could not read" << filename << ':' << p.errorLine << ':' << p.errorColumn << ' ' << p.errorMessage;
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& origin, QDomElement& alternate )
{
QDomNode n = alternate.firstChild();
while ( !n.isNull() )
{
if ( n.isElement() )
{
QDomElement alternateMessage = n.toElement();
if ( alternateMessage.tagName() == "message" )
{
QString alternateSourceText = alternateMessage.firstChildElement( "source" ).text();
QString alternateTranslationText = alternateMessage.firstChildElement( "translation" ).text();
QDomElement originMessage = find_message( origin, alternateSourceText );
if ( originMessage.isNull() )
{
qDebug() << "No origin translation for" << alternateSourceText;
return false;
}
QString originSourceText = originMessage.firstChildElement( "source" ).text();
QString originTranslationText = originMessage.firstChildElement( "translation" ).text();
if ( alternateSourceText != originSourceText )
{
qDebug() << "Mismatch for messages\n" << alternateSourceText << '\n' << originSourceText;
return false;
}
if ( !alternateTranslationText.isEmpty() && ( alternateTranslationText != originTranslationText ) )
{
qDebug() << "\n\n\nSource:" << alternateSourceText << "\nTL1:" << originTranslationText
<< "\nTL2:" << alternateTranslationText;
}
}
}
n = n.nextSibling();
}
return true;
}
bool
merge_into( QDomDocument& originDocument, QDomElement& context )
{
QDomElement name = context.firstChildElement( "name" );
if ( name.isNull() )
{
return false;
}
QString contextname = name.text();
QDomElement originContext = find_context( originDocument, contextname );
if ( originContext.isNull() )
{
qDebug() << "Origin document has no context" << contextname;
return false;
}
return merge_into( originContext, context );
}
bool
merge_into( QDomDocument& originDocument, QDomDocument& alternateDocument )
{
QDomElement top = alternateDocument.documentElement();
QDomNode n = top.firstChild();
while ( !n.isNull() )
{
if ( n.isElement() )
{
QDomElement e = n.toElement();
if ( e.tagName() == "context" )
{
if ( !merge_into( originDocument, 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 originDocument( "origin" );
if ( !load_file( argv[ 1 ], originDocument ) )
{
return 1;
}
for ( int i = 2; i < argc; ++i )
{
QDomDocument alternateDocument( "alternate" );
if ( !load_file( argv[ i ], alternateDocument ) )
{
return 1;
}
if ( !merge_into( originDocument, alternateDocument ) )
{
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( originDocument.toString( 4 ).toUtf8() );
outfile.close();
return 0;
}
|