Пример #1
0
int main( int argc, char** argv )
{
    if ( argc != 3 ) {
        usage( argv[0] );
        return 0;
    }

    QFileInfo const ts = QFileInfo( QString( argv[1] ) );
    if ( !ts.exists() ) {
        usage( argv[0] );
        return 1;
    }

    QFileInfo const po = QFileInfo( QString( argv[2] ) );
    if ( !po.exists() ) {
        usage( argv[0] );
        return 2;
    }

    QMap<QString,QString> translations;

    // Open the .po file and build a map of translations
    QFile poFile( po.absoluteFilePath() );
    poFile.open( QFile::ReadOnly );
    QTextStream poStream( &poFile );
    poStream.setCodec( "UTF-8" );
    poStream.setAutoDetectUnicode( true );
    QString source;
    bool ignore = false;
    while( !poStream.atEnd() ) {
        QString line = poStream.readLine();
        if ( line.startsWith( "#, fuzzy" ) ) {
            ignore = true;
        } else if ( line.startsWith( "msgid " ) ) {
            source = line.mid( 7, line.size() - 8 );
        } else if ( !source.isEmpty() && line.startsWith( "msgstr " ) ) {
            if ( ignore ) {
                ignore = false;
            } else {
                QString translation = line.mid( 8, line.size() - 9 );
                source.replace( "&", "&amp;" );
                translation.replace( "&", "&amp;" );
                source.replace( "<", "&lt;" );
                translation.replace( "<", "&lt;" );
                source.replace( ">", "&gt;" );
                translation.replace( ">", "&gt;" );
                if ( !translation.isEmpty() ) {
                    translations[source] = translation;
                }
            }
        }
    }

    QTextStream console( stdout );
    console.setCodec( "UTF-8" );

    // Open the .ts file and replace source strings with translations
    // The modified .to file is dumped to stdout
    QFile tsFile( ts.absoluteFilePath() );
    tsFile.open( QFile::ReadOnly );
    QTextStream tsStream( &tsFile );
    tsStream.setCodec( "UTF-8" );
    tsStream.setAutoDetectUnicode( true );
    source = QString();
    while( !tsStream.atEnd() ) {
        QString line = tsStream.readLine().trimmed();
        if ( line.startsWith( "<source>" ) ) {
            source = line.mid( 8, line.size() - 17 );
            console << line << "\n";
        } else if ( !source.isEmpty() &&
                   line == "<translation type=\"unfinished\"></translation>" &&
                   translations.contains( source ) ) {
            console << "<translation>" << translations[source] << "</translation>\n";
        } else if ( !source.isEmpty() &&
                   line == "<translation type=\"unfinished\"></translation>" ) {
            console << line << "\n";
        } else {
            console << line << "\n";
        }
    }

    return 0;
}
Пример #2
0
int
main(int argc, char *argv[])
{
  QTextStream error(stderr);
  QString errorMessage;
  char *infile, *outfile;
  QTextCodec *codec = QTextCodec::codecForName("utf-8");
  bool quiet = false;

  /* Check for the correct number of input parameters. */
  if (argc < 5 || argc > 8)
    print_usage_and_exit();
  for (int i = 1; i < argc; i++) {
    QString arg(argv[i]);
    if (!arg.compare("-q", Qt::CaseInsensitive))
      quiet = true;
    else if (!arg.compare("-i", Qt::CaseInsensitive) && ++i < argc)
      infile = argv[i];
    else if (!arg.compare("-o", Qt::CaseInsensitive) && ++i < argc)
      outfile = argv[i];
    else if (!arg.compare("-c", Qt::CaseInsensitive) && ++i < argc) {
      codec = QTextCodec::codecForName(argv[i]);
      if (!codec) {
        error << "Invalid text encoding specified\n";
        return 1;
      }
    } else
      print_usage_and_exit(); 
  }

  /* Open the input PO file for reading. */
  QFile poFile(infile);
  if (!poFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
    error << QString("Unable to open '%1' for reading: %2\n").arg(infile)
                                                .arg(poFile.errorString());
    return 2;
  }

  QDomDocument ts;
  QTextStream po(&poFile);
  po.setCodec(codec);
  int n_strings = po2ts(&po, &ts, &errorMessage);
  if (n_strings < 0) {
    error << QString("Unable to convert '%1': %2\n").arg(infile)
                                                    .arg(errorMessage);
    return 3;
  }

  /* Open the TS file for writing. */
  QFile tsFile(outfile);
  if (!tsFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
    error << QString("Unable to open '%1' for writing: %2\n").arg(outfile)
                                                .arg(tsFile.errorString());
    return 4;
  }

  /* Write the .ts output. */
  QTextStream out(&tsFile);
  out.setCodec(codec);
  out << QString("<?xml version=\"1.0\" encoding=\"%1\"?>\n")
                                                  .arg(QString(codec->name()));
  out << ts.toString(4);

  if (!quiet) {
    QTextStream results(stdout);
    results << QString("Converted %1 strings from %2 to %3.\n").arg(n_strings)
                                                               .arg(infile)
                                                               .arg(outfile);
  }
  return 0;
}