| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #ifndef METATRANSLATOR_H |
| 5 | #define METATRANSLATOR_H |
| 6 | |
| 7 | #include "translatormessage.h" |
| 8 | #include "fmt.h" |
| 9 | |
| 10 | #include <QCoreApplication> |
| 11 | #include <QDir> |
| 12 | #include <QList> |
| 13 | #include <QLocale> |
| 14 | #include <QMultiHash> |
| 15 | #include <QRegularExpression> |
| 16 | #include <QString> |
| 17 | #include <QSet> |
| 18 | #include <QVector> |
| 19 | |
| 20 | QT_BEGIN_NAMESPACE |
| 21 | |
| 22 | class QIODevice; |
| 23 | |
| 24 | // A struct of "interesting" data passed to and from the load and save routines |
| 25 | class ConversionData |
| 26 | { |
| 27 | public: |
| 28 | // tag manipulation |
| 29 | const QStringList &dropTags() const { return m_dropTags; } |
| 30 | QStringList &dropTags() { return m_dropTags; } |
| 31 | const QDir &targetDir() const { return m_targetDir; } |
| 32 | bool isVerbose() const { return m_verbose; } |
| 33 | bool ignoreUnfinished() const { return m_ignoreUnfinished; } |
| 34 | bool sortContexts() const { return m_sortContexts; } |
| 35 | bool sortMessages() const { return m_sortMessages; } |
| 36 | |
| 37 | void appendError(const QString &error) { m_errors.append(t: error); } |
| 38 | QString error() const { return m_errors.isEmpty() ? QString() : m_errors.join(sep: u'\n') + u'\n'; } |
| 39 | QStringList errors() const { return m_errors; } |
| 40 | void clearErrors() { m_errors.clear(); } |
| 41 | |
| 42 | public: |
| 43 | QString m_defaultContext; |
| 44 | bool m_sourceIsUtf16; // CPP & JAVA specific |
| 45 | QString m_unTrPrefix; // QM specific |
| 46 | QString m_sourceFileName; |
| 47 | QString m_targetFileName; |
| 48 | QString m_compilationDatabaseDir; |
| 49 | QVector<QRegularExpression> m_excludes; |
| 50 | QDir m_sourceDir; |
| 51 | QDir m_targetDir; // FIXME: TS specific |
| 52 | QSet<QString> m_projectRoots; |
| 53 | QMultiHash<QString, QString> m_allCSources; |
| 54 | QStringList m_includePath; |
| 55 | QStringList m_dropTags; // tags to be dropped |
| 56 | QStringList m_errors; |
| 57 | bool m_verbose = false; |
| 58 | bool m_ignoreUnfinished = false; |
| 59 | bool m_sortContexts = false; |
| 60 | bool m_sortMessages = false; |
| 61 | bool m_noUiLines = false; |
| 62 | TranslatorSaveMode m_saveMode = SaveEverything; |
| 63 | QStringList m_rootDirs; |
| 64 | }; |
| 65 | |
| 66 | class TMMKey { |
| 67 | public: |
| 68 | TMMKey(const TranslatorMessage &msg) |
| 69 | { context = msg.context(); source = msg.sourceText(); comment = msg.comment(); } |
| 70 | bool operator==(const TMMKey &o) const |
| 71 | { return context == o.context && source == o.source && comment == o.comment; } |
| 72 | QString context, source, ; |
| 73 | }; |
| 74 | Q_DECLARE_TYPEINFO(TMMKey, Q_RELOCATABLE_TYPE); |
| 75 | inline size_t qHash(const TMMKey &key) |
| 76 | { |
| 77 | return qHash(key: key.context) ^ qHash(key: key.source) ^ qHash(key: key.comment); |
| 78 | } |
| 79 | |
| 80 | class Translator |
| 81 | { |
| 82 | public: |
| 83 | Translator(); |
| 84 | |
| 85 | bool load(const QString &filename, ConversionData &err, const QString &format /* = "auto" */); |
| 86 | bool save(const QString &filename, ConversionData &err, const QString &format /* = "auto" */) const; |
| 87 | |
| 88 | int find(const TranslatorMessage &msg) const; |
| 89 | int find(const QString &context, |
| 90 | const QString &, const TranslatorMessage::References &refs) const; |
| 91 | |
| 92 | int find(const QString &context) const; |
| 93 | |
| 94 | void replaceSorted(const TranslatorMessage &msg); |
| 95 | void extend(const TranslatorMessage &msg, ConversionData &cd); // Only for single-location messages |
| 96 | void append(const TranslatorMessage &msg); |
| 97 | void appendSorted(const TranslatorMessage &msg); |
| 98 | |
| 99 | void stripObsoleteMessages(); |
| 100 | void stripFinishedMessages(); |
| 101 | void stripUntranslatedMessages(); |
| 102 | void stripEmptyContexts(); |
| 103 | void stripNonPluralForms(); |
| 104 | void stripIdenticalSourceTranslations(); |
| 105 | void dropTranslations(); |
| 106 | void dropUiLines(); |
| 107 | void makeFileNamesAbsolute(const QDir &originalPath); |
| 108 | bool translationsExist() const; |
| 109 | bool unfinishedTranslationsExist() const; |
| 110 | |
| 111 | using DuplicateEntries = QHash<int, QVector<int>>; |
| 112 | struct Duplicates |
| 113 | { |
| 114 | DuplicateEntries byId, byContents; |
| 115 | }; |
| 116 | Duplicates resolveDuplicates(); |
| 117 | void reportDuplicates(const Duplicates &dupes, const QString &fileName, bool verbose); |
| 118 | void reportDuplicatesLines(const TranslatorMessage &msg, |
| 119 | const DuplicateEntries::value_type &dups) const; |
| 120 | |
| 121 | QString languageCode() const { return m_language; } |
| 122 | QString sourceLanguageCode() const { return m_sourceLanguage; } |
| 123 | |
| 124 | enum LocationsType { DefaultLocations, NoLocations, RelativeLocations, AbsoluteLocations }; |
| 125 | void setLocationsType(LocationsType lt) { m_locationsType = lt; } |
| 126 | LocationsType locationsType() const { return m_locationsType; } |
| 127 | |
| 128 | static QString makeLanguageCode(QLocale::Language language, QLocale::Territory territory); |
| 129 | static void languageAndTerritory(QStringView languageCode, QLocale::Language *langPtr, |
| 130 | QLocale::Territory *territoryPtr); |
| 131 | void setLanguageCode(const QString &languageCode) { m_language = languageCode; } |
| 132 | void setSourceLanguageCode(const QString &languageCode) { m_sourceLanguage = languageCode; } |
| 133 | static QString guessLanguageCodeFromFileName(const QString &fileName); |
| 134 | const QList<TranslatorMessage> &messages() const; |
| 135 | static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals); |
| 136 | void normalizeTranslations(ConversionData &cd); |
| 137 | QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const; |
| 138 | |
| 139 | int messageCount() const { return m_messages.size(); } |
| 140 | TranslatorMessage &message(int i) { return m_messages[i]; } |
| 141 | const TranslatorMessage &message(int i) const { return m_messages.at(i); } |
| 142 | const TranslatorMessage &constMessage(int i) const { return m_messages.at(i); } |
| 143 | void dump() const; |
| 144 | |
| 145 | void appendDependencies(const QStringList &dependencies); |
| 146 | void setDependencies(const QStringList &dependencies) { m_dependencies = dependencies; } |
| 147 | QStringList dependencies() const { return m_dependencies; } |
| 148 | void satisfyDependency(const QString &file, const QString &format); |
| 149 | |
| 150 | // additional file format specific data |
| 151 | // note: use '<fileformat>:' as prefix for file format specific members, |
| 152 | // e.g. "po-flags", "po-msgid_plural" |
| 153 | typedef TranslatorMessage::ExtraData ; |
| 154 | QString (const QString &ba) const; |
| 155 | void (const QString &ba, const QString &var); |
| 156 | bool (const QString &ba) const; |
| 157 | const ExtraData &() const { return m_extra; } |
| 158 | void (const ExtraData &) { m_extra = extras; } |
| 159 | |
| 160 | // registration of file formats |
| 161 | typedef bool (*SaveFunction)(const Translator &, QIODevice &out, ConversionData &data); |
| 162 | typedef bool (*LoadFunction)(Translator &, QIODevice &in, ConversionData &data); |
| 163 | struct FileFormat { |
| 164 | FileFormat() : untranslatedDescription(nullptr), loader(0), saver(0), priority(-1) {} |
| 165 | QString extension; // such as "ts", "xlf", ... |
| 166 | const char *untranslatedDescription; |
| 167 | // human-readable description |
| 168 | QString description() const { return FMT::tr(sourceText: untranslatedDescription); } |
| 169 | LoadFunction loader; |
| 170 | SaveFunction saver; |
| 171 | enum FileType { TranslationSource, TranslationBinary } fileType; |
| 172 | int priority; // 0 = highest, -1 = invisible |
| 173 | }; |
| 174 | static void registerFileFormat(const FileFormat &format); |
| 175 | static QList<FileFormat> ®isteredFileFormats(); |
| 176 | |
| 177 | static constexpr QChar TextVariantSeparator{0x2762}; // some weird character nobody ever heard of :-D |
| 178 | static constexpr QChar BinaryVariantSeparator{0x9c}; // unicode "STRING TERMINATOR" |
| 179 | |
| 180 | private: |
| 181 | void insert(int idx, const TranslatorMessage &msg); |
| 182 | void addIndex(int idx, const TranslatorMessage &msg) const; |
| 183 | void delIndex(int idx) const; |
| 184 | void ensureIndexed() const; |
| 185 | |
| 186 | typedef QList<TranslatorMessage> TMM; // int stores the sequence position. |
| 187 | |
| 188 | TMM m_messages; |
| 189 | LocationsType m_locationsType; |
| 190 | |
| 191 | // A string beginning with a 2 or 3 letter language code (ISO 639-1 |
| 192 | // or ISO-639-2), followed by the optional territory variant to distinguish |
| 193 | // between territory-specific variations of the language. The language code |
| 194 | // and territory code are always separated by '_' |
| 195 | // Note that the language part can also be a 3-letter ISO 639-2 code. |
| 196 | // Legal examples: |
| 197 | // 'pt' portuguese, assumes portuguese from portugal |
| 198 | // 'pt_BR' Brazilian portuguese (ISO 639-1 language code) |
| 199 | // 'por_BR' Brazilian portuguese (ISO 639-2 language code) |
| 200 | QString m_language; |
| 201 | QString m_sourceLanguage; |
| 202 | QStringList m_dependencies; |
| 203 | ExtraData ; |
| 204 | |
| 205 | mutable bool m_indexOk; |
| 206 | mutable QHash<QString, int> m_idMsgIdx; |
| 207 | mutable QHash<TMMKey, int> m_msgIdx; |
| 208 | }; |
| 209 | |
| 210 | bool getNumerusInfo(QLocale::Language language, QLocale::Territory territory, QByteArray *rules, |
| 211 | QStringList *forms, const char **gettextRules); |
| 212 | |
| 213 | QString getNumerusInfoString(); |
| 214 | |
| 215 | bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd); |
| 216 | |
| 217 | /* |
| 218 | This is a quick hack. The proper way to handle this would be |
| 219 | to extend Translator's interface. |
| 220 | */ |
| 221 | #define "QT_LINGUIST_INTERNAL_CONTEXT_COMMENT" |
| 222 | |
| 223 | QT_END_NAMESPACE |
| 224 | |
| 225 | #endif |
| 226 | |