| 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 | #include "translator.h" |
| 5 | |
| 6 | #ifndef QT_BOOTSTRAPPED |
| 7 | #include <QtCore/QCoreApplication> |
| 8 | #endif |
| 9 | #include <QtCore/QDataStream> |
| 10 | #include <QtCore/QDebug> |
| 11 | #include <QtCore/QDir> |
| 12 | #include <QtCore/QFile> |
| 13 | #include <QtCore/QFileInfo> |
| 14 | #include <QtCore/QMap> |
| 15 | #include <QtCore/QString> |
| 16 | #include <QtCore/QStringDecoder> |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | using namespace Qt::Literals::StringLiterals; |
| 21 | |
| 22 | // magic number for the file |
| 23 | static const int MagicLength = 16; |
| 24 | static const uchar magic[MagicLength] = { |
| 25 | 0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95, |
| 26 | 0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | enum Tag { |
| 33 | Tag_End = 1, |
| 34 | Tag_SourceText16 = 2, |
| 35 | Tag_Translation = 3, |
| 36 | Tag_Context16 = 4, |
| 37 | Tag_Obsolete1 = 5, |
| 38 | Tag_SourceText = 6, |
| 39 | Tag_Context = 7, |
| 40 | = 8, |
| 41 | Tag_Obsolete2 = 9 |
| 42 | }; |
| 43 | |
| 44 | enum Prefix { |
| 45 | NoPrefix, |
| 46 | Hash, |
| 47 | HashContext, |
| 48 | HashContextSourceText, |
| 49 | |
| 50 | }; |
| 51 | |
| 52 | } // namespace anon |
| 53 | |
| 54 | static uint elfHash(const QByteArray &ba) |
| 55 | { |
| 56 | const uchar *k = (const uchar *)ba.data(); |
| 57 | uint h = 0; |
| 58 | uint g; |
| 59 | |
| 60 | if (k) { |
| 61 | while (*k) { |
| 62 | h = (h << 4) + *k++; |
| 63 | if ((g = (h & 0xf0000000)) != 0) |
| 64 | h ^= g >> 24; |
| 65 | h &= ~g; |
| 66 | } |
| 67 | } |
| 68 | if (!h) |
| 69 | h = 1; |
| 70 | return h; |
| 71 | } |
| 72 | |
| 73 | class ByteTranslatorMessage |
| 74 | { |
| 75 | public: |
| 76 | ByteTranslatorMessage( |
| 77 | const QByteArray &context, |
| 78 | const QByteArray &sourceText, |
| 79 | const QByteArray &, |
| 80 | const QStringList &translations) : |
| 81 | m_context(context), |
| 82 | m_sourcetext(sourceText), |
| 83 | m_comment(comment), |
| 84 | m_translations(translations) |
| 85 | {} |
| 86 | const QByteArray &context() const { return m_context; } |
| 87 | const QByteArray &sourceText() const { return m_sourcetext; } |
| 88 | const QByteArray &() const { return m_comment; } |
| 89 | const QStringList &translations() const { return m_translations; } |
| 90 | bool operator<(const ByteTranslatorMessage& m) const; |
| 91 | |
| 92 | private: |
| 93 | QByteArray m_context; |
| 94 | QByteArray m_sourcetext; |
| 95 | QByteArray ; |
| 96 | QStringList m_translations; |
| 97 | }; |
| 98 | |
| 99 | Q_DECLARE_TYPEINFO(ByteTranslatorMessage, Q_RELOCATABLE_TYPE); |
| 100 | |
| 101 | bool ByteTranslatorMessage::operator<(const ByteTranslatorMessage& m) const |
| 102 | { |
| 103 | if (m_context != m.m_context) |
| 104 | return m_context < m.m_context; |
| 105 | if (m_sourcetext != m.m_sourcetext) |
| 106 | return m_sourcetext < m.m_sourcetext; |
| 107 | return m_comment < m.m_comment; |
| 108 | } |
| 109 | |
| 110 | class Releaser |
| 111 | { |
| 112 | public: |
| 113 | struct Offset { |
| 114 | Offset() |
| 115 | : h(0), o(0) |
| 116 | {} |
| 117 | Offset(uint hash, uint offset) |
| 118 | : h(hash), o(offset) |
| 119 | {} |
| 120 | |
| 121 | bool operator<(const Offset &other) const { |
| 122 | return (h != other.h) ? h < other.h : o < other.o; |
| 123 | } |
| 124 | bool operator==(const Offset &other) const { |
| 125 | return h == other.h && o == other.o; |
| 126 | } |
| 127 | uint h; |
| 128 | uint o; |
| 129 | }; |
| 130 | |
| 131 | enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69, NumerusRules = 0x88, Dependencies = 0x96, Language = 0xa7 }; |
| 132 | |
| 133 | Releaser(const QString &language) : m_language(language) {} |
| 134 | |
| 135 | bool save(QIODevice *iod); |
| 136 | |
| 137 | void insert(const TranslatorMessage &msg, const QStringList &tlns, bool ); |
| 138 | void insertIdBased(const TranslatorMessage &message, const QStringList &tlns); |
| 139 | |
| 140 | void squeeze(TranslatorSaveMode mode); |
| 141 | |
| 142 | void setNumerusRules(const QByteArray &rules); |
| 143 | void setDependencies(const QStringList &dependencies); |
| 144 | |
| 145 | private: |
| 146 | Q_DISABLE_COPY(Releaser) |
| 147 | |
| 148 | // This should reproduce the byte array fetched from the source file, which |
| 149 | // on turn should be the same as passed to the actual tr(...) calls |
| 150 | QByteArray originalBytes(const QString &str) const; |
| 151 | |
| 152 | static Prefix commonPrefix(const ByteTranslatorMessage &m1, const ByteTranslatorMessage &m2); |
| 153 | |
| 154 | static uint msgHash(const ByteTranslatorMessage &msg); |
| 155 | |
| 156 | void writeMessage(const ByteTranslatorMessage & msg, QDataStream & stream, |
| 157 | TranslatorSaveMode strip, Prefix prefix) const; |
| 158 | |
| 159 | QString m_language; |
| 160 | // for squeezed but non-file data, this is what needs to be deleted |
| 161 | QByteArray m_messageArray; |
| 162 | QByteArray m_offsetArray; |
| 163 | QByteArray m_contextArray; |
| 164 | QMap<ByteTranslatorMessage, void *> m_messages; |
| 165 | QByteArray m_numerusRules; |
| 166 | QStringList m_dependencies; |
| 167 | QByteArray m_dependencyArray; |
| 168 | }; |
| 169 | |
| 170 | QByteArray Releaser::originalBytes(const QString &str) const |
| 171 | { |
| 172 | if (str.isEmpty()) { |
| 173 | // Do not use QByteArray() here as the result of the serialization |
| 174 | // will be different. |
| 175 | return QByteArray("" ); |
| 176 | } |
| 177 | return str.toUtf8(); |
| 178 | } |
| 179 | |
| 180 | uint Releaser::msgHash(const ByteTranslatorMessage &msg) |
| 181 | { |
| 182 | return elfHash(ba: msg.sourceText() + msg.comment()); |
| 183 | } |
| 184 | |
| 185 | Prefix Releaser::commonPrefix(const ByteTranslatorMessage &m1, const ByteTranslatorMessage &m2) |
| 186 | { |
| 187 | if (msgHash(msg: m1) != msgHash(msg: m2)) |
| 188 | return NoPrefix; |
| 189 | if (m1.context() != m2.context()) |
| 190 | return Hash; |
| 191 | if (m1.sourceText() != m2.sourceText()) |
| 192 | return HashContext; |
| 193 | if (m1.comment() != m2.comment()) |
| 194 | return HashContextSourceText; |
| 195 | return HashContextSourceTextComment; |
| 196 | } |
| 197 | |
| 198 | void Releaser::writeMessage(const ByteTranslatorMessage &msg, QDataStream &stream, |
| 199 | TranslatorSaveMode mode, Prefix prefix) const |
| 200 | { |
| 201 | for (int i = 0; i < msg.translations().size(); ++i) |
| 202 | stream << quint8(Tag_Translation) << msg.translations().at(i); |
| 203 | |
| 204 | if (mode == SaveEverything) |
| 205 | prefix = HashContextSourceTextComment; |
| 206 | |
| 207 | // lrelease produces "wrong" QM files for QByteArrays that are .isNull(). |
| 208 | switch (prefix) { |
| 209 | default: |
| 210 | case HashContextSourceTextComment: |
| 211 | stream << quint8(Tag_Comment) << msg.comment(); |
| 212 | Q_FALLTHROUGH(); |
| 213 | case HashContextSourceText: |
| 214 | stream << quint8(Tag_SourceText) << msg.sourceText(); |
| 215 | Q_FALLTHROUGH(); |
| 216 | case HashContext: |
| 217 | stream << quint8(Tag_Context) << msg.context(); |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | stream << quint8(Tag_End); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | bool Releaser::save(QIODevice *iod) |
| 226 | { |
| 227 | QDataStream s(iod); |
| 228 | s.writeRawData((const char *)magic, len: MagicLength); |
| 229 | |
| 230 | if (!m_language.isEmpty()) { |
| 231 | QByteArray lang = originalBytes(str: m_language); |
| 232 | quint32 las = quint32(lang.size()); |
| 233 | s << quint8(Language) << las; |
| 234 | s.writeRawData(lang, len: las); |
| 235 | } |
| 236 | if (!m_dependencyArray.isEmpty()) { |
| 237 | quint32 das = quint32(m_dependencyArray.size()); |
| 238 | s << quint8(Dependencies) << das; |
| 239 | s.writeRawData(m_dependencyArray.constData(), len: das); |
| 240 | } |
| 241 | if (!m_offsetArray.isEmpty()) { |
| 242 | quint32 oas = quint32(m_offsetArray.size()); |
| 243 | s << quint8(Hashes) << oas; |
| 244 | s.writeRawData(m_offsetArray.constData(), len: oas); |
| 245 | } |
| 246 | if (!m_messageArray.isEmpty()) { |
| 247 | quint32 mas = quint32(m_messageArray.size()); |
| 248 | s << quint8(Messages) << mas; |
| 249 | s.writeRawData(m_messageArray.constData(), len: mas); |
| 250 | } |
| 251 | if (!m_contextArray.isEmpty()) { |
| 252 | quint32 cas = quint32(m_contextArray.size()); |
| 253 | s << quint8(Contexts) << cas; |
| 254 | s.writeRawData(m_contextArray.constData(), len: cas); |
| 255 | } |
| 256 | if (!m_numerusRules.isEmpty()) { |
| 257 | quint32 nrs = m_numerusRules.size(); |
| 258 | s << quint8(NumerusRules) << nrs; |
| 259 | s.writeRawData(m_numerusRules.constData(), len: nrs); |
| 260 | } |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | void Releaser::squeeze(TranslatorSaveMode mode) |
| 265 | { |
| 266 | m_dependencyArray.clear(); |
| 267 | QDataStream depstream(&m_dependencyArray, QIODevice::WriteOnly); |
| 268 | for (const QString &dep : std::as_const(t&: m_dependencies)) |
| 269 | depstream << dep; |
| 270 | |
| 271 | if (m_messages.isEmpty() && mode == SaveEverything) |
| 272 | return; |
| 273 | |
| 274 | const auto messages = m_messages; |
| 275 | |
| 276 | // re-build contents |
| 277 | m_messageArray.clear(); |
| 278 | m_offsetArray.clear(); |
| 279 | m_contextArray.clear(); |
| 280 | m_messages.clear(); |
| 281 | |
| 282 | QMap<Offset, void *> offsets; |
| 283 | |
| 284 | QDataStream ms(&m_messageArray, QIODevice::WriteOnly); |
| 285 | int cpPrev = 0, cpNext = 0; |
| 286 | for (auto it = messages.cbegin(), end = messages.cend(); it != end; ++it) { |
| 287 | cpPrev = cpNext; |
| 288 | const auto next = std::next(x: it); |
| 289 | if (next == end) |
| 290 | cpNext = 0; |
| 291 | else |
| 292 | cpNext = commonPrefix(m1: it.key(), m2: next.key()); |
| 293 | offsets.insert(key: Offset(msgHash(msg: it.key()), ms.device()->pos()), value: (void *)0); |
| 294 | writeMessage(msg: it.key(), stream&: ms, mode, prefix: Prefix(qMax(a: cpPrev, b: cpNext + 1))); |
| 295 | } |
| 296 | |
| 297 | auto offset = offsets.cbegin(); |
| 298 | QDataStream ds(&m_offsetArray, QIODevice::WriteOnly); |
| 299 | while (offset != offsets.cend()) { |
| 300 | Offset k = offset.key(); |
| 301 | ++offset; |
| 302 | ds << quint32(k.h) << quint32(k.o); |
| 303 | } |
| 304 | |
| 305 | if (mode == SaveStripped) { |
| 306 | QMap<QByteArray, int> contextSet; |
| 307 | for (auto it = messages.cbegin(), end = messages.cend(); it != end; ++it) |
| 308 | ++contextSet[it.key().context()]; |
| 309 | |
| 310 | quint16 hTableSize; |
| 311 | if (contextSet.size() < 200) |
| 312 | hTableSize = (contextSet.size() < 60) ? 151 : 503; |
| 313 | else if (contextSet.size() < 2500) |
| 314 | hTableSize = (contextSet.size() < 750) ? 1511 : 5003; |
| 315 | else |
| 316 | hTableSize = (contextSet.size() < 10000) ? 15013 : 3 * contextSet.size() / 2; |
| 317 | |
| 318 | QMultiMap<int, QByteArray> hashMap; |
| 319 | for (auto c = contextSet.cbegin(), end = contextSet.cend(); c != end; ++c) |
| 320 | hashMap.insert(key: elfHash(ba: c.key()) % hTableSize, value: c.key()); |
| 321 | |
| 322 | /* |
| 323 | The contexts found in this translator are stored in a hash |
| 324 | table to provide fast lookup. The context array has the |
| 325 | following format: |
| 326 | |
| 327 | quint16 hTableSize; |
| 328 | quint16 hTable[hTableSize]; |
| 329 | quint8 contextPool[...]; |
| 330 | |
| 331 | The context pool stores the contexts as Pascal strings: |
| 332 | |
| 333 | quint8 len; |
| 334 | quint8 data[len]; |
| 335 | |
| 336 | Let's consider the look-up of context "FunnyDialog". A |
| 337 | hash value between 0 and hTableSize - 1 is computed, say h. |
| 338 | If hTable[h] is 0, "FunnyDialog" is not covered by this |
| 339 | translator. Else, we check in the contextPool at offset |
| 340 | 2 * hTable[h] to see if "FunnyDialog" is one of the |
| 341 | contexts stored there, until we find it or we meet the |
| 342 | empty string. |
| 343 | */ |
| 344 | m_contextArray.resize(size: 2 + (hTableSize << 1)); |
| 345 | QDataStream t(&m_contextArray, QIODevice::WriteOnly); |
| 346 | |
| 347 | quint16 *hTable = new quint16[hTableSize]; |
| 348 | memset(s: hTable, c: 0, n: hTableSize * sizeof(quint16)); |
| 349 | |
| 350 | t << hTableSize; |
| 351 | t.device()->seek(pos: 2 + (hTableSize << 1)); |
| 352 | t << quint16(0); // the entry at offset 0 cannot be used |
| 353 | uint upto = 2; |
| 354 | |
| 355 | auto entry = hashMap.constBegin(); |
| 356 | while (entry != hashMap.constEnd()) { |
| 357 | int i = entry.key(); |
| 358 | hTable[i] = quint16(upto >> 1); |
| 359 | |
| 360 | do { |
| 361 | const char *con = entry.value().constData(); |
| 362 | uint len = uint(entry.value().size()); |
| 363 | len = qMin(a: len, b: 255u); |
| 364 | t << quint8(len); |
| 365 | t.writeRawData(con, len); |
| 366 | upto += 1 + len; |
| 367 | ++entry; |
| 368 | } while (entry != hashMap.constEnd() && entry.key() == i); |
| 369 | if (upto & 0x1) { |
| 370 | // offsets have to be even |
| 371 | t << quint8(0); // empty string |
| 372 | ++upto; |
| 373 | } |
| 374 | } |
| 375 | t.device()->seek(pos: 2); |
| 376 | for (int j = 0; j < hTableSize; j++) |
| 377 | t << hTable[j]; |
| 378 | delete [] hTable; |
| 379 | |
| 380 | if (upto > 131072) { |
| 381 | qWarning(msg: "Releaser::squeeze: Too many contexts" ); |
| 382 | m_contextArray.clear(); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void Releaser::insert(const TranslatorMessage &message, const QStringList &tlns, bool ) |
| 388 | { |
| 389 | ByteTranslatorMessage bmsg(originalBytes(str: message.context()), |
| 390 | originalBytes(str: message.sourceText()), |
| 391 | originalBytes(str: message.comment()), |
| 392 | tlns); |
| 393 | if (!forceComment) { |
| 394 | ByteTranslatorMessage bmsg2( |
| 395 | bmsg.context(), bmsg.sourceText(), QByteArray("" ), bmsg.translations()); |
| 396 | if (!m_messages.contains(key: bmsg2)) { |
| 397 | m_messages.insert(key: bmsg2, value: 0); |
| 398 | return; |
| 399 | } |
| 400 | } |
| 401 | m_messages.insert(key: bmsg, value: 0); |
| 402 | } |
| 403 | |
| 404 | void Releaser::insertIdBased(const TranslatorMessage &message, const QStringList &tlns) |
| 405 | { |
| 406 | ByteTranslatorMessage bmsg("" , originalBytes(str: message.id()), "" , tlns); |
| 407 | m_messages.insert(key: bmsg, value: 0); |
| 408 | } |
| 409 | |
| 410 | void Releaser::setNumerusRules(const QByteArray &rules) |
| 411 | { |
| 412 | m_numerusRules = rules; |
| 413 | } |
| 414 | |
| 415 | void Releaser::setDependencies(const QStringList &dependencies) |
| 416 | { |
| 417 | m_dependencies = dependencies; |
| 418 | } |
| 419 | |
| 420 | static quint8 read8(const uchar *data) |
| 421 | { |
| 422 | return *data; |
| 423 | } |
| 424 | |
| 425 | static quint32 read32(const uchar *data) |
| 426 | { |
| 427 | return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]); |
| 428 | } |
| 429 | |
| 430 | static void fromBytes(const char *str, int len, QString *out, bool *utf8Fail) |
| 431 | { |
| 432 | QStringDecoder toUnicode(QStringDecoder::Utf8, QStringDecoder::Flag::Stateless); |
| 433 | *out = toUnicode(QByteArrayView(str, len)); |
| 434 | *utf8Fail = toUnicode.hasError(); |
| 435 | } |
| 436 | |
| 437 | bool loadQM(Translator &translator, QIODevice &dev, ConversionData &cd) |
| 438 | { |
| 439 | QByteArray ba = dev.readAll(); |
| 440 | const uchar *data = (uchar*)ba.data(); |
| 441 | int len = ba.size(); |
| 442 | if (len < MagicLength || memcmp(s1: data, s2: magic, n: MagicLength) != 0) { |
| 443 | cd.appendError(error: "QM-Format error: magic marker missing"_L1 ); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69, NumerusRules = 0x88, Dependencies = 0x96, Language = 0xa7 }; |
| 448 | |
| 449 | // for squeezed but non-file data, this is what needs to be deleted |
| 450 | const uchar *messageArray = nullptr; |
| 451 | const uchar *offsetArray = nullptr; |
| 452 | uint offsetLength = 0; |
| 453 | |
| 454 | bool ok = true; |
| 455 | bool utf8Fail = false; |
| 456 | const uchar *end = data + len; |
| 457 | |
| 458 | data += MagicLength; |
| 459 | |
| 460 | while (data < end - 4) { |
| 461 | quint8 tag = read8(data: data++); |
| 462 | quint32 blockLen = read32(data); |
| 463 | //qDebug() << "TAG:" << tag << "BLOCKLEN:" << blockLen; |
| 464 | data += 4; |
| 465 | if (!tag || !blockLen) |
| 466 | break; |
| 467 | if (data + blockLen > end) { |
| 468 | ok = false; |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | if (tag == Hashes) { |
| 473 | offsetArray = data; |
| 474 | offsetLength = blockLen; |
| 475 | //qDebug() << "HASHES: " << blockLen << QByteArray((const char *)data, blockLen).toHex(); |
| 476 | } else if (tag == Messages) { |
| 477 | messageArray = data; |
| 478 | //qDebug() << "MESSAGES: " << blockLen << QByteArray((const char *)data, blockLen).toHex(); |
| 479 | } else if (tag == Dependencies) { |
| 480 | QStringList dependencies; |
| 481 | QDataStream stream(QByteArray::fromRawData(data: (const char*)data, size: blockLen)); |
| 482 | QString dep; |
| 483 | while (!stream.atEnd()) { |
| 484 | stream >> dep; |
| 485 | dependencies.append(t: dep); |
| 486 | } |
| 487 | translator.setDependencies(dependencies); |
| 488 | } else if (tag == Language) { |
| 489 | QString language; |
| 490 | fromBytes(str: (const char *)data, len: blockLen, out: &language, utf8Fail: &utf8Fail); |
| 491 | translator.setLanguageCode(language); |
| 492 | } |
| 493 | |
| 494 | data += blockLen; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | size_t numItems = offsetLength / (2 * sizeof(quint32)); |
| 499 | //qDebug() << "NUMITEMS: " << numItems; |
| 500 | |
| 501 | QString strProN = "%n"_L1 ; |
| 502 | QLocale::Language l; |
| 503 | QLocale::Territory c; |
| 504 | Translator::languageAndTerritory(languageCode: translator.languageCode(), langPtr: &l, territoryPtr: &c); |
| 505 | QStringList numerusForms; |
| 506 | bool guessPlurals = true; |
| 507 | if (getNumerusInfo(language: l, territory: c, rules: 0, forms: &numerusForms, gettextRules: 0)) |
| 508 | guessPlurals = (numerusForms.size() == 1); |
| 509 | |
| 510 | QString context, sourcetext, ; |
| 511 | QStringList translations; |
| 512 | |
| 513 | for (const uchar *start = offsetArray; start != offsetArray + (numItems << 3); start += 8) { |
| 514 | //quint32 hash = read32(start); |
| 515 | quint32 ro = read32(data: start + 4); |
| 516 | //qDebug() << "\nHASH:" << hash; |
| 517 | const uchar *m = messageArray + ro; |
| 518 | |
| 519 | for (;;) { |
| 520 | uchar tag = read8(data: m++); |
| 521 | //qDebug() << "Tag:" << tag << " ADDR: " << m; |
| 522 | switch(tag) { |
| 523 | case Tag_End: |
| 524 | goto end; |
| 525 | case Tag_Translation: { |
| 526 | int len = read32(data: m); |
| 527 | m += 4; |
| 528 | |
| 529 | // -1 indicates an empty string |
| 530 | // Otherwise streaming format is UTF-16 -> 2 bytes per character |
| 531 | if ((len != -1) && (len & 1)) { |
| 532 | cd.appendError(error: "QM-Format error"_L1 ); |
| 533 | return false; |
| 534 | } |
| 535 | QString str; |
| 536 | if (len != -1) |
| 537 | str = QString((const QChar *)m, len / 2); |
| 538 | if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) { |
| 539 | for (int i = 0; i < str.size(); ++i) |
| 540 | str[i] = QChar((str.at(i).unicode() >> 8) + |
| 541 | ((str.at(i).unicode() << 8) & 0xff00)); |
| 542 | } |
| 543 | translations << str; |
| 544 | m += len; |
| 545 | break; |
| 546 | } |
| 547 | case Tag_Obsolete1: |
| 548 | m += 4; |
| 549 | //qDebug() << "OBSOLETE"; |
| 550 | break; |
| 551 | case Tag_SourceText: { |
| 552 | quint32 len = read32(data: m); |
| 553 | m += 4; |
| 554 | //qDebug() << "SOURCE LEN: " << len; |
| 555 | //qDebug() << "SOURCE: " << QByteArray((const char*)m, len); |
| 556 | fromBytes(str: (const char*)m, len, out: &sourcetext, utf8Fail: &utf8Fail); |
| 557 | m += len; |
| 558 | break; |
| 559 | } |
| 560 | case Tag_Context: { |
| 561 | quint32 len = read32(data: m); |
| 562 | m += 4; |
| 563 | //qDebug() << "CONTEXT LEN: " << len; |
| 564 | //qDebug() << "CONTEXT: " << QByteArray((const char*)m, len); |
| 565 | fromBytes(str: (const char*)m, len, out: &context, utf8Fail: &utf8Fail); |
| 566 | m += len; |
| 567 | break; |
| 568 | } |
| 569 | case Tag_Comment: { |
| 570 | quint32 len = read32(data: m); |
| 571 | m += 4; |
| 572 | //qDebug() << "COMMENT LEN: " << len; |
| 573 | //qDebug() << "COMMENT: " << QByteArray((const char*)m, len); |
| 574 | fromBytes(str: (const char*)m, len, out: &comment, utf8Fail: &utf8Fail); |
| 575 | m += len; |
| 576 | break; |
| 577 | } |
| 578 | default: |
| 579 | //qDebug() << "UNKNOWN TAG" << tag; |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | end:; |
| 584 | TranslatorMessage msg; |
| 585 | msg.setType(TranslatorMessage::Finished); |
| 586 | if (translations.size() > 1) { |
| 587 | // If guessPlurals is not false here, plural form discard messages |
| 588 | // will be spewn out later. |
| 589 | msg.setPlural(true); |
| 590 | } else if (guessPlurals) { |
| 591 | // This might cause false positives, so it is a fallback only. |
| 592 | if (sourcetext.contains(s: strProN)) |
| 593 | msg.setPlural(true); |
| 594 | } |
| 595 | msg.setTranslations(translations); |
| 596 | translations.clear(); |
| 597 | msg.setContext(context); |
| 598 | msg.setSourceText(sourcetext); |
| 599 | msg.setComment(comment); |
| 600 | translator.append(msg); |
| 601 | } |
| 602 | if (utf8Fail) { |
| 603 | cd.appendError(error: "Error: File contains invalid UTF-8 sequences."_L1 ); |
| 604 | return false; |
| 605 | } |
| 606 | return ok; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | |
| 611 | static bool containsStripped(const Translator &translator, const TranslatorMessage &msg) |
| 612 | { |
| 613 | for (const TranslatorMessage &tmsg : translator.messages()) |
| 614 | if (tmsg.sourceText() == msg.sourceText() |
| 615 | && tmsg.context() == msg.context() |
| 616 | && tmsg.comment().isEmpty()) |
| 617 | return true; |
| 618 | return false; |
| 619 | } |
| 620 | |
| 621 | bool saveQM(const Translator &translator, QIODevice &dev, ConversionData &cd) |
| 622 | { |
| 623 | Releaser releaser(translator.languageCode()); |
| 624 | QLocale::Language l; |
| 625 | QLocale::Territory c; |
| 626 | Translator::languageAndTerritory(languageCode: translator.languageCode(), langPtr: &l, territoryPtr: &c); |
| 627 | QByteArray rules; |
| 628 | if (getNumerusInfo(language: l, territory: c, rules: &rules, forms: 0, gettextRules: 0)) |
| 629 | releaser.setNumerusRules(rules); |
| 630 | |
| 631 | int finished = 0; |
| 632 | int unfinished = 0; |
| 633 | int untranslated = 0; |
| 634 | int droppedData = 0; |
| 635 | |
| 636 | for (int i = 0; i != translator.messageCount(); ++i) { |
| 637 | const TranslatorMessage &msg = translator.message(i); |
| 638 | bool idBased = msg.context().isEmpty() && !msg.id().isEmpty(); |
| 639 | TranslatorMessage::Type typ = msg.type(); |
| 640 | if (typ != TranslatorMessage::Obsolete && typ != TranslatorMessage::Vanished) { |
| 641 | if (typ == TranslatorMessage::Unfinished) { |
| 642 | if (msg.translation().isEmpty() && !idBased && cd.m_unTrPrefix.isEmpty()) { |
| 643 | ++untranslated; |
| 644 | continue; |
| 645 | } else { |
| 646 | if (cd.ignoreUnfinished()) |
| 647 | continue; |
| 648 | ++unfinished; |
| 649 | } |
| 650 | } else { |
| 651 | ++finished; |
| 652 | } |
| 653 | QStringList tlns = msg.translations(); |
| 654 | if (msg.type() == TranslatorMessage::Unfinished |
| 655 | && (idBased || !cd.m_unTrPrefix.isEmpty())) |
| 656 | for (int j = 0; j < tlns.size(); ++j) |
| 657 | if (tlns.at(i: j).isEmpty()) |
| 658 | tlns[j] = cd.m_unTrPrefix + msg.sourceText(); |
| 659 | if (idBased) { |
| 660 | if (!msg.comment().isEmpty()) |
| 661 | ++droppedData; |
| 662 | releaser.insertIdBased(message: msg, tlns); |
| 663 | } else { |
| 664 | // Drop the comment in (context, sourceText, comment), |
| 665 | // unless the context is empty, |
| 666 | // unless (context, sourceText, "") already exists or |
| 667 | // unless we already dropped the comment of (context, |
| 668 | // sourceText, comment0). |
| 669 | bool = |
| 670 | msg.comment().isEmpty() |
| 671 | || msg.context().isEmpty() |
| 672 | || containsStripped(translator, msg); |
| 673 | releaser.insert(message: msg, tlns, forceComment); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (droppedData) |
| 679 | cd.appendError(error: QCoreApplication::translate(context: "LRelease" , |
| 680 | key: "Excess context/disambiguation dropped from %n message(s)." , disambiguation: 0, |
| 681 | n: droppedData)); |
| 682 | |
| 683 | releaser.setDependencies(translator.dependencies()); |
| 684 | releaser.squeeze(mode: cd.m_saveMode); |
| 685 | bool saved = releaser.save(iod: &dev); |
| 686 | if (saved && cd.isVerbose()) { |
| 687 | int generatedCount = finished + unfinished; |
| 688 | cd.appendError(error: QCoreApplication::translate(context: "LRelease" , |
| 689 | key: " Generated %n translation(s) (%1 finished and %2 unfinished)" , disambiguation: 0, |
| 690 | n: generatedCount).arg(a: finished).arg(a: unfinished)); |
| 691 | if (untranslated) |
| 692 | cd.appendError(error: QCoreApplication::translate(context: "LRelease" , |
| 693 | key: " Ignored %n untranslated source text(s)" , disambiguation: 0, |
| 694 | n: untranslated)); |
| 695 | } |
| 696 | return saved; |
| 697 | } |
| 698 | |
| 699 | int initQM() |
| 700 | { |
| 701 | Translator::FileFormat format; |
| 702 | |
| 703 | format.extension = "qm"_L1 ; |
| 704 | format.untranslatedDescription = QT_TRANSLATE_NOOP("FMT" , "Compiled Qt translations" ); |
| 705 | format.fileType = Translator::FileFormat::TranslationBinary; |
| 706 | format.priority = 0; |
| 707 | format.loader = &loadQM; |
| 708 | format.saver = &saveQM; |
| 709 | Translator::registerFileFormat(format); |
| 710 | |
| 711 | return 1; |
| 712 | } |
| 713 | |
| 714 | Q_CONSTRUCTOR_FUNCTION(initQM) |
| 715 | |
| 716 | QT_END_NAMESPACE |
| 717 | |