@ -26,29 +26,31 @@
# 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 )
" \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 ) ;
QFile file ( filename ) ;
QString err ;
int err_line , err_column ;
if ( ! file . open ( QIODevice : : ReadOnly ) )
if ( ! file . open ( QIODevice : : ReadOnly ) )
{
qDebug ( ) < < " Could not open " < < filename ;
return false ;
}
QByteArray ba ( file . read ( 1024 * 1024 ) ) ;
QByteArray ba ( file . read ( 1024 * 1024 ) ) ;
qDebug ( ) < < " Read " < < ba . length ( ) < < " bytes from " < < filename ;
if ( ! doc . setContent ( ba , & err , & err_line , & err_column ) ) {
if ( ! doc . setContent ( ba , & err , & err_line , & err_column ) )
{
qDebug ( ) < < " Could not read " < < filename < < ' : ' < < err_line < < ' : ' < < err_column < < ' ' < < err ;
file . close ( ) ;
return false ;
@ -58,15 +60,20 @@ bool load_file(const char* filename, QDomDocument& doc)
return true ;
}
QDomElement find_context ( QDomDocument & doc , const QString & name )
QDomElement
find_context ( QDomDocument & doc , const QString & name )
{
QDomElement top = doc . documentElement ( ) ;
QDomNode n = top . firstChild ( ) ;
while ( ! n . isNull ( ) ) {
if ( n . isElement ( ) ) {
while ( ! n . isNull ( ) )
{
if ( n . isElement ( ) )
{
QDomElement e = n . toElement ( ) ;
if ( ( e . tagName ( ) = = " context " ) & & ( e . firstChildElement ( " name " ) . text ( ) = = name ) )
{
return e ;
}
}
n = n . nextSibling ( ) ;
}
@ -74,17 +81,22 @@ QDomElement find_context(QDomDocument& doc, const QString& name)
return QDomElement ( ) ;
}
QDomElement find_message ( QDomElement & context , const QString & source )
QDomElement
find_message ( QDomElement & context , const QString & source )
{
QDomNode n = context . firstChild ( ) ;
while ( ! n . isNull ( ) ) {
if ( n . isElement ( ) ) {
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 ( ) ;
@ -92,11 +104,14 @@ QDomElement find_message(QDomElement& context, const QString& source)
return QDomElement ( ) ;
}
bool merge_into ( QDomElement & origin , QDomElement & alternate )
bool
merge_into ( QDomElement & origin , QDomElement & alternate )
{
QDomNode n = alternate . firstChild ( ) ;
while ( ! n . isNull ( ) ) {
if ( n . isElement ( ) ) {
while ( ! n . isNull ( ) )
{
if ( n . isElement ( ) )
{
QDomElement alternateMessage = n . toElement ( ) ;
if ( alternateMessage . tagName ( ) = = " message " )
{
@ -119,7 +134,8 @@ bool merge_into(QDomElement& origin, QDomElement& alternate)
}
if ( ! alternateTranslationText . isEmpty ( ) & & ( alternateTranslationText ! = originTranslationText ) )
{
qDebug ( ) < < " \n \n \n Source: " < < alternateSourceText < < " \n TL1: " < < originTranslationText < < " \n TL2: " < < alternateTranslationText ;
qDebug ( ) < < " \n \n \n Source: " < < alternateSourceText < < " \n TL1: " < < originTranslationText
< < " \n TL2: " < < alternateTranslationText ;
}
}
}
@ -130,12 +146,14 @@ bool merge_into(QDomElement& origin, QDomElement& alternate)
}
bool merge_into ( QDomDocument & originDocument , QDomElement & context )
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 ) ;
@ -148,16 +166,21 @@ bool merge_into(QDomDocument& originDocument, QDomElement& context)
return merge_into ( originContext , context ) ;
}
bool merge_into ( QDomDocument & originDocument , QDomDocument & alternateDocument )
bool
merge_into ( QDomDocument & originDocument , QDomDocument & alternateDocument )
{
QDomElement top = alternateDocument . documentElement ( ) ;
QDomNode n = top . firstChild ( ) ;
while ( ! n . isNull ( ) ) {
if ( n . isElement ( ) ) {
while ( ! n . isNull ( ) )
{
if ( n . isElement ( ) )
{
QDomElement e = n . toElement ( ) ;
if ( e . tagName ( ) = = " context " )
if ( ! merge_into ( originDocument , e ) )
{
return false ;
}
}
n = n . nextSibling ( ) ;
}
@ -165,39 +188,46 @@ bool merge_into(QDomDocument& originDocument, QDomDocument& alternateDocument)
return true ;
}
int main ( int argc , char * * argv )
int
main ( int argc , char * * argv )
{
QCoreApplication a ( argc , argv ) ;
QCoreApplication a ( argc , argv ) ;
if ( argc < 2 )
if ( argc < 2 )
{
qWarning ( ) < < usage ;
return 1 ;
}
QDomDocument originDocument ( " origin " ) ;
if ( ! load_file ( argv [ 1 ] , originDocument ) )
QDomDocument originDocument ( " origin " ) ;
if ( ! load_file ( argv [ 1 ] , originDocument ) )
{
return 1 ;
}
for ( int i = 2 ; i < argc ; + + i )
for ( int i = 2 ; i < argc ; + + i )
{
QDomDocument alternateDocument ( " alternate " ) ;
if ( ! load_file ( argv [ i ] , alternateDocument ) )
QDomDocument alternateDocument ( " alternate " ) ;
if ( ! load_file ( argv [ i ] , alternateDocument ) )
{
return 1 ;
}
if ( ! merge_into ( originDocument , alternateDocument ) )
{
return 1 ;
}
}
QString outfilename ( argv [ 1 ] ) ;
QString outfilename ( argv [ 1 ] ) ;
outfilename . append ( " .new " ) ;
QFile outfile ( outfilename ) ;
if ( ! outfile . open ( QIODevice : : WriteOnly ) )
QFile outfile ( outfilename ) ;
if ( ! outfile . open ( QIODevice : : WriteOnly ) )
{
qDebug ( ) < < " Could not open " < < outfilename ;
return 1 ;
}
outfile . write ( originDocument . toString ( 4 ) . toUtf8 ( ) ) ;
outfile . write ( originDocument . toString ( 4 ) . toUtf8 ( ) ) ;
outfile . close ( ) ;
return 0 ;