Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tools/linguist/shared/qm.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the Qt Linguist of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    4242#include "translator.h"
    4343
     44#ifndef QT_BOOTSTRAPPED
    4445#include <QtCore/QCoreApplication>
     46#endif
    4547#include <QtCore/QDebug>
    4648#include <QtCore/QDir>
     
    173175    bool save(QIODevice *iod);
    174176
    175     void insert(const TranslatorMessage &msg, bool forceComment);
     177    void insert(const TranslatorMessage &msg, const QStringList &tlns, bool forceComment);
     178    void insertIdBased(const TranslatorMessage &message, const QStringList &tlns);
    176179
    177180    void squeeze(TranslatorSaveMode mode);
     
    186189    QByteArray originalBytes(const QString &str, bool isUtf8) const;
    187190
    188     void insertInternal(const TranslatorMessage &message, bool forceComment, bool isUtf8);
     191    void insertInternal(const TranslatorMessage &message, const QStringList &tlns,
     192                        bool forceComment, bool isUtf8);
    189193
    190194    static Prefix commonPrefix(const ByteTranslatorMessage &m1, const ByteTranslatorMessage &m2);
     
    239243    TranslatorSaveMode mode, Prefix prefix) const
    240244{
    241     for (int i = 0; i < msg.translations().count(); ++i) {
    242         QString str = msg.translations().at(i);
    243         str.replace(QChar(Translator::DefaultVariantSeparator),
    244                     QChar(Translator::InternalVariantSeparator));
    245         stream << quint8(Tag_Translation) << str;
    246     }
     245    for (int i = 0; i < msg.translations().count(); ++i)
     246        stream << quint8(Tag_Translation) << msg.translations().at(i);
    247247
    248248    if (mode == SaveEverything)
    249249        prefix = HashContextSourceTextComment;
    250250
    251     // lrelease produces "wrong" .qm files for QByteArrays that are .isNull().
     251    // lrelease produces "wrong" QM files for QByteArrays that are .isNull().
    252252    switch (prefix) {
    253253    default:
     
    417417}
    418418
    419 void Releaser::insertInternal(const TranslatorMessage &message, bool forceComment, bool isUtf8)
     419void Releaser::insertInternal(const TranslatorMessage &message, const QStringList &tlns,
     420                              bool forceComment, bool isUtf8)
    420421{
    421422    ByteTranslatorMessage bmsg(originalBytes(message.context(), isUtf8),
    422423                               originalBytes(message.sourceText(), isUtf8),
    423424                               originalBytes(message.comment(), isUtf8),
    424                                message.translations());
     425                               tlns);
    425426    if (!forceComment) {
    426427        ByteTranslatorMessage bmsg2(
     
    434435}
    435436
    436 void Releaser::insert(const TranslatorMessage &message, bool forceComment)
    437 {
    438     insertInternal(message, forceComment, message.isUtf8());
     437void Releaser::insert(const TranslatorMessage &message, const QStringList &tlns, bool forceComment)
     438{
     439    insertInternal(message, tlns, forceComment, message.isUtf8());
    439440    if (message.isUtf8() && message.isNonUtf8())
    440         insertInternal(message, forceComment, false);
     441        insertInternal(message, tlns, forceComment, false);
     442}
     443
     444void Releaser::insertIdBased(const TranslatorMessage &message, const QStringList &tlns)
     445{
     446    ByteTranslatorMessage bmsg("", originalBytes(message.id(), false), "", tlns);
     447    m_messages.insert(bmsg, 0);
    441448}
    442449
     
    546553    //qDebug() << "NUMITEMS: " << numItems;
    547554
    548     // FIXME: that's just a guess, the original locale data is lost...
    549     QTextCodec *codec = QTextCodec::codecForLocale();
     555    QTextCodec *codec = QTextCodec::codecForName(
     556        cd.m_codecForSource.isEmpty() ? QByteArray("Latin1") : cd.m_codecForSource);
    550557    QTextCodec *utf8Codec = 0;
    551558    if (codec->name() != "UTF-8")
    552559        utf8Codec = QTextCodec::codecForName("UTF-8");
     560
     561    QString strProN = QLatin1String("%n");
     562    QLocale::Language l;
     563    QLocale::Country c;
     564    Translator::languageAndCountry(translator.languageCode(), &l, &c);
     565    QStringList numerusForms;
     566    bool guessPlurals = true;
     567    if (getNumerusInfo(l, c, 0, &numerusForms))
     568        guessPlurals = (numerusForms.count() == 1);
    553569
    554570    QString context, contextUtf8;
     
    585601                            ((str.at(i).unicode() << 8) & 0xff00));
    586602                }
    587                 str.replace(QChar(Translator::InternalVariantSeparator),
    588                             QChar(Translator::DefaultVariantSeparator));
    589603                translations << str;
    590604                m += len;
     
    636650        TranslatorMessage msg;
    637651        msg.setType(TranslatorMessage::Finished);
     652        if (translations.count() > 1) {
     653            // If guessPlurals is not false here, plural form discard messages
     654            // will be spewn out later.
     655            msg.setPlural(true);
     656        } else if (guessPlurals) {
     657            // This might cause false positives, so it is a fallback only.
     658            if (sourcetext.contains(strProN))
     659                msg.setPlural(true);
     660        }
    638661        msg.setTranslations(translations);
    639662        translations.clear();
     
    650673            if (!(contextIsSystem && sourcetextIsSystem && commentIsSystem)) {
    651674                cd.appendError(QLatin1String(
    652                         "Cannot read file with current system character codec"));
     675                        "Cannot read file with specified input codec"));
    653676                return false;
    654677            }
     
    664687
    665688
     689
     690static bool containsStripped(const Translator &translator, const TranslatorMessage &msg)
     691{
     692    foreach (const TranslatorMessage &tmsg, translator.messages())
     693        if (tmsg.sourceText() == msg.sourceText()
     694            && tmsg.context() == msg.context()
     695            && tmsg.comment().isEmpty())
     696        return true;
     697    return false;
     698}
    666699
    667700static bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd)
     
    679712    int unfinished = 0;
    680713    int untranslated = 0;
     714    int missingIds = 0;
     715    int droppedData = 0;
    681716
    682717    for (int i = 0; i != translator.messageCount(); ++i) {
     
    684719        TranslatorMessage::Type typ = msg.type();
    685720        if (typ != TranslatorMessage::Obsolete) {
     721            if (cd.m_idBased && msg.id().isEmpty()) {
     722                ++missingIds;
     723                continue;
     724            }
    686725            if (typ == TranslatorMessage::Unfinished) {
    687                 if (msg.translation().isEmpty()) {
     726                if (!cd.m_idBased && msg.translation().isEmpty()) {
    688727                    ++untranslated;
    689728                    continue;
     
    696735                ++finished;
    697736            }
    698             // Drop the comment in (context, sourceText, comment),
    699             // unless the context is empty,
    700             // unless (context, sourceText, "") already exists or
    701             // unless we already dropped the comment of (context,
    702             // sourceText, comment0).
    703             bool forceComment =
    704                     msg.comment().isEmpty()
    705                     || msg.context().isEmpty()
    706                     || translator.contains(msg.context(), msg.sourceText(), QString());
    707             releaser.insert(msg, forceComment);
    708         }
    709     }
     737            QStringList tlns = msg.translations();
     738            if (msg.type() == TranslatorMessage::Unfinished
     739                && (cd.m_idBased || !cd.m_unTrPrefix.isEmpty()))
     740                for (int j = 0; j < tlns.size(); ++j)
     741                    if (tlns.at(j).isEmpty())
     742                        tlns[j] = cd.m_unTrPrefix + msg.sourceText();
     743            if (cd.m_idBased) {
     744                if (!msg.context().isEmpty() || !msg.comment().isEmpty())
     745                    ++droppedData;
     746                releaser.insertIdBased(msg, tlns);
     747            } else {
     748                // Drop the comment in (context, sourceText, comment),
     749                // unless the context is empty,
     750                // unless (context, sourceText, "") already exists or
     751                // unless we already dropped the comment of (context,
     752                // sourceText, comment0).
     753                bool forceComment =
     754                        msg.comment().isEmpty()
     755                        || msg.context().isEmpty()
     756                        || containsStripped(translator, msg);
     757                releaser.insert(msg, tlns, forceComment);
     758            }
     759        }
     760    }
     761
     762    if (missingIds)
     763        cd.appendError(QCoreApplication::translate("LRelease",
     764            "Dropped %n message(s) which had no ID.", 0,
     765            QCoreApplication::CodecForTr, missingIds));
     766    if (droppedData)
     767        cd.appendError(QCoreApplication::translate("LRelease",
     768            "Excess context/disambiguation dropped from %n message(s).", 0,
     769            QCoreApplication::CodecForTr, droppedData));
    710770
    711771    releaser.squeeze(cd.m_saveMode);
Note: See TracChangeset for help on using the changeset viewer.