static QString makeMsgId(const TranslatorMessage &msg) { QString id = msg.context() + QLatin1String("//") + elidedId(msg.sourceText(), 100); if (!msg.comment().isEmpty()) id += QLatin1String("//") + elidedId(msg.comment(), 30); return id; }
void Translator::addIndex(int idx, const TranslatorMessage &msg) const { if (msg.sourceText().isEmpty() && msg.id().isEmpty()) { m_ctxCmtIdx[msg.context()] = idx; } else { m_msgIdx[TMMKey(msg)] = idx; if (!msg.id().isEmpty()) m_idMsgIdx[msg.id()] = idx; } }
void Translator::appendSorted(const TranslatorMessage &msg) { int msgLine = msg.lineNumber(); if (msgLine < 0) { append(msg); return; } int bestIdx = 0; // Best insertion point found so far int bestScore = 0; // Its category: 0 = no hit, 1 = pre or post, 2 = middle int bestSize = 0; // The length of the region. Longer is better within one category. // The insertion point to use should this region turn out to be the best one so far int thisIdx = 0; int thisScore = 0; int thisSize = 0; // Working vars int prevLine = 0; int curIdx = 0; foreach (const TranslatorMessage &mit, m_messages) { bool sameFile = mit.fileName() == msg.fileName() && mit.context() == msg.context(); int curLine; if (sameFile && (curLine = mit.lineNumber()) >= prevLine) { if (msgLine >= prevLine && msgLine < curLine) { thisIdx = curIdx; thisScore = thisSize ? 2 : 1; } ++thisSize; prevLine = curLine; } else { if (thisSize) { if (!thisScore) { thisIdx = curIdx; thisScore = 1; } if (thisScore > bestScore || (thisScore == bestScore && thisSize > bestSize)) { bestIdx = thisIdx; bestScore = thisScore; bestSize = thisSize; } thisScore = 0; thisSize = sameFile ? 1 : 0; prevLine = 0; } } ++curIdx; }
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; }