コード例 #1
0
ファイル: xliff.cpp プロジェクト: FilipBE/qtextended
static void writeComment(QTextStream *t, const TranslatorMessage &msg, int indent)
{
    if (!msg.comment().isEmpty()) {
        writeIndent(t, indent);
        (*t) << "<note>" << evilBytes(msg.comment(), msg.utf8()) << "</note>\n";
    }
}
コード例 #2
0
ファイル: messages.cpp プロジェクト: cwarden/quasar
bool
Messages::save(const QString& filePath) const
{
    QFile file(filePath);
    if (!file.open(IO_WriteOnly)) return false;

    QTextStream stream(&file);
    //stream.setCodec(QTextCodec::codecForName("ISO-8859-1"));

    stream << "<!DOCTYPE TS><TS>\n";
    if (_codecName != "ISO-8859-1")
	stream << "<defaultcodec>" << _codecName << "</defaultcodec>\n";

    for (unsigned int i = 0; i < _contexts.size(); ++i) {
	const Context& context = _contexts[i];
	QCString name = context.name.local8Bit();

	stream << "<context>" << endl;
	stream << "  <name>" << evilBytes(name) << "</name>" << endl;

	for (unsigned int j = 0; j < context.messages.size(); ++j) {
	    const Message& message = context.messages[j];
	    stream << "  <message>" << endl;
	    stream << "    <source>" << evilBytes(message.source.local8Bit())
		   << "</source>" << endl;
	    stream << "    <translation type=\"" << message.type
		   << "\">" << evilBytes(message.translation.local8Bit())
		   << "</translation>" << endl;
	    stream << "  </message>" << endl;
	}
	stream << "</context>" << endl;
    }
    stream << "</TS>" << endl;

    return true;
}
コード例 #3
0
ファイル: xliff.cpp プロジェクト: FilipBE/qtextended
static void writeTransUnit(QTextStream *t, const TranslatorMessage &msg, int msgid,
                           int indent, const QString &translation = QString())
{
    static int plural = 0;
    static int prevMsgId = -1;
    writeIndent(t, indent);
    (*t) << "<trans-unit id=\"msg";
    QString strid;
    QByteArray transl;
    if (msg.isPlural()) {
        if (prevMsgId != msgid)
            plural = 0;
        strid = QString::fromAscii("%1[%2]").arg(msgid).arg(plural);
        ++plural;
        transl = translation.toUtf8();
    } else {
        strid = QString::fromAscii("%1").arg(msgid);
        plural = 0;
        transl = msg.translation().toUtf8();
    }
    prevMsgId = msgid;
    (*t) << strid << "\"";
    QString state;
    indent+=2;
    if (msg.type() == TranslatorMessage::Obsolete) {
        (*t) << " translate=\"no\"";
    } else {
        state = msg.type() == TranslatorMessage::Finished
            ? QLatin1String("final") : QLatin1String("new");
        state = QString::fromAscii(" state=\"%1\"").arg(state);
    }
    (*t) << ">\n";
    writeIndent(t, indent);
    (*t) << "<source xml:space=\"preserve\">" << evilBytes(msg.sourceText(), msg.utf8()) << "</source>\n";

    writeIndent(t, indent);
    (*t) << "<target xml:space=\"preserve\"" << state << ">" << evilBytes2(transl, msg.utf8()) << "</target>\n";
    // ### In XLIFF 1.1, name is marked as required, and it must be unique
    // This is questionable behaviour, and was brought up at the xliff-comments mailinglist.
    if (!msg.isPlural()) {
        writeLineNumber(t, msg, indent);
        writeComment(t, msg, indent);
    }
    indent-=2;
    writeIndent(t, indent);
    (*t) << "</trans-unit>\n";
}
コード例 #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;
}