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