Changeset View
Changeset View
Standalone View
Standalone View
src/qtquick/ArchiveBookModel.cpp
Context not available. | |||||
30 | #include <AcbfPage.h> | 30 | #include <AcbfPage.h> | ||
---|---|---|---|---|---|
31 | #include <AcbfPublishinfo.h> | 31 | #include <AcbfPublishinfo.h> | ||
32 | #include <AcbfDocumentinfo.h> | 32 | #include <AcbfDocumentinfo.h> | ||
33 | #include <AcbfTextlayer.h> | ||||
34 | #include <AcbfTextarea.h> | ||||
33 | 35 | | |||
34 | #include <QCoreApplication> | 36 | #include <QCoreApplication> | ||
35 | #include <QDebug> | 37 | #include <QDebug> | ||
Context not available. | |||||
1251 | } | 1253 | } | ||
1252 | return false; | 1254 | return false; | ||
1253 | } | 1255 | } | ||
1256 | | ||||
1257 | bool ArchiveBookModel::generatePot(const QString fileName, const QString language) | ||||
1258 | { | ||||
1259 | QFile file(fileName); | ||||
1260 | qDebug() << fileName; | ||||
1261 | bool success = false; | ||||
1262 | | ||||
1263 | if (file.open(QFile::WriteOnly| QFile::Truncate) && acbfData()) { | ||||
1264 | AdvancedComicBookFormat::Document* acbf = qobject_cast<AdvancedComicBookFormat::Document*>(acbfData()); | ||||
1265 | QTextStream pot(&file); | ||||
1266 | QString quote = "\""; | ||||
1267 | QString newLine = "\n"; | ||||
1268 | | ||||
1269 | pot << "msid " << quote+quote+newLine; | ||||
1270 | pot << "msgstr " << quote+quote+newLine; | ||||
1271 | | ||||
1272 | pot << quote << "POT-Creation-Date: " << QDateTime::currentDateTimeUtc().toString() << "\\n" << quote << newLine; | ||||
1273 | pot << quote << "Content-Type: text/plain; charset=UTF-8\\n" << quote << newLine; | ||||
1274 | pot << quote << "Content-Transfer-Encoding: 8bit\\n" << quote << newLine; | ||||
1275 | pot << quote << "X-Generator: Peruse Creator\\n" << quote << newLine; | ||||
1276 | | ||||
1277 | pot << newLine; | ||||
1278 | pot << "#. Title of the work" << newLine; | ||||
1279 | pot << "msgctxt \"@meta-title\"" << newLine; | ||||
1280 | pot << "msgid " << quote << acbf->metaData()->bookInfo()->title(language) << quote << newLine; | ||||
1281 | pot << "msgstr " << quote << quote << newLine; | ||||
1282 | pot << newLine; | ||||
1283 | | ||||
1284 | pot << "#. The summary" << newLine; | ||||
1285 | pot << "msgctxt \"@meta-summary\"" << newLine; | ||||
1286 | pot << "msgid " << quote << quote << newLine; | ||||
1287 | for (int i =0; i < acbf->metaData()->bookInfo()->annotation(language).size(); i++) { | ||||
1288 | QString paragraph = acbf->metaData()->bookInfo()->annotation(language).at(i); | ||||
1289 | paragraph.replace(quote, "\\\""); | ||||
1290 | paragraph.replace("\'", "\\\'"); | ||||
1291 | paragraph.replace("#", "\\#"); | ||||
1292 | pot << quote << "<p>" << paragraph << "</p>" << quote << newLine; | ||||
1293 | } | ||||
1294 | pot << "msgstr " << quote << quote << newLine; | ||||
1295 | pot << newLine; | ||||
1296 | | ||||
1297 | pot << "#. The keywords, these need to be comma separated." + newLine; | ||||
1298 | pot << "msgctxt \"@meta-keywords\"" << newLine; | ||||
1299 | pot << "msgid " << quote << acbf->metaData()->bookInfo()->keywords(language).join(", ") << quote << newLine; | ||||
1300 | pot << "msgstr " << quote << quote << newLine; | ||||
1301 | pot << newLine; | ||||
1302 | | ||||
1303 | for (int p = 0; p < acbf->body()->pageCount(); p++) { | ||||
1304 | AdvancedComicBookFormat::Textlayer* textlayer = acbf->body()->page(p)->textLayer(language); | ||||
1305 | | ||||
1306 | if (!acbf->body()->page(p)->title("").isEmpty()) { | ||||
1307 | pot << "#. Page title"; | ||||
1308 | pot << "msgctxt " << quote << "@page-title" << quote << newLine; | ||||
1309 | pot << "msgid " << quote << acbf->body()->page(p)->title("") << quote << newLine; | ||||
1310 | pot << "msgstr " << quote << quote << newLine; | ||||
1311 | pot << newLine; | ||||
1312 | } | ||||
1313 | | ||||
1314 | for (int t = 0; t < textlayer->textareas().size(); t++) { | ||||
1315 | AdvancedComicBookFormat::Textarea* textarea = textlayer->textarea(t); | ||||
1316 | | ||||
1317 | pot << newLine; | ||||
1318 | pot << "msgid " << quote << quote << newLine; | ||||
1319 | for (int i =0; i < textarea->paragraphs().size(); i++) { | ||||
1320 | QString paragraph = textarea->paragraphs().at(i); | ||||
1321 | paragraph.replace(quote, "\\\""); | ||||
1322 | paragraph.replace("\'", "\\\'"); | ||||
1323 | paragraph.replace("#", "\\#"); | ||||
1324 | pot << quote << "<p>" << paragraph << "</p>" << quote << newLine; | ||||
1325 | } | ||||
1326 | pot << "msgstr " << quote << quote << newLine; | ||||
1327 | pot << newLine; | ||||
1328 | } | ||||
1329 | } | ||||
1330 | success = true; | ||||
1331 | } | ||||
1332 | file.close(); | ||||
1333 | return success; | ||||
1334 | } | ||||
1335 | | ||||
1336 | bool ArchiveBookModel::readPoFile(const QString fileName, bool addTranslatorEmail) | ||||
1337 | { | ||||
1338 | QFile file(fileName); | ||||
1339 | bool success = false; | ||||
1340 | QString language; | ||||
1341 | QString translator; | ||||
1342 | QString multiline; | ||||
1343 | QString context; | ||||
1344 | QString id; | ||||
1345 | QString translation; | ||||
1346 | QHash<QString, QString> table; | ||||
1347 | | ||||
1348 | if (file.open(QFile::ReadOnly|QFile::Text)) { | ||||
1349 | QTextStream po(&file); | ||||
1350 | QString line; | ||||
1351 | while (po.readLineInto(&line)) { | ||||
1352 | if (line.startsWith("\"Last-Translator: ")) | ||||
1353 | { | ||||
1354 | line.remove("\"Last-Translator: "); | ||||
1355 | translator = line.replace("\\n\"", ""); | ||||
1356 | } | ||||
1357 | | ||||
1358 | else if (line.startsWith("\"Language: ")) | ||||
1359 | { | ||||
1360 | line.remove("\"Language: "); | ||||
1361 | language = line.replace("\\n\"", ""); | ||||
1362 | } | ||||
1363 | | ||||
1364 | else if (line.startsWith("msgctxt")) | ||||
1365 | { | ||||
1366 | if (!line.endsWith("\"\"")) { | ||||
1367 | line.remove("msgctxt \""); | ||||
1368 | context = line.replace("\"", ""); | ||||
1369 | } | ||||
1370 | } | ||||
1371 | | ||||
1372 | else if (line.startsWith("msgid")) | ||||
1373 | { | ||||
1374 | if (!multiline.isEmpty()) { | ||||
1375 | context.append(multiline); | ||||
1376 | multiline.clear(); | ||||
1377 | } | ||||
1378 | if (!line.endsWith("\"\"")) { | ||||
1379 | line.remove("msgid \""); | ||||
1380 | id = line.replace("\"", ""); | ||||
1381 | } | ||||
1382 | } | ||||
1383 | | ||||
1384 | else if (line.startsWith("msgstr")) | ||||
1385 | { | ||||
1386 | if (!multiline.isEmpty()) { | ||||
1387 | id.append(multiline); | ||||
1388 | multiline.clear(); | ||||
1389 | } | ||||
1390 | if (!line.endsWith("\"\"")) { | ||||
1391 | line.remove("msgstr \""); | ||||
1392 | translation = line.replace("\"", ""); | ||||
1393 | } | ||||
1394 | } | ||||
1395 | | ||||
1396 | else if (line.startsWith("\"")) | ||||
1397 | { | ||||
1398 | if (!line.endsWith("\"\"")) { | ||||
1399 | line.remove("\""); | ||||
1400 | multiline.append(line.replace("\"", "")); | ||||
1401 | } | ||||
1402 | } | ||||
1403 | | ||||
1404 | else if (line.isEmpty()) { | ||||
1405 | translation.append(multiline); | ||||
1406 | multiline.clear(); | ||||
1407 | if (!id.isEmpty()) { | ||||
1408 | if (context.isEmpty()) { | ||||
1409 | table.insert(id, translation); | ||||
1410 | } else { | ||||
1411 | table.insert(context, translation); | ||||
1412 | } | ||||
1413 | } | ||||
1414 | context.clear(); | ||||
1415 | id.clear(); | ||||
1416 | translation.clear(); | ||||
1417 | } else { | ||||
1418 | multiline.clear(); | ||||
1419 | context.clear(); | ||||
1420 | id.clear(); | ||||
1421 | translation.clear(); | ||||
1422 | } | ||||
1423 | | ||||
1424 | } | ||||
1425 | | ||||
1426 | AdvancedComicBookFormat::Document* acbf = qobject_cast<AdvancedComicBookFormat::Document*>(acbfData()); | ||||
1427 | acbf->metaData()->bookInfo()->addLanguage(language); | ||||
1428 | if (!table.value("@meta-title").isEmpty()) { | ||||
1429 | acbf->metaData()->bookInfo()->setTitle(table.value("@meta-title"), language); | ||||
1430 | qDebug() << Q_FUNC_INFO << "adding translation" << language << acbf->metaData()->bookInfo()->title(language); | ||||
1431 | } | ||||
1432 | if (!table.value("@meta-summary").isEmpty()) { | ||||
1433 | QStringList paragraphs = table.value("@meta-summary").split("</p>", QString::SkipEmptyParts); | ||||
1434 | for (int i=0; i < paragraphs.size(); i++) { | ||||
1435 | QString p = paragraphs.at(i); | ||||
1436 | p.replace("\\\"", "\""); | ||||
1437 | p.replace("\\\'", "\'"); | ||||
1438 | p.replace("\\#", "#"); | ||||
1439 | paragraphs.replace(i, p.replace("<p>", "").trimmed()); | ||||
1440 | } | ||||
1441 | acbf->metaData()->bookInfo()->setAnnotation(paragraphs, language); | ||||
1442 | qDebug() << Q_FUNC_INFO << "adding translation" << language << paragraphs; | ||||
1443 | } | ||||
1444 | if (!table.value("@meta-keywords").isEmpty()) { | ||||
1445 | QStringList keys = table.value("@meta-keywords").split(",", QString::SkipEmptyParts); | ||||
1446 | for (int i=0; i < keys.size(); i++) { | ||||
1447 | QString p = keys.at(i); | ||||
1448 | keys.replace(i, p.trimmed()); | ||||
1449 | } | ||||
1450 | qDebug() << Q_FUNC_INFO << "adding translation" << language << keys; | ||||
1451 | acbf->metaData()->bookInfo()->setKeywords(keys, language); | ||||
1452 | } | ||||
1453 | | ||||
1454 | for (int p = 0; p < acbf->body()->pageCount(); p++) { | ||||
1455 | acbf->body()->page(p)->duplicateTextLayer("", language); | ||||
1456 | if (!table.value(acbf->body()->page(p)->title("")).isEmpty()) { | ||||
1457 | acbf->body()->page(p)->setTitle(table.value(acbf->body()->page(p)->title("")), language); | ||||
1458 | qDebug() << Q_FUNC_INFO << "adding translation" << language << acbf->body()->page(p)->title(language); | ||||
1459 | } | ||||
1460 | AdvancedComicBookFormat::Textlayer* translation = acbf->body()->page(p)->textLayer(language); | ||||
1461 | for (int t=0; t < translation->textareaPointStrings().size(); t++) { | ||||
1462 | QString key; | ||||
1463 | for (int i =0; i < translation->textarea(t)->paragraphs().size(); i++) { | ||||
1464 | QString paragraph = translation->textarea(t)->paragraphs().at(i); | ||||
1465 | paragraph.replace("\"", "\\\""); | ||||
1466 | paragraph.replace("\'", "\\\'"); | ||||
1467 | paragraph.replace("#", "\\#"); | ||||
1468 | key.append(paragraph); | ||||
1469 | } | ||||
1470 | QStringList paragraphs = table.value("<p>"+key+"</p>").split("</p>", QString::SkipEmptyParts); | ||||
1471 | for (int i=0; i < paragraphs.size(); i++) { | ||||
1472 | QString p = paragraphs.at(i); | ||||
1473 | p.replace("\\\"", "\""); | ||||
1474 | p.replace("\\\'", "\'"); | ||||
1475 | p.replace("\\#", "#"); | ||||
1476 | paragraphs.replace(i, p.replace("<p>", "").trimmed()); | ||||
1477 | } | ||||
1478 | translation->textarea(t)->setParagraphs(paragraphs); | ||||
1479 | if (!paragraphs.isEmpty()) { | ||||
1480 | qDebug() << Q_FUNC_INFO << "adding translation" << language << translation->textarea(t)->paragraphs(); | ||||
1481 | } | ||||
1482 | } | ||||
1483 | } | ||||
1484 | // Finally, add translator. | ||||
1485 | QStringList emails; | ||||
1486 | if (addTranslatorEmail) { | ||||
1487 | QString email = translator.split("<").at(1); | ||||
1488 | email.replace(">", ""); | ||||
1489 | emails.append(email); | ||||
1490 | } | ||||
1491 | translator = translator.split("<").at(0); | ||||
1492 | qDebug() << Q_FUNC_INFO << "adding translator" << translator << emails; | ||||
1493 | | ||||
1494 | if (acbf->metaData()->bookInfo()->authorNames().indexOf(translator) < 0) { | ||||
1495 | acbf->metaData()->bookInfo()->addAuthor("Translator", language, "", "", "", translator, QStringList(), emails); | ||||
1496 | } | ||||
1497 | | ||||
1498 | } | ||||
1499 | file.close(); | ||||
1500 | return success; | ||||
1501 | } | ||||
Context not available. |