static void updateTsFiles(const Translator &fetchedTor, const QStringList &tsFileNames, bool setCodec, const QString &sourceLanguage, const QString &targetLanguage, UpdateOptions options, bool *fail) { QDir dir; QString err; foreach (const QString &fileName, tsFileNames) { QString fn = dir.relativeFilePath(fileName); ConversionData cd; Translator tor; cd.m_sortContexts = !(options & NoSort); if (QFile(fileName).exists()) { if (!tor.load(fileName, cd, QLatin1String("auto"))) { printErr(cd.error()); *fail = true; continue; } tor.resolveDuplicates(); cd.clearErrors(); if (setCodec && fetchedTor.codec() != tor.codec()) printErr(LU::tr("lupdate warning: Codec for tr() '%1' disagrees with" " existing file's codec '%2'. Expect trouble.\n") .arg(QString::fromLatin1(fetchedTor.codecName()), QString::fromLatin1(tor.codecName()))); if (!targetLanguage.isEmpty() && targetLanguage != tor.languageCode()) printErr(LU::tr("lupdate warning: Specified target language '%1' disagrees with" " existing file's language '%2'. Ignoring.\n") .arg(targetLanguage, tor.languageCode())); if (!sourceLanguage.isEmpty() && sourceLanguage != tor.sourceLanguageCode()) printErr(LU::tr("lupdate warning: Specified source language '%1' disagrees with" " existing file's language '%2'. Ignoring.\n") .arg(sourceLanguage, tor.sourceLanguageCode())); } else { if (setCodec) tor.setCodec(fetchedTor.codec()); if (!targetLanguage.isEmpty()) tor.setLanguageCode(targetLanguage); else tor.setLanguageCode(Translator::guessLanguageCodeFromFileName(fileName)); if (!sourceLanguage.isEmpty()) tor.setSourceLanguageCode(sourceLanguage); } tor.makeFileNamesAbsolute(QFileInfo(fileName).absoluteDir()); if (options & NoLocations) tor.setLocationsType(Translator::NoLocations); else if (options & RelativeLocations) tor.setLocationsType(Translator::RelativeLocations); else if (options & AbsoluteLocations) tor.setLocationsType(Translator::AbsoluteLocations); if (options & Verbose) printOut(LU::tr("Updating '%1'...\n").arg(fn)); UpdateOptions theseOptions = options; if (tor.locationsType() == Translator::NoLocations) // Could be set from file theseOptions |= NoLocations; Translator out = merge(tor, fetchedTor, theseOptions, err); if (setCodec) out.setCodec(fetchedTor.codec()); if ((options & Verbose) && !err.isEmpty()) { printOut(err); err.clear(); } if (options & PluralOnly) { if (options & Verbose) printOut(LU::tr("Stripping non plural forms in '%1'...\n").arg(fn)); out.stripNonPluralForms(); } if (options & NoObsolete) out.stripObsoleteMessages(); out.stripEmptyContexts(); out.normalizeTranslations(cd); if (!cd.errors().isEmpty()) { printErr(cd.error()); cd.clearErrors(); } if (!out.save(fileName, cd, QLatin1String("auto"))) { printErr(cd.error()); *fail = true; } }
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); #ifndef Q_OS_WIN32 QTranslator translator; QTranslator qtTranslator; QString sysLocale = QLocale::system().name(); QString resourceDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath); if (translator.load(QLatin1String("linguist_") + sysLocale, resourceDir) && qtTranslator.load(QLatin1String("qt_") + sysLocale, resourceDir)) { app.installTranslator(&translator); app.installTranslator(&qtTranslator); } #endif // Q_OS_WIN32 QStringList args = app.arguments(); QList<File> inFiles; QString inFormat(QLatin1String("auto")); QString outFileName; QString outFormat(QLatin1String("auto")); QString targetLanguage; QString sourceLanguage; bool dropTranslations = false; bool noObsolete = false; bool noFinished = false; bool verbose = false; bool noUiLines = false; Translator::LocationsType locations = Translator::DefaultLocations; ConversionData cd; Translator tr; for (int i = 1; i < args.size(); ++i) { if (args[i].startsWith(QLatin1String("--"))) args[i].remove(0, 1); if (args[i] == QLatin1String("-o") || args[i] == QLatin1String("-output-file")) { if (++i >= args.size()) return usage(args); outFileName = args[i]; } else if (args[i] == QLatin1String("-of") || args[i] == QLatin1String("-output-format")) { if (++i >= args.size()) return usage(args); outFormat = args[i]; } else if (args[i] == QLatin1String("-i") || args[i] == QLatin1String("-input-file")) { if (++i >= args.size()) return usage(args); File file; file.name = args[i]; file.format = inFormat; inFiles.append(file); } else if (args[i] == QLatin1String("-if") || args[i] == QLatin1String("-input-format")) { if (++i >= args.size()) return usage(args); inFormat = args[i]; } else if (args[i] == QLatin1String("-input-codec")) { if (++i >= args.size()) return usage(args); cd.m_codecForSource = args[i].toLatin1(); } else if (args[i] == QLatin1String("-output-codec")) { if (++i >= args.size()) return usage(args); cd.m_outputCodec = args[i].toLatin1(); } else if (args[i] == QLatin1String("-drop-tag")) { if (++i >= args.size()) return usage(args); cd.m_dropTags.append(args[i]); } else if (args[i] == QLatin1String("-drop-translations")) { dropTranslations = true; } else if (args[i] == QLatin1String("-target-language")) { if (++i >= args.size()) return usage(args); targetLanguage = args[i]; } else if (args[i] == QLatin1String("-source-language")) { if (++i >= args.size()) return usage(args); sourceLanguage = args[i]; } else if (args[i].startsWith(QLatin1String("-h"))) { usage(args); return 0; } else if (args[i] == QLatin1String("-no-obsolete")) { noObsolete = true; } else if (args[i] == QLatin1String("-no-finished")) { noFinished = true; } else if (args[i] == QLatin1String("-sort-contexts")) { cd.m_sortContexts = true; } else if (args[i] == QLatin1String("-locations")) { if (++i >= args.size()) return usage(args); if (args[i] == QLatin1String("none")) locations = Translator::NoLocations; else if (args[i] == QLatin1String("relative")) locations = Translator::RelativeLocations; else if (args[i] == QLatin1String("absolute")) locations = Translator::AbsoluteLocations; else return usage(args); } else if (args[i] == QLatin1String("-no-ui-lines")) { noUiLines = true; } else if (args[i] == QLatin1String("-verbose")) { verbose = true; } else if (args[i].startsWith(QLatin1Char('-'))) { return usage(args); } else { File file; file.name = args[i]; file.format = inFormat; inFiles.append(file); } } if (inFiles.isEmpty()) return usage(args); tr.setLanguageCode(Translator::guessLanguageCodeFromFileName(inFiles[0].name)); if (!tr.load(inFiles[0].name, cd, inFiles[0].format)) { std::cerr << qPrintable(cd.error()); return 2; } tr.reportDuplicates(tr.resolveDuplicates(), inFiles[0].name, verbose); for (int i = 1; i < inFiles.size(); ++i) { Translator tr2; if (!tr2.load(inFiles[i].name, cd, inFiles[i].format)) { std::cerr << qPrintable(cd.error()); return 2; } tr2.reportDuplicates(tr2.resolveDuplicates(), inFiles[i].name, verbose); for (int j = 0; j < tr2.messageCount(); ++j) tr.replaceSorted(tr2.message(j)); } if (!targetLanguage.isEmpty()) tr.setLanguageCode(targetLanguage); if (!sourceLanguage.isEmpty()) tr.setSourceLanguageCode(sourceLanguage); if (noObsolete) tr.stripObsoleteMessages(); if (noFinished) tr.stripFinishedMessages(); if (dropTranslations) tr.dropTranslations(); if (noUiLines) tr.dropUiLines(); if (locations != Translator::DefaultLocations) tr.setLocationsType(locations); tr.normalizeTranslations(cd); if (!cd.errors().isEmpty()) { std::cerr << qPrintable(cd.error()); cd.clearErrors(); } if (!tr.save(outFileName, cd, outFormat)) { std::cerr << qPrintable(cd.error()); return 3; } return 0; }
bool TSReader::read(Translator &translator) { STRING(both); STRING(byte); STRING(comment); STRING(context); STRING(defaultcodec); STRING(encoding); STRING(extracomment); STRING(filename); STRING(id); STRING(language); STRING(line); STRING(location); STRING(message); STRING(name); STRING(numerus); STRING(numerusform); STRING(obsolete); STRING(oldcomment); STRING(oldsource); STRING(source); STRING(sourcelanguage); STRING(translation); STRING(translatorcomment); STRING(true); STRING(TS); STRING(type); STRING(unfinished); STRING(userdata); STRING(utf8); STRING(value); //STRING(version); STRING(yes); static const QString strextrans(QLatin1String("extra-")); static const QString strUtf8(QLatin1String("UTF-8")); while (!atEnd()) { readNext(); if (isStartDocument()) { // <!DOCTYPE TS> //qDebug() << attributes(); } else if (isEndDocument()) { // <!DOCTYPE TS> //qDebug() << attributes(); } else if (isDTD()) { // <!DOCTYPE TS> //qDebug() << tokenString(); } else if (elementStarts(strTS)) { // <TS> //qDebug() << "TS " << attributes(); QHash<QString, int> currentLine; QString currentFile; QXmlStreamAttributes atts = attributes(); //QString version = atts.value(strversion).toString(); translator.setLanguageCode(atts.value(strlanguage).toString()); translator.setSourceLanguageCode(atts.value(strsourcelanguage).toString()); while (!atEnd()) { readNext(); if (isEndElement()) { // </TS> found, finish local loop break; } else if (isWhiteSpace()) { // ignore these, just whitespace } else if (elementStarts(strdefaultcodec)) { // <defaultcodec> const QString &codec = readElementText(); if (!codec.isEmpty()) translator.setCodecName(codec.toLatin1()); // </defaultcodec> } else if (isStartElement() && name().toString().startsWith(strextrans)) { // <extra-...> QString tag = name().toString(); translator.setExtra(tag.mid(6), readContents()); // </extra-...> } else if (elementStarts(strcontext)) { // <context> QString context; while (!atEnd()) { readNext(); if (isEndElement()) { // </context> found, finish local loop break; } else if (isWhiteSpace()) { // ignore these, just whitespace } else if (elementStarts(strname)) { // <name> context = readElementText(); // </name> } else if (elementStarts(strmessage)) { // <message> TranslatorMessage::References refs; QString currentMsgFile = currentFile; TranslatorMessage msg; msg.setId(attributes().value(strid).toString()); msg.setContext(context); msg.setType(TranslatorMessage::Finished); msg.setPlural(attributes().value(strnumerus) == stryes); const QStringRef &utf8Attr = attributes().value(strutf8); msg.setNonUtf8(utf8Attr == strboth); msg.setUtf8(msg.isNonUtf8() || utf8Attr == strtrue || attributes().value(strencoding) == strUtf8); while (!atEnd()) { readNext(); if (isEndElement()) { // </message> found, finish local loop msg.setReferences(refs); translator.append(msg); break; } else if (isWhiteSpace()) { // ignore these, just whitespace } else if (elementStarts(strsource)) { // <source>...</source> msg.setSourceText(readContents()); } else if (elementStarts(stroldsource)) { // <oldsource>...</oldsource> msg.setOldSourceText(readContents()); } else if (elementStarts(stroldcomment)) { // <oldcomment>...</oldcomment> msg.setOldComment(readContents()); } else if (elementStarts(strextracomment)) { // <extracomment>...</extracomment> msg.setExtraComment(readContents()); } else if (elementStarts(strtranslatorcomment)) { // <translatorcomment>...</translatorcomment> msg.setTranslatorComment(readContents()); } else if (elementStarts(strlocation)) { // <location/> QXmlStreamAttributes atts = attributes(); QString fileName = atts.value(strfilename).toString(); if (fileName.isEmpty()) { fileName = currentMsgFile; } else { if (refs.isEmpty()) currentFile = fileName; currentMsgFile = fileName; } const QString lin = atts.value(strline).toString(); if (lin.isEmpty()) { translator.setLocationsType(Translator::RelativeLocations); refs.append(TranslatorMessage::Reference(fileName, -1)); } else { bool bOK; int lineNo = lin.toInt(&bOK); if (bOK) { if (lin.startsWith(QLatin1Char('+')) || lin.startsWith(QLatin1Char('-'))) { lineNo = (currentLine[fileName] += lineNo); translator.setLocationsType(Translator::RelativeLocations); } else { translator.setLocationsType(Translator::AbsoluteLocations); } refs.append(TranslatorMessage::Reference(fileName, lineNo)); } } readContents(); } else if (elementStarts(strcomment)) { // <comment>...</comment> msg.setComment(readContents()); } else if (elementStarts(struserdata)) { // <userdata>...</userdata> msg.setUserData(readContents()); } else if (elementStarts(strtranslation)) { // <translation> QXmlStreamAttributes atts = attributes(); QStringRef type = atts.value(strtype); if (type == strunfinished) msg.setType(TranslatorMessage::Unfinished); else if (type == strobsolete) msg.setType(TranslatorMessage::Obsolete); if (msg.isPlural()) { QStringList translations; while (!atEnd()) { readNext(); if (isEndElement()) { break; } else if (isWhiteSpace()) { // ignore these, just whitespace } else if (elementStarts(strnumerusform)) { translations.append(readTransContents()); } else { handleError(); break; } } msg.setTranslations(translations); } else { msg.setTranslation(readTransContents()); } // </translation> } else if (isStartElement() && name().toString().startsWith(strextrans)) { // <extra-...> QString tag = name().toString(); msg.setExtra(tag.mid(6), readContents()); // </extra-...> } else { handleError(); } } // </message> } else { handleError(); } } // </context> } else { handleError(); } } // </TS> } else { handleError(); } } if (hasError()) { m_cd.appendError(errorString()); return false; } return true; }
static bool loadQPH(Translator &translator, QIODevice &dev, ConversionData &) { translator.setLocationsType(Translator::NoLocations); QPHReader reader(dev); return reader.read(translator); }