tools/linguist/lupdate/merge.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   245 
   245 
   246     for (u = untranslated.begin(); u != untranslated.end(); ++u) {
   246     for (u = untranslated.begin(); u != untranslated.end(); ++u) {
   247         t = translated.find(zeroKey((*u).sourceText()));
   247         t = translated.find(zeroKey((*u).sourceText()));
   248         if (t != translated.end() && !t.key().isEmpty()
   248         if (t != translated.end() && !t.key().isEmpty()
   249             && t->sourceText() != u->sourceText()) {
   249             && t->sourceText() != u->sourceText()) {
   250             TranslatorMessage m = *u;
   250             u->setTranslation(translationAttempt(t->translation(), t->sourceText(),
   251             m.setTranslation(translationAttempt(t->translation(), t->sourceText(),
   251                                                  u->sourceText()));
   252                                                 u->sourceText()));
       
   253             tor.replace(m);
       
   254             inserted++;
   252             inserted++;
   255         }
   253         }
   256     }
   254     }
   257     return inserted;
   255     return inserted;
   258 }
   256 }
   303 
   301 
   304     for (u = untranslated.begin(); u != untranslated.end(); ++u) {
   302     for (u = untranslated.begin(); u != untranslated.end(); ++u) {
   305         QString key = u->sourceText();
   303         QString key = u->sourceText();
   306         t = translated.find(key);
   304         t = translated.find(key);
   307         if (t != translated.end()) {
   305         if (t != translated.end()) {
   308             TranslatorMessage m = *u;
   306             u->setTranslations(t->translations());
   309             m.setTranslations(t->translations());
       
   310             tor.replace(m);
       
   311             ++inserted;
   307             ++inserted;
   312         }
   308         }
   313     }
   309     }
   314     return inserted;
   310     return inserted;
   315 }
   311 }
   343       are updated according to the virgin translator.
   339       are updated according to the virgin translator.
   344     */
   340     */
   345     foreach (TranslatorMessage m, tor.messages()) {
   341     foreach (TranslatorMessage m, tor.messages()) {
   346         TranslatorMessage::Type newType = TranslatorMessage::Finished;
   342         TranslatorMessage::Type newType = TranslatorMessage::Finished;
   347 
   343 
   348         if (m.sourceText().isEmpty()) {
   344         if (m.sourceText().isEmpty() && m.id().isEmpty()) {
   349             // context/file comment
   345             // context/file comment
   350             TranslatorMessage mv = virginTor.find(m.context());
   346             TranslatorMessage mv = virginTor.find(m.context());
   351             if (!mv.isNull())
   347             if (!mv.isNull())
   352                 m.setComment(mv.comment());
   348                 m.setComment(mv.comment());
   353         } else {
   349         } else {
   354             TranslatorMessage mv = virginTor.find(m.context(), m.sourceText(), m.comment());
   350             TranslatorMessage mv;
   355             if (mv.isNull()) {
   351             int mvi = virginTor.find(m);
       
   352             if (mvi < 0) {
   356                 if (!(options & HeuristicSimilarText)) {
   353                 if (!(options & HeuristicSimilarText)) {
       
   354                   makeObsolete:
   357                     newType = TranslatorMessage::Obsolete;
   355                     newType = TranslatorMessage::Obsolete;
   358                     if (m.type() != TranslatorMessage::Obsolete)
   356                     if (m.type() != TranslatorMessage::Obsolete)
   359                         obsoleted++;
   357                         obsoleted++;
   360                     m.clearReferences();
   358                     m.clearReferences();
   361                 } else {
   359                 } else {
   362                     mv = virginTor.find(m.context(), m.comment(), m.allReferences());
   360                     mv = virginTor.find(m.context(), m.comment(), m.allReferences());
   363                     if (mv.isNull()) {
   361                     if (mv.isNull()) {
   364                         // did not find it in the virgin, mark it as obsolete
   362                         // did not find it in the virgin, mark it as obsolete
   365                         newType = TranslatorMessage::Obsolete;
   363                         goto makeObsolete;
   366                         if (m.type() != TranslatorMessage::Obsolete)
       
   367                             obsoleted++;
       
   368                         m.clearReferences();
       
   369                     } else {
   364                     } else {
   370                         // Do not just accept it if its on the same line number,
   365                         // Do not just accept it if its on the same line number,
   371                         // but different source text.
   366                         // but different source text.
   372                         // Also check if the texts are more or less similar before
   367                         // Also check if the texts are more or less similar before
   373                         // we consider them to represent the same message...
   368                         // we consider them to represent the same message...
   378                             // was changed it might require re-translating...)
   373                             // was changed it might require re-translating...)
   379                             newType = TranslatorMessage::Unfinished;
   374                             newType = TranslatorMessage::Unfinished;
   380                             ++similarTextHeuristicCount;
   375                             ++similarTextHeuristicCount;
   381                             neww++;
   376                             neww++;
   382 
   377 
       
   378                           outdateSource:
   383                             m.setOldSourceText(m.sourceText());
   379                             m.setOldSourceText(m.sourceText());
   384                             m.setSourceText(mv.sourceText());
   380                             m.setSourceText(mv.sourceText());
   385                             const QString &oldpluralsource = m.extra(QLatin1String("po-msgid_plural"));
   381                             const QString &oldpluralsource = m.extra(QLatin1String("po-msgid_plural"));
   386                             if (!oldpluralsource.isEmpty()) {
   382                             if (!oldpluralsource.isEmpty()) {
   387                                 m.setExtra(QLatin1String("po-old_msgid_plural"), oldpluralsource);
   383                                 m.setExtra(QLatin1String("po-old_msgid_plural"), oldpluralsource);
   388                                 m.unsetExtra(QLatin1String("po-msgid_plural"));
   384                                 m.unsetExtra(QLatin1String("po-msgid_plural"));
   389                             }
   385                             }
   390                             m.setReferences(mv.allReferences()); // Update secondary references
   386                             goto copyAttribs; // Update secondary references
   391                             m.setPlural(mv.isPlural());
       
   392                             m.setUtf8(mv.isUtf8());
       
   393                             m.setExtraComment(mv.extraComment());
       
   394                         } else {
   387                         } else {
   395                             // The virgin and vernacular sourceTexts are so
   388                             // The virgin and vernacular sourceTexts are so
   396                             // different that we could not find it.
   389                             // different that we could not find it.
   397                             newType = TranslatorMessage::Obsolete;
   390                             goto makeObsolete;
   398                             if (m.type() != TranslatorMessage::Obsolete)
       
   399                                 obsoleted++;
       
   400                             m.clearReferences();
       
   401                         }
   391                         }
   402                     }
   392                     }
   403                 }
   393                 }
   404             } else {
   394             } else {
   405                 switch (m.type()) {
   395                 mv = virginTor.message(mvi);
   406                 case TranslatorMessage::Finished:
   396                 if (!mv.id().isEmpty()
   407                 default:
   397                     && (mv.context() != m.context()
   408                     if (m.isPlural() == mv.isPlural()) {
   398                         || mv.sourceText() != m.sourceText()
   409                         newType = TranslatorMessage::Finished;
   399                         || mv.comment() != m.comment())) {
   410                     } else {
   400                     known++;
       
   401                     newType = TranslatorMessage::Unfinished;
       
   402                     m.setContext(mv.context());
       
   403                     m.setComment(mv.comment());
       
   404                     if (mv.sourceText() != m.sourceText())
       
   405                         goto outdateSource;
       
   406                 } else {
       
   407                     switch (m.type()) {
       
   408                     case TranslatorMessage::Finished:
       
   409                     default:
       
   410                         if (m.isPlural() == mv.isPlural()) {
       
   411                             newType = TranslatorMessage::Finished;
       
   412                         } else {
       
   413                             newType = TranslatorMessage::Unfinished;
       
   414                         }
       
   415                         known++;
       
   416                         break;
       
   417                     case TranslatorMessage::Unfinished:
   411                         newType = TranslatorMessage::Unfinished;
   418                         newType = TranslatorMessage::Unfinished;
       
   419                         known++;
       
   420                         break;
       
   421                     case TranslatorMessage::Obsolete:
       
   422                         newType = TranslatorMessage::Unfinished;
       
   423                         neww++;
   412                     }
   424                     }
   413                     known++;
       
   414                     break;
       
   415                 case TranslatorMessage::Unfinished:
       
   416                     newType = TranslatorMessage::Unfinished;
       
   417                     known++;
       
   418                     break;
       
   419                 case TranslatorMessage::Obsolete:
       
   420                     newType = TranslatorMessage::Unfinished;
       
   421                     neww++;
       
   422                 }
   425                 }
   423 
   426 
   424                 // Always get the filename and linenumber info from the
   427                 // Always get the filename and linenumber info from the
   425                 // virgin Translator, in case it has changed location.
   428                 // virgin Translator, in case it has changed location.
   426                 // This should also enable us to read a file that does not
   429                 // This should also enable us to read a file that does not
   427                 // have the <location> element.
   430                 // have the <location> element.
   428                 // why not use operator=()? Because it overwrites e.g. userData.
   431                 // why not use operator=()? Because it overwrites e.g. userData.
       
   432               copyAttribs:
   429                 m.setReferences(mv.allReferences());
   433                 m.setReferences(mv.allReferences());
   430                 m.setPlural(mv.isPlural());
   434                 m.setPlural(mv.isPlural());
   431                 m.setUtf8(mv.isUtf8());
   435                 m.setUtf8(mv.isUtf8());
   432                 m.setExtraComment(mv.extraComment());
   436                 m.setExtraComment(mv.extraComment());
       
   437                 m.setId(mv.id());
   433             }
   438             }
   434         }
   439         }
   435 
   440 
   436         m.setType(newType);
   441         m.setType(newType);
   437         outTor.append(m);
   442         outTor.append(m);
   440     /*
   445     /*
   441       Messages found only in the virgin translator are added to the
   446       Messages found only in the virgin translator are added to the
   442       vernacular translator.
   447       vernacular translator.
   443     */
   448     */
   444     foreach (const TranslatorMessage &mv, virginTor.messages()) {
   449     foreach (const TranslatorMessage &mv, virginTor.messages()) {
   445         if (mv.sourceText().isEmpty()) {
   450         if (mv.sourceText().isEmpty() && mv.id().isEmpty()) {
   446             if (tor.contains(mv.context()))
   451             if (tor.contains(mv.context()))
   447                 continue;
   452                 continue;
   448         } else {
   453         } else {
   449             if (tor.contains(mv.context(), mv.sourceText(), mv.comment()))
   454             if (tor.find(mv) >= 0)
   450                 continue;
   455                 continue;
   451             if (options & HeuristicSimilarText) {
   456             if (options & HeuristicSimilarText) {
   452                 TranslatorMessage m = tor.find(mv.context(), mv.comment(), mv.allReferences());
   457                 TranslatorMessage m = tor.find(mv.context(), mv.comment(), mv.allReferences());
   453                 if (!m.isNull()) {
   458                 if (!m.isNull()) {
   454                     if (getSimilarityScore(m.sourceText(), mv.sourceText()) >= textSimilarityThreshold)
   459                     if (getSimilarityScore(m.sourceText(), mv.sourceText()) >= textSimilarityThreshold)
   458         }
   463         }
   459         if (options & NoLocations)
   464         if (options & NoLocations)
   460             outTor.append(mv);
   465             outTor.append(mv);
   461         else
   466         else
   462             outTor.appendSorted(mv);
   467             outTor.appendSorted(mv);
   463         if (!mv.sourceText().isEmpty())
   468         if (!mv.sourceText().isEmpty() || !mv.id().isEmpty())
   464             ++neww;
   469             ++neww;
   465     }
   470     }
   466 
   471 
   467     /*
   472     /*
   468       The same-text heuristic handles cases where a message has an
   473       The same-text heuristic handles cases where a message has an