| 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 | #include <QtCore/QCoreApplication> |
| 7 | #include <QtCore/QDebug> |
| 8 | #include <QtCore/QString> |
| 9 | #include <QtCore/QStringList> |
| 10 | #include <QtCore/QTranslator> |
| 11 | #include <QtCore/QLibraryInfo> |
| 12 | |
| 13 | #include <iostream> |
| 14 | |
| 15 | QT_USE_NAMESPACE |
| 16 | |
| 17 | using namespace Qt::Literals::StringLiterals; |
| 18 | |
| 19 | static int usage(const QStringList &args) |
| 20 | { |
| 21 | Q_UNUSED(args); |
| 22 | |
| 23 | QString loaders; |
| 24 | QString line(" %1 - %2\n"_L1 ); |
| 25 | for (const Translator::FileFormat &format : std::as_const(t&: Translator::registeredFileFormats())) |
| 26 | loaders += line.arg(a: format.extension, fieldWidth: -5).arg(a: format.description()); |
| 27 | |
| 28 | std::cout << qPrintable(QStringLiteral("\nUsage:\n" |
| 29 | " lconvert [options] <infile> [<infile>...]\n\n" |
| 30 | "lconvert is part of Qt's Linguist tool chain. It can be used as a\n" |
| 31 | "stand-alone tool to convert and filter translation data files.\n" |
| 32 | "The following file formats are supported:\n\n%1\n" |
| 33 | "If multiple input files are specified, they are merged with\n" |
| 34 | "translations from later files taking precedence.\n\n" |
| 35 | "Options:\n" |
| 36 | " -h\n" |
| 37 | " -help Display this information and exit.\n\n" |
| 38 | " -i <infile>\n" |
| 39 | " -input-file <infile>\n" |
| 40 | " Specify input file. Use if <infile> might start with a dash.\n" |
| 41 | " This option can be used several times to merge inputs.\n" |
| 42 | " May be '-' (standard input) for use in a pipe.\n\n" |
| 43 | " -o <outfile>\n" |
| 44 | " -output-file <outfile>\n" |
| 45 | " Specify output file. Default is '-' (standard output).\n\n" |
| 46 | " -if <informat>\n" |
| 47 | " -input-format <format>\n" |
| 48 | " Specify input format for subsequent <infile>s.\n" |
| 49 | " The format is auto-detected from the file name and defaults to 'ts'.\n\n" |
| 50 | " -of <outformat>\n" |
| 51 | " -output-format <outformat>\n" |
| 52 | " Specify output format. See -if.\n\n" |
| 53 | " -drop-tags <regexp>\n" |
| 54 | " Drop named extra tags when writing TS or XLIFF files.\n" |
| 55 | " May be specified repeatedly.\n\n" |
| 56 | " -drop-translations\n" |
| 57 | " Drop existing translations and reset the status to 'unfinished'.\n" |
| 58 | " Note: this implies --no-obsolete.\n\n" |
| 59 | " -source-language <language>[_<region>]\n" |
| 60 | " Specify/override the language of the source strings. Defaults to\n" |
| 61 | " POSIX if not specified and the file does not name it yet.\n\n" |
| 62 | " -target-language <language>[_<region>]\n" |
| 63 | " Specify/override the language of the translation.\n" |
| 64 | " The target language is guessed from the file name if this option\n" |
| 65 | " is not specified and the file contents name no language yet.\n\n" |
| 66 | " -no-obsolete\n" |
| 67 | " Drop obsolete messages.\n\n" |
| 68 | " -no-finished\n" |
| 69 | " Drop finished messages.\n\n" |
| 70 | " -no-untranslated\n" |
| 71 | " Drop untranslated messages.\n\n" |
| 72 | " -sort-contexts\n" |
| 73 | " Sort contexts in output TS file alphabetically.\n\n" |
| 74 | " -sort-messages\n" |
| 75 | " Sort messages in a context alphabetically in TS files.\n\n" |
| 76 | " -locations {absolute|relative|none}\n" |
| 77 | " Override how source code references are saved in TS files.\n" |
| 78 | " Default is absolute.\n\n" |
| 79 | " -no-ui-lines\n" |
| 80 | " Drop line numbers from references to UI files.\n\n" |
| 81 | " -pluralonly\n" |
| 82 | " Drop non-plural form messages.\n\n" |
| 83 | " -verbose\n" |
| 84 | " be a bit more verbose\n\n" |
| 85 | "Long options can be specified with only one leading dash, too.\n\n" |
| 86 | "Return value:\n" |
| 87 | " 0 on success\n" |
| 88 | " 1 on command line parse failures\n" |
| 89 | " 2 on read failures\n" |
| 90 | " 3 on write failures\n" ).arg(loaders)); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | struct File |
| 95 | { |
| 96 | QString name; |
| 97 | QString format; |
| 98 | }; |
| 99 | |
| 100 | int main(int argc, char *argv[]) |
| 101 | { |
| 102 | QCoreApplication app(argc, argv); |
| 103 | #ifndef QT_BOOTSTRAPPED |
| 104 | #ifndef Q_OS_WIN32 |
| 105 | QTranslator translator; |
| 106 | QTranslator qtTranslator; |
| 107 | QString resourceDir = QLibraryInfo::path(p: QLibraryInfo::TranslationsPath); |
| 108 | if (translator.load(filename: "linguist_en"_L1 , directory: resourceDir) |
| 109 | && qtTranslator.load(filename: "qt_en"_L1 , directory: resourceDir)) { |
| 110 | app.installTranslator(messageFile: &translator); |
| 111 | app.installTranslator(messageFile: &qtTranslator); |
| 112 | } |
| 113 | #endif // Q_OS_WIN32 |
| 114 | #endif |
| 115 | |
| 116 | QStringList args = app.arguments(); |
| 117 | QList<File> inFiles; |
| 118 | QString inFormat("auto"_L1 ); |
| 119 | QString outFileName; |
| 120 | QString outFormat("auto"_L1 ); |
| 121 | QString targetLanguage; |
| 122 | QString sourceLanguage; |
| 123 | bool dropTranslations = false; |
| 124 | bool noObsolete = false; |
| 125 | bool noFinished = false; |
| 126 | bool noUntranslated = false; |
| 127 | bool verbose = false; |
| 128 | bool noUiLines = false; |
| 129 | bool pluralOnly = false; |
| 130 | Translator::LocationsType locations = Translator::DefaultLocations; |
| 131 | |
| 132 | ConversionData cd; |
| 133 | Translator tr; |
| 134 | |
| 135 | for (int i = 1; i < args.size(); ++i) { |
| 136 | const QString &arg = args.at(i); |
| 137 | if (arg.startsWith(s: "--"_L1 )) |
| 138 | args[i].remove(i: 0, len: 1); |
| 139 | if (arg == "-o"_L1 || arg == "-output-file"_L1 ) { |
| 140 | if (++i >= args.size()) |
| 141 | return usage(args); |
| 142 | outFileName = args[i]; |
| 143 | } else if (arg == "-of"_L1 || arg == "-output-format"_L1 ) { |
| 144 | if (++i >= args.size()) |
| 145 | return usage(args); |
| 146 | outFormat = args[i]; |
| 147 | } else if (arg == "-i"_L1 || arg == "-input-file"_L1 ) { |
| 148 | if (++i >= args.size()) |
| 149 | return usage(args); |
| 150 | File file; |
| 151 | file.name = args[i]; |
| 152 | file.format = inFormat; |
| 153 | inFiles.append(t: file); |
| 154 | } else if (arg == "-if"_L1 || arg == "-input-format"_L1 ) { |
| 155 | if (++i >= args.size()) |
| 156 | return usage(args); |
| 157 | inFormat = args[i]; |
| 158 | } else if (arg == "-drop-tag"_L1 || arg == "-drop-tags"_L1 ) { |
| 159 | if (++i >= args.size()) |
| 160 | return usage(args); |
| 161 | cd.m_dropTags.append(t: args[i]); |
| 162 | } else if (arg == "-drop-translations"_L1 ) { |
| 163 | dropTranslations = true; |
| 164 | } else if (arg == "-target-language"_L1 ) { |
| 165 | if (++i >= args.size()) |
| 166 | return usage(args); |
| 167 | targetLanguage = args[i]; |
| 168 | } else if (arg == "-source-language"_L1 ) { |
| 169 | if (++i >= args.size()) |
| 170 | return usage(args); |
| 171 | sourceLanguage = args[i]; |
| 172 | } else if (arg.startsWith(s: "-h"_L1 )) { |
| 173 | usage(args); |
| 174 | return 0; |
| 175 | } else if (arg == "-no-obsolete"_L1 ) { |
| 176 | noObsolete = true; |
| 177 | } else if (arg == "-no-finished"_L1 ) { |
| 178 | noFinished = true; |
| 179 | } else if (arg == "-no-untranslated"_L1 ) { |
| 180 | noUntranslated = true; |
| 181 | } else if (arg == "-sort-contexts"_L1 ) { |
| 182 | cd.m_sortContexts = true; |
| 183 | } else if (arg == "-sort-messages"_L1 ) { |
| 184 | cd.m_sortMessages = true; |
| 185 | } else if (arg == "-locations"_L1 ) { |
| 186 | if (++i >= args.size()) |
| 187 | return usage(args); |
| 188 | if (args[i] == "none"_L1 ) |
| 189 | locations = Translator::NoLocations; |
| 190 | else if (args[i] == "relative"_L1 ) |
| 191 | locations = Translator::RelativeLocations; |
| 192 | else if (args[i] == "absolute"_L1 ) |
| 193 | locations = Translator::AbsoluteLocations; |
| 194 | else |
| 195 | return usage(args); |
| 196 | } else if (arg == "-no-ui-lines"_L1 ) { |
| 197 | noUiLines = true; |
| 198 | } else if (arg == "-pluralonly"_L1 ) { |
| 199 | pluralOnly = true; |
| 200 | } else if (arg == "-verbose"_L1 ) { |
| 201 | verbose = true; |
| 202 | } else if (arg.startsWith(c: u'-')) { |
| 203 | return usage(args); |
| 204 | } else { |
| 205 | File file; |
| 206 | file.name = args[i]; |
| 207 | file.format = inFormat; |
| 208 | inFiles.append(t: file); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if (inFiles.isEmpty()) |
| 213 | return usage(args); |
| 214 | |
| 215 | tr.setLanguageCode(Translator::guessLanguageCodeFromFileName(fileName: inFiles[0].name)); |
| 216 | |
| 217 | if (!tr.load(filename: inFiles[0].name, err&: cd, format: inFiles[0].format)) { |
| 218 | std::cerr << qPrintable(cd.error()); |
| 219 | return 2; |
| 220 | } |
| 221 | tr.reportDuplicates(dupes: tr.resolveDuplicates(), fileName: inFiles[0].name, verbose); |
| 222 | |
| 223 | for (int i = 1; i < inFiles.size(); ++i) { |
| 224 | Translator tr2; |
| 225 | if (!tr2.load(filename: inFiles[i].name, err&: cd, format: inFiles[i].format)) { |
| 226 | std::cerr << qPrintable(cd.error()); |
| 227 | return 2; |
| 228 | } |
| 229 | tr2.reportDuplicates(dupes: tr2.resolveDuplicates(), fileName: inFiles[i].name, verbose); |
| 230 | for (int j = 0; j < tr2.messageCount(); ++j) |
| 231 | tr.replaceSorted(msg: tr2.message(i: j)); |
| 232 | |
| 233 | tr.appendDependencies(dependencies: tr2.dependencies()); |
| 234 | } |
| 235 | |
| 236 | for (const auto &file: inFiles) { |
| 237 | tr.satisfyDependency(file: file.name, format: file.format); |
| 238 | } |
| 239 | |
| 240 | if (!targetLanguage.isEmpty()) |
| 241 | tr.setLanguageCode(targetLanguage); |
| 242 | if (!sourceLanguage.isEmpty()) |
| 243 | tr.setSourceLanguageCode(sourceLanguage); |
| 244 | if (noObsolete) |
| 245 | tr.stripObsoleteMessages(); |
| 246 | if (noFinished) |
| 247 | tr.stripFinishedMessages(); |
| 248 | if (noUntranslated) |
| 249 | tr.stripUntranslatedMessages(); |
| 250 | if (dropTranslations) |
| 251 | tr.dropTranslations(); |
| 252 | if (noUiLines) |
| 253 | tr.dropUiLines(); |
| 254 | if (pluralOnly) |
| 255 | tr.stripNonPluralForms(); |
| 256 | if (locations != Translator::DefaultLocations) |
| 257 | tr.setLocationsType(locations); |
| 258 | |
| 259 | tr.normalizeTranslations(cd); |
| 260 | if (!cd.errors().isEmpty()) { |
| 261 | std::cerr << qPrintable(cd.error()); |
| 262 | cd.clearErrors(); |
| 263 | } |
| 264 | if (!tr.save(filename: outFileName, err&: cd, format: outFormat)) { |
| 265 | std::cerr << qPrintable(cd.error()); |
| 266 | return 3; |
| 267 | } |
| 268 | return 0; |
| 269 | } |
| 270 | |