| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Linguist of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #ifndef METATRANSLATOR_H |
| 30 | #define METATRANSLATOR_H |
| 31 | |
| 32 | #include "translatormessage.h" |
| 33 | |
| 34 | #include <QCoreApplication> |
| 35 | #include <QDir> |
| 36 | #include <QList> |
| 37 | #include <QLocale> |
| 38 | #include <QMultiHash> |
| 39 | #include <QString> |
| 40 | #include <QSet> |
| 41 | |
| 42 | |
| 43 | QT_BEGIN_NAMESPACE |
| 44 | |
| 45 | class FMT { |
| 46 | Q_DECLARE_TR_FUNCTIONS(Linguist) |
| 47 | }; |
| 48 | |
| 49 | class QIODevice; |
| 50 | |
| 51 | // A struct of "interesting" data passed to and from the load and save routines |
| 52 | class ConversionData |
| 53 | { |
| 54 | public: |
| 55 | ConversionData() : |
| 56 | m_verbose(false), |
| 57 | m_ignoreUnfinished(false), |
| 58 | m_sortContexts(false), |
| 59 | m_noUiLines(false), |
| 60 | m_idBased(false), |
| 61 | m_saveMode(SaveEverything) |
| 62 | {} |
| 63 | |
| 64 | // tag manipulation |
| 65 | const QStringList &dropTags() const { return m_dropTags; } |
| 66 | QStringList &dropTags() { return m_dropTags; } |
| 67 | const QDir &targetDir() const { return m_targetDir; } |
| 68 | bool isVerbose() const { return m_verbose; } |
| 69 | bool ignoreUnfinished() const { return m_ignoreUnfinished; } |
| 70 | bool sortContexts() const { return m_sortContexts; } |
| 71 | |
| 72 | void appendError(const QString &error) { m_errors.append(t: error); } |
| 73 | QString error() const { return m_errors.isEmpty() ? QString() : m_errors.join(sep: QLatin1Char('\n')) + QLatin1Char('\n'); } |
| 74 | QStringList errors() const { return m_errors; } |
| 75 | void clearErrors() { m_errors.clear(); } |
| 76 | |
| 77 | public: |
| 78 | QString m_defaultContext; |
| 79 | bool m_sourceIsUtf16; // CPP & JAVA specific |
| 80 | QString m_unTrPrefix; // QM specific |
| 81 | QString m_sourceFileName; |
| 82 | QString m_targetFileName; |
| 83 | QStringList m_excludes; |
| 84 | QDir m_sourceDir; |
| 85 | QDir m_targetDir; // FIXME: TS specific |
| 86 | QSet<QString> m_projectRoots; |
| 87 | QMultiHash<QString, QString> m_allCSources; |
| 88 | QStringList m_includePath; |
| 89 | QStringList m_dropTags; // tags to be dropped |
| 90 | QStringList m_errors; |
| 91 | bool m_verbose; |
| 92 | bool m_ignoreUnfinished; |
| 93 | bool m_sortContexts; |
| 94 | bool m_noUiLines; |
| 95 | bool m_idBased; |
| 96 | TranslatorSaveMode m_saveMode; |
| 97 | }; |
| 98 | |
| 99 | class TMMKey { |
| 100 | public: |
| 101 | TMMKey(const TranslatorMessage &msg) |
| 102 | { context = msg.context(); source = msg.sourceText(); comment = msg.comment(); } |
| 103 | bool operator==(const TMMKey &o) const |
| 104 | { return context == o.context && source == o.source && comment == o.comment; } |
| 105 | QString context, source, ; |
| 106 | }; |
| 107 | Q_DECLARE_TYPEINFO(TMMKey, Q_MOVABLE_TYPE); |
| 108 | inline uint qHash(const TMMKey &key) { return qHash(key: key.context) ^ qHash(key: key.source) ^ qHash(key: key.comment); } |
| 109 | |
| 110 | class Translator |
| 111 | { |
| 112 | public: |
| 113 | Translator(); |
| 114 | |
| 115 | bool load(const QString &filename, ConversionData &err, const QString &format /* = "auto" */); |
| 116 | bool save(const QString &filename, ConversionData &err, const QString &format /* = "auto" */) const; |
| 117 | |
| 118 | int find(const TranslatorMessage &msg) const; |
| 119 | int find(const QString &context, |
| 120 | const QString &, const TranslatorMessage::References &refs) const; |
| 121 | |
| 122 | int find(const QString &context) const; |
| 123 | |
| 124 | void replaceSorted(const TranslatorMessage &msg); |
| 125 | void extend(const TranslatorMessage &msg, ConversionData &cd); // Only for single-location messages |
| 126 | void append(const TranslatorMessage &msg); |
| 127 | void appendSorted(const TranslatorMessage &msg); |
| 128 | |
| 129 | void stripObsoleteMessages(); |
| 130 | void stripFinishedMessages(); |
| 131 | void stripUntranslatedMessages(); |
| 132 | void stripEmptyContexts(); |
| 133 | void stripNonPluralForms(); |
| 134 | void stripIdenticalSourceTranslations(); |
| 135 | void dropTranslations(); |
| 136 | void dropUiLines(); |
| 137 | void makeFileNamesAbsolute(const QDir &originalPath); |
| 138 | bool translationsExist(); |
| 139 | |
| 140 | struct Duplicates { QSet<int> byId, byContents; }; |
| 141 | Duplicates resolveDuplicates(); |
| 142 | void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose); |
| 143 | |
| 144 | QString languageCode() const { return m_language; } |
| 145 | QString sourceLanguageCode() const { return m_sourceLanguage; } |
| 146 | |
| 147 | enum LocationsType { DefaultLocations, NoLocations, RelativeLocations, AbsoluteLocations }; |
| 148 | void setLocationsType(LocationsType lt) { m_locationsType = lt; } |
| 149 | LocationsType locationsType() const { return m_locationsType; } |
| 150 | |
| 151 | static QString makeLanguageCode(QLocale::Language language, QLocale::Country country); |
| 152 | static void languageAndCountry(const QString &languageCode, |
| 153 | QLocale::Language *lang, QLocale::Country *country); |
| 154 | void setLanguageCode(const QString &languageCode) { m_language = languageCode; } |
| 155 | void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; } |
| 156 | static QString guessLanguageCodeFromFileName(const QString &fileName); |
| 157 | QList<TranslatorMessage> messages() const; |
| 158 | static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals); |
| 159 | void normalizeTranslations(ConversionData &cd); |
| 160 | QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const; |
| 161 | |
| 162 | int messageCount() const { return m_messages.size(); } |
| 163 | TranslatorMessage &message(int i) { return m_messages[i]; } |
| 164 | const TranslatorMessage &message(int i) const { return m_messages.at(i); } |
| 165 | const TranslatorMessage &constMessage(int i) const { return m_messages.at(i); } |
| 166 | void dump() const; |
| 167 | |
| 168 | void setDependencies(const QStringList &dependencies) { m_dependencies = dependencies; } |
| 169 | QStringList dependencies() const { return m_dependencies; } |
| 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 ; |
| 175 | QString (const QString &ba) const; |
| 176 | void (const QString &ba, const QString &var); |
| 177 | bool (const QString &ba) const; |
| 178 | const ExtraData &() const { return m_extra; } |
| 179 | void (const ExtraData &) { 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() : untranslatedDescription(nullptr), loader(0), saver(0), priority(-1) {} |
| 186 | QString extension; // such as "ts", "xlf", ... |
| 187 | const char *untranslatedDescription; |
| 188 | // human-readable description |
| 189 | QString description() const { return FMT::tr(sourceText: untranslatedDescription); } |
| 190 | LoadFunction loader; |
| 191 | SaveFunction saver; |
| 192 | enum FileType { TranslationSource, TranslationBinary } fileType; |
| 193 | int priority; // 0 = highest, -1 = invisible |
| 194 | }; |
| 195 | static void registerFileFormat(const FileFormat &format); |
| 196 | static QList<FileFormat> ®isteredFileFormats(); |
| 197 | |
| 198 | enum { |
| 199 | TextVariantSeparator = 0x2762, // some weird character nobody ever heard of :-D |
| 200 | BinaryVariantSeparator = 0x9c // unicode "STRING TERMINATOR" |
| 201 | }; |
| 202 | |
| 203 | private: |
| 204 | void insert(int idx, const TranslatorMessage &msg); |
| 205 | void addIndex(int idx, const TranslatorMessage &msg) const; |
| 206 | void delIndex(int idx) const; |
| 207 | void ensureIndexed() const; |
| 208 | |
| 209 | typedef QList<TranslatorMessage> TMM; // int stores the sequence position. |
| 210 | |
| 211 | TMM m_messages; |
| 212 | LocationsType m_locationsType; |
| 213 | |
| 214 | // A string beginning with a 2 or 3 letter language code (ISO 639-1 |
| 215 | // or ISO-639-2), followed by the optional country variant to distinguish |
| 216 | // between country-specific variations of the language. The language code |
| 217 | // and country code are always separated by '_' |
| 218 | // Note that the language part can also be a 3-letter ISO 639-2 code. |
| 219 | // Legal examples: |
| 220 | // 'pt' portuguese, assumes portuguese from portugal |
| 221 | // 'pt_BR' Brazilian portuguese (ISO 639-1 language code) |
| 222 | // 'por_BR' Brazilian portuguese (ISO 639-2 language code) |
| 223 | QString m_language; |
| 224 | QString m_sourceLanguage; |
| 225 | QStringList m_dependencies; |
| 226 | ExtraData ; |
| 227 | |
| 228 | mutable bool m_indexOk; |
| 229 | mutable QHash<QString, int> m_ctxCmtIdx; |
| 230 | mutable QHash<QString, int> m_idMsgIdx; |
| 231 | mutable QHash<TMMKey, int> m_msgIdx; |
| 232 | }; |
| 233 | |
| 234 | bool getNumerusInfo(QLocale::Language language, QLocale::Country country, |
| 235 | QByteArray *rules, QStringList *forms, const char **gettextRules); |
| 236 | |
| 237 | QString getNumerusInfoString(); |
| 238 | |
| 239 | bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd); |
| 240 | |
| 241 | /* |
| 242 | This is a quick hack. The proper way to handle this would be |
| 243 | to extend Translator's interface. |
| 244 | */ |
| 245 | #define "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT" |
| 246 | |
| 247 | QT_END_NAMESPACE |
| 248 | |
| 249 | #endif |
| 250 | |