コード例 #1
0
ファイル: metatranslator.cpp プロジェクト: nmelchior/Tools
bool MetaTranslator::release( const QString& filename, bool verbose,
                              bool ignoreUnfinished,
                              Translator::SaveMode mode ) const
{
    Translator tor( 0 );
    int finished = 0;
    int unfinished = 0;
    int untranslated = 0;
    TMM::ConstIterator m;

    for ( m = mm.begin(); m != mm.end(); ++m ) {
        if ( m.key().type() != MetaTranslatorMessage::Obsolete ) {
            if ( m.key().translation().isEmpty() ) {
                untranslated++;
            } else {
                if ( m.key().type() == MetaTranslatorMessage::Unfinished )
                    unfinished++;
                else
                    finished++;

                QByteArray context = m.key().context();
                QByteArray sourceText = m.key().sourceText();
                QByteArray comment = m.key().comment();
                QByteArray translatorComment = m.key().translatorComment();
                QStringList translations = m.key().translations();

                if ( !ignoreUnfinished
                     || m.key().type() != MetaTranslatorMessage::Unfinished ) {
                    /*
                      Drop the comment in (context, sourceText, comment),
                      unless the context is empty,
                      unless (context, sourceText, "") already exists or
                      unless we already dropped the comment of (context,
                      sourceText, comment0).
                    */
                    if ( comment.isEmpty()
                         || context.isEmpty()
                         || contains(context, sourceText, "")
                         || !tor.findMessage(context, sourceText, "").translation()
                                .isNull() ) {
                        tor.insert( m.key() );
                    } else {
                        tor.insert( TranslatorMessage(context, sourceText, "", "",
                                                      QString(), -1, translations) );
                        //filename and lineNumbers will be ignored from now.
                    }
                }
            }
        }
    }

    bool saved = tor.save( filename, mode );
    if ( saved && verbose )
        fprintf( stderr,
                 " %d finished, %d unfinished and %d untranslated messages\n",
                 finished, unfinished, untranslated );

    return saved;
}
コード例 #2
0
QValueList<MetaTranslatorMessage> MetaTranslator::translatedMessages() const
{
    QValueList<MetaTranslatorMessage> val;
    TMM::ConstIterator m;
    for ( m = mm.begin(); m != mm.end(); ++m ) {
	if ( m.key().type() == MetaTranslatorMessage::Finished )
	    val.append( m.key() );
    }
    return val;
}
コード例 #3
0
bool MetaTranslator::release( const QString& filename, bool verbose, QTranslator::SaveMode mode ) const
{
	QTranslator tor( 0 );
	int finished = 0;
	int unfinished = 0;
	int untranslated = 0;
	TMM::ConstIterator m;

	for ( m = mm.begin(); m != mm.end(); ++m )
	{
		if ( m.key().type() != MetaTranslatorMessage::Obsolete )
		{
			if ( m.key().translation().isEmpty() )
			{
				untranslated++;
			}
			else
			{
				if ( m.key().type() == MetaTranslatorMessage::Unfinished )
					unfinished++;
				else
					finished++;

				QCString context = m.key().context();
				QCString sourceText = m.key().sourceText();
				QCString comment = m.key().comment();
				QString translation = m.key().translation();

				/*
					  Drop the comment in (context, sourceText, comment),
					  unless (context, sourceText, "") already exists, or
					  unless we already dropped the comment of (context,
					  sourceText, comment0).
					*/
				if ( comment.isEmpty() || contains( context, sourceText, "" ) || !tor.findMessage( context, sourceText, "" ).translation()
					.isNull() )
				{
					tor.insert( m.key() );
				}
				else
				{
					tor.insert( QTranslatorMessage( context, sourceText, "", translation ) );
				}
			}
		}
	}

	bool saved = tor.save( filename, mode );
	if ( saved && verbose )
		fprintf( stderr, " %d finished, %d unfinished and %d untranslated messages\n", finished, unfinished, untranslated );

	return saved;
}
コード例 #4
0
bool MetaTranslator::save( const QString& filename ) const
{
    QFile f( filename );
    if ( !f.open(IO_WriteOnly) )
	return FALSE;

    QTextStream t( &f );
    t.setCodec( QTextCodec::codecForName("ISO-8859-1") );

    t << "<!DOCTYPE TS><TS>\n";
    if ( codecName != "ISO-8859-1" )
	t << "<defaultcodec>" << codecName << "</defaultcodec>\n";
    TMM::ConstIterator m = mm.begin();
    while ( m != mm.end() ) {
	TMMInv inv;
	TMMInv::Iterator i;
	bool contextIsUtf8 = m.key().utf8();
	QCString context = m.key().context();
	QCString comment = "";

	do {
	    if ( QCString(m.key().sourceText()) == ContextComment ) {
		if ( m.key().type() != MetaTranslatorMessage::Obsolete ) {
		    contextIsUtf8 = m.key().utf8();
		    comment = QCString( m.key().comment() );
		}
	    } else {
		inv.insert( *m, m.key() );
	    }
	} while ( ++m != mm.end() && QCString(m.key().context()) == context );

	t << "<context";
	if ( contextIsUtf8 )
	    t << " encoding=\"UTF-8\"";
	t << ">\n";
	t << "    <name>" << evilBytes( context, contextIsUtf8 )
	  << "</name>\n";
	if ( !comment.isEmpty() )
	    t << "    <comment>" << evilBytes( comment, contextIsUtf8 )
	      << "</comment>\n";

	for ( i = inv.begin(); i != inv.end(); ++i ) {
	    // no need for such noise
	    if ( (*i).type() == MetaTranslatorMessage::Obsolete &&
		 (*i).translation().isEmpty() )
		continue;

	    t << "    <message";
	    if ( (*i).utf8() )
		t << " encoding=\"UTF-8\"";
	    t << ">\n"
	      << "        <source>" << evilBytes( (*i).sourceText(),
						  (*i).utf8() )
	      << "</source>\n";
	    if ( !QCString((*i).comment()).isEmpty() )
		t << "        <comment>" << evilBytes( (*i).comment(),
						       (*i).utf8() )
		  << "</comment>\n";
	    t << "        <translation";
	    if ( (*i).type() == MetaTranslatorMessage::Unfinished )
		t << " type=\"unfinished\"";
	    else if ( (*i).type() == MetaTranslatorMessage::Obsolete )
		t << " type=\"obsolete\"";
	    t << ">" << protect( (*i).translation().utf8() )
	      << "</translation>\n";
	    t << "    </message>\n";
	}
	t << "</context>\n";
    }
    t << "</TS>\n";
    f.close();
    return TRUE;
}
コード例 #5
0
ファイル: metatranslator.cpp プロジェクト: nmelchior/Tools
bool MetaTranslator::save( const QString& filename ) const
{
    QFile f( filename );
    if ( !f.open(QIODevice::WriteOnly) )
        return false;

    QTextStream t( &f );
    t.setCodec( QTextCodec::codecForName("ISO-8859-1") );

    //### The xml prolog allows processors to easily detect the correct encoding
    t << "<?xml version=\"1.0\"";
    t << " encoding=\"utf-8\"";
    t << "?>\n<!DOCTYPE TS><TS version=\"1.1\"";
    if (!languageCode().isEmpty() && languageCode() != QLatin1String("C"))
        t << " language=\"" << languageCode() << "\"";
    t << ">\n";
    if ( codecName != "ISO-8859-1" )
        t << "<defaultcodec>" << codecName << "</defaultcodec>\n";
    TMM::ConstIterator m = mm.begin();
    while ( m != mm.end() ) {
        TMMInv inv;
        TMMInv::Iterator i;
        bool contextIsUtf8 = m.key().utf8();
        QByteArray context = m.key().context();
        QByteArray comment = "";

        do {
            if (QByteArray(m.key().sourceText()) == ContextComment) {
                if ( m.key().type() != MetaTranslatorMessage::Obsolete ) {
                    contextIsUtf8 = m.key().utf8();
                    comment = QByteArray(m.key().comment());
                }
            } else {
                inv.insert( *m, m.key() );
            }
        } while ( ++m != mm.end() && QByteArray(m.key().context()) == context );

        if ( inv.isEmpty() )
            continue;

        t << "<context";
        if ( contextIsUtf8 )
            t << " encoding=\"UTF-8\"";
        t << ">\n";
        t << "    <name>" << evilBytes( context, contextIsUtf8 )
          << "</name>\n";
        if ( !comment.isEmpty() )
            t << "    <comment>" << evilBytes( comment, contextIsUtf8 )
              << "</comment>\n";

        for ( i = inv.begin(); i != inv.end(); ++i ) {
            MetaTranslatorMessage msg = *i;
            // no need for such noise
            if ( msg.type() == MetaTranslatorMessage::Obsolete &&
                 msg.translation().isEmpty() )
                continue;

            t << "    <message";
            if ( msg.utf8() )
                t << " encoding=\"UTF-8\"";
            if ( msg.isPlural() )
                t << " numerus=\"yes\"";
            t << ">\n";
            if (!msg.fileName().isEmpty() && msg.lineNumber() >= 0) {
                QDir tsPath = QFileInfo(filename).absoluteDir();
                QString fn = tsPath.relativeFilePath(msg.fileName()).replace('\\','/');
                t << "        <location filename=\"" << fn << "\" line=\"" << msg.lineNumber() << "\"/>\n";
            }
            t  << "        <source>" << evilBytes( msg.sourceText(),
                                                  msg.utf8() )
              << "</source>\n";
            if ( !QByteArray(msg.comment()).isEmpty() )
                t << "        <comment>" << evilBytes( msg.comment(),
                                                       msg.utf8() )
                  << "</comment>\n";
            if ( !QByteArray(msg.translatorComment()).isEmpty() )
                t << "        <translatorcomment>"
                  << evilBytes( msg.translatorComment(), msg.utf8() )
                  << "</translatorcomment>\n";
            t << "        <translation";
            if ( msg.type() == MetaTranslatorMessage::Unfinished )
                t << " type=\"unfinished\"";
            else if ( msg.type() == MetaTranslatorMessage::Obsolete )
                t << " type=\"obsolete\"";
            if (msg.isPlural()) {
                t << ">\n";
                QLocale::Language l;
                QLocale::Country c;
                languageAndCountry(m_language, &l, &c);
                QStringList translns = normalizedTranslations(msg, l, c);
                for (int j = 0; j < qMax(1, translns.count()); ++j) {
                    const QString &transln = translns.value(j);
                    t << "            <numerusform";
                    if ( transln.contains( VARIANT_DELIMITER ) )
                        saveLengthVariants( t, "            ", transln );
                    else
                        t << ">"
                          << protect( transln.toUtf8() ) << "</numerusform>\n";
                }
                t << "        ";
            } else {
                if ( msg.translation().contains( VARIANT_DELIMITER ) )
                    saveLengthVariants( t, "        ", msg.translation() );
                else
                    t << ">"
                      << protect( msg.translation().toUtf8() );
            }
            t << "</translation>\n";
            t << "    </message>\n";
        }
        t << "</context>\n";
    }
    t << "</TS>\n";
    f.close();
    return true;
}
コード例 #6
0
ファイル: xliff.cpp プロジェクト: FilipBE/qtextended
bool MetaTranslator::saveXLIFF( const QString& filename) const
{
    QFile f( filename );
    if ( !f.open(QIODevice::WriteOnly | QIODevice::Text) )
        return false;

    int indent = 2;
    int currentindent = 0;

    QTextStream t( &f );
    t.setCodec( QTextCodec::codecForName("ISO-8859-1") );

    QMap<QString, TranslatorMessage> mtSortByFileName;
    TMM::ConstIterator m = mm.begin();
    while ( m != mm.end() ) {
        TranslatorMessage msg = m.key();
        QString location = msg.fileName() + QLatin1String(msg.context()) + QString::number(msg.lineNumber());
        mtSortByFileName.insertMulti(location, msg);
        ++m;
    }

    t.setFieldAlignment(QTextStream::AlignRight);
    t << "<?xml version=\"1.0\"";
    t << " encoding=\"utf-8\"?>\n";
    t << "<xliff version=\"1.1\" xmlns=\"" << XLIFF11namespaceURI << "\">\n";
    currentindent += indent;
    QMap<QString, TranslatorMessage>::iterator mi = mtSortByFileName.begin();
    TranslatorMessage msg;
    QByteArray ctx;
    QString fn;
    bool ctxdiffer = false;
    bool filediffer = false;
    while (mi != mtSortByFileName.end()) {
        msg = mi.value();
        ctxdiffer = msg.context() != ctx;
        filediffer = msg.fileName() != fn;

        if (ctxdiffer || filediffer) {
            if (!ctx.isEmpty()) {
            writeIndent(&t, currentindent);
                t << "</group>\n";
                currentindent -= indent;
            }
        }

        if (filediffer) {
            if (!fn.isEmpty()) {
                writeIndent(&t, currentindent);
                t << "</body></file>\n";
                currentindent -= indent;
            }
            fn = msg.fileName();

            writeIndent(&t, currentindent);
            t << "<file original=\"" << fn << "\""
                << " datatype=\"" << dataType(msg) << "\""
                << " source-language=\"" << "en" << "\"" //###
                << " target-language=\"" << languageCode() << "\""
                << "><body>\n";
            currentindent += indent;

        }

        if (ctxdiffer || filediffer) {
            ctx = msg.context();
            writeIndent(&t, currentindent);
            t << "<group restype=\"" << restypeContext << "\""
                << " resname=\"" << evilBytes(ctx, msg.utf8()) << "\""
                << ">\n";
            currentindent += indent;
        }

        writeMessage(&t, msg, currentindent, m_language);
        ++mi;
    }
    currentindent-=indent;
    writeIndent(&t, currentindent);
    t << "</group>\n";
    currentindent-=indent;
    writeIndent(&t, currentindent);
    t << "</body></file>\n";
    t << "</xliff>\n";

    f.close();
    return true;
}