tools/linguist/shared/translator.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Linguist of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef METATRANSLATOR_H
       
    43 #define METATRANSLATOR_H
       
    44 
       
    45 #include "translatormessage.h"
       
    46 
       
    47 #include <QDir>
       
    48 #include <QList>
       
    49 #include <QLocale>
       
    50 #include <QMultiHash>
       
    51 #include <QString>
       
    52 #include <QSet>
       
    53 
       
    54 
       
    55 QT_BEGIN_NAMESPACE
       
    56 
       
    57 class QIODevice;
       
    58 
       
    59 // A struct of "interesting" data passed to and from the load and save routines
       
    60 class ConversionData
       
    61 {
       
    62 public:
       
    63     ConversionData() :
       
    64         m_verbose(false),
       
    65         m_ignoreUnfinished(false),
       
    66         m_sortContexts(false),
       
    67         m_noUiLines(false),
       
    68         m_idBased(false),
       
    69         m_saveMode(SaveEverything)
       
    70     {}
       
    71 
       
    72     // tag manipulation
       
    73     const QStringList &dropTags() const { return m_dropTags; }
       
    74     QStringList &dropTags() { return m_dropTags; }
       
    75     const QDir &targetDir() const { return m_targetDir; }
       
    76     bool isVerbose() const { return m_verbose; }
       
    77     bool ignoreUnfinished() const { return m_ignoreUnfinished; }
       
    78     bool sortContexts() const { return m_sortContexts; }
       
    79 
       
    80     void appendError(const QString &error) { m_errors.append(error); }
       
    81     QString error() const { return  m_errors.join(QLatin1String("\n")); }
       
    82     QStringList errors() const { return  m_errors; }
       
    83     void clearErrors() { m_errors.clear(); }
       
    84 
       
    85 public:
       
    86     QString m_defaultContext;
       
    87     QByteArray m_codecForSource; // CPP, PO & QM specific
       
    88     QByteArray m_outputCodec; // PO specific
       
    89     QString m_sourceFileName;
       
    90     QString m_targetFileName;
       
    91     QDir m_sourceDir;
       
    92     QDir m_targetDir; // FIXME: TS specific
       
    93     QSet<QString> m_projectRoots;
       
    94     QMultiHash<QString, QString> m_allCSources;
       
    95     QStringList m_includePath;
       
    96     QStringList m_dropTags;  // tags to be dropped
       
    97     QStringList m_errors;
       
    98     bool m_verbose;
       
    99     bool m_ignoreUnfinished;
       
   100     bool m_sortContexts;
       
   101     bool m_noUiLines;
       
   102     bool m_idBased;
       
   103     TranslatorSaveMode m_saveMode;
       
   104 };
       
   105 
       
   106 class Translator
       
   107 {
       
   108 public:
       
   109     Translator();
       
   110 
       
   111     bool load(const QString &filename, ConversionData &err, const QString &format /*= "auto"*/);
       
   112     bool save(const QString &filename, ConversionData &err, const QString &format /*= "auto"*/) const;
       
   113     bool release(QFile *iod, ConversionData &cd) const;
       
   114 
       
   115     bool contains(const QString &context, const QString &sourceText,
       
   116         const QString &comment) const;
       
   117     TranslatorMessage find(const QString &context,
       
   118         const QString &sourceText, const QString &comment) const;
       
   119 
       
   120     TranslatorMessage find(const QString &context,
       
   121         const QString &comment, const TranslatorMessage::References &refs) const;
       
   122 
       
   123     bool contains(const QString &context) const;
       
   124     TranslatorMessage find(const QString &context) const;
       
   125 
       
   126     void replace(const TranslatorMessage &msg);
       
   127     void replaceSorted(const TranslatorMessage &msg);
       
   128     void extend(const TranslatorMessage &msg); // Only for single-location messages
       
   129     void append(const TranslatorMessage &msg);
       
   130     void appendSorted(const TranslatorMessage &msg);
       
   131 
       
   132     void stripObsoleteMessages();
       
   133     void stripFinishedMessages();
       
   134     void stripEmptyContexts();
       
   135     void stripNonPluralForms();
       
   136     void stripIdenticalSourceTranslations();
       
   137     void dropTranslations();
       
   138     void dropUiLines();
       
   139     void makeFileNamesAbsolute(const QDir &originalPath);
       
   140     QSet<TranslatorMessagePtr> resolveDuplicates();
       
   141     static void reportDuplicates(const QSet<TranslatorMessagePtr> &dupes,
       
   142                                  const QString &fileName, bool verbose);
       
   143 
       
   144     void setCodecName(const QByteArray &name);
       
   145     QByteArray codecName() const { return m_codecName; }
       
   146 
       
   147     QString languageCode() const { return m_language; }
       
   148     QString sourceLanguageCode() const { return m_sourceLanguage; }
       
   149 
       
   150     enum LocationsType { DefaultLocations, NoLocations, RelativeLocations, AbsoluteLocations };
       
   151     void setLocationsType(LocationsType lt) { m_locationsType = lt; }
       
   152     LocationsType locationsType() const { return m_locationsType; }
       
   153 
       
   154     static QString makeLanguageCode(QLocale::Language language, QLocale::Country country);
       
   155     static void languageAndCountry(const QString &languageCode,
       
   156         QLocale::Language *lang, QLocale::Country *country);
       
   157     void setLanguageCode(const QString &languageCode) { m_language = languageCode; }
       
   158     void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; }
       
   159     static QString guessLanguageCodeFromFileName(const QString &fileName);
       
   160     QList<TranslatorMessage> messages() const;
       
   161     QList<TranslatorMessage> translatedMessages() const;
       
   162     static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals);
       
   163     void normalizeTranslations(ConversionData &cd);
       
   164     QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const;
       
   165 
       
   166     int messageCount() const { return m_messages.size(); }
       
   167     TranslatorMessage &message(int i) { return m_messages[i]; }
       
   168     const TranslatorMessage &message(int i) const { return m_messages.at(i); }
       
   169     void dump() const;
       
   170 
       
   171     // additional file format specific data
       
   172     // note: use '<fileformat>:' as prefix for file format specific members,
       
   173     // e.g. "po-flags", "po-msgid_plural"
       
   174     typedef TranslatorMessage::ExtraData ExtraData;
       
   175     QString extra(const QString &ba) const;
       
   176     void setExtra(const QString &ba, const QString &var);
       
   177     bool hasExtra(const QString &ba) const;
       
   178     const ExtraData &extras() const { return m_extra; }
       
   179     void setExtras(const ExtraData &extras) { m_extra = extras; }
       
   180 
       
   181     // registration of file formats
       
   182     typedef bool (*SaveFunction)(const Translator &, QIODevice &out, ConversionData &data);
       
   183     typedef bool (*LoadFunction)(Translator &, QIODevice &in, ConversionData &data);
       
   184     struct FileFormat {
       
   185         FileFormat() : loader(0), saver(0), priority(-1) {}
       
   186         QString extension; // such as "ts", "xlf", ...
       
   187         QString description; // human-readable description
       
   188         LoadFunction loader;
       
   189         SaveFunction saver;
       
   190         enum FileType { TranslationSource, TranslationBinary } fileType;
       
   191         int priority; // 0 = highest, -1 = invisible
       
   192     };
       
   193     static void registerFileFormat(const FileFormat &format);
       
   194     static QList<FileFormat> &registeredFileFormats();
       
   195 
       
   196     enum {
       
   197         TextVariantSeparator = 0x2762, // some weird character nobody ever heard of :-D
       
   198         BinaryVariantSeparator = 0x9c // unicode "STRING TERMINATOR"
       
   199     };
       
   200 
       
   201 private:
       
   202     typedef QList<TranslatorMessage> TMM;       // int stores the sequence position.
       
   203 
       
   204     TMM m_messages;
       
   205     QByteArray m_codecName;
       
   206     LocationsType m_locationsType;
       
   207 
       
   208     // A string beginning with a 2 or 3 letter language code (ISO 639-1
       
   209     // or ISO-639-2), followed by the optional country variant to distinguish
       
   210     //  between country-specific variations of the language. The language code
       
   211     // and country code are always separated by '_'
       
   212     // Note that the language part can also be a 3-letter ISO 639-2 code.
       
   213     // Legal examples:
       
   214     // 'pt'         portuguese, assumes portuguese from portugal
       
   215     // 'pt_BR'      Brazilian portuguese (ISO 639-1 language code)
       
   216     // 'por_BR'     Brazilian portuguese (ISO 639-2 language code)
       
   217     QString m_language;
       
   218     QString m_sourceLanguage;
       
   219     ExtraData m_extra;
       
   220 };
       
   221 
       
   222 bool getNumerusInfo(QLocale::Language language, QLocale::Country country,
       
   223                            QByteArray *rules, QStringList *forms);
       
   224 
       
   225 /*
       
   226   This is a quick hack. The proper way to handle this would be
       
   227   to extend Translator's interface.
       
   228 */
       
   229 #define ContextComment "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT"
       
   230 
       
   231 QT_END_NAMESPACE
       
   232 
       
   233 #endif