forked from anderskaplan/Pseudolocalizer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
380 lines (315 loc) · 13.5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
namespace PseudoLocalizer
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using PseudoLocalizer.Core;
/// <summary>
/// Main class for the pseudo-localizer console application.
/// </summary>
public class Program
{
private List<string> _inputFiles = new List<string>();
public Program()
{
UseDefaultOptions = true;
}
public bool UseDefaultOptions { get; set; }
public bool EnableExtraLength { get; set; }
public bool EnableAccents { get; set; }
public bool EnableBrackets { get; set; }
public bool EnableMirror { get; set; }
public bool EnableUnderscores { get; set; }
public bool Force { get; set; }
public bool HasInputFiles
{
get { return _inputFiles.Count > 0; }
}
public string OutputCulture { get; set; }
public bool Overwrite { get; set; }
private static string FileVersion()
=> typeof(Program).Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
private static string InfoVersion()
{
string version = typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
string[] split = version.Split('+');
return $"{split[0]}+{split[1].Substring(0, 7)}";
}
public static void Main(string[] args)
{
var instance = new Program();
if (ParseArguments(args, instance))
{
instance.Run();
}
else
{
Console.WriteLine($"PseudoLocalize {InfoVersion()}");
Console.WriteLine();
Console.WriteLine("Usage: pseudo-localize [/l] [/a] [/b] [/m] [/u] [/c culture] file [file...]");
Console.WriteLine("Generates pseudo-localized versions of the specified input file(s).");
Console.WriteLine();
Console.WriteLine("The input files must be resource files in Resx or Xlf file format.");
Console.WriteLine("The output will be written to a file next to the original, with .qps-Ploc");
Console.WriteLine("(or the output culture you specify) appended to its name. For example, if");
Console.WriteLine("the input file is X:\\Foo\\Bar.resx, then the output file will be");
Console.WriteLine("X:\\Foo\\Bar.qps-Ploc.resx.");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" /h, --help Show command line help.");
Console.WriteLine(" /v, --version Show the version of the tool.");
Console.WriteLine(" /l, --lengthen Make all words 30% longer, to ensure that there is room for translations.");
Console.WriteLine(" /a, --accents Add accents on all letters so that non-localized text can be spotted.");
Console.WriteLine(" /b, --brackets Add brackets to show the start and end of each localized string.");
Console.WriteLine(" This makes it possible to spot cut off strings.");
Console.WriteLine(" /m, --mirror Reverse all words (\"mirror\").");
Console.WriteLine(" /u, --underscores Replace all characters with underscores.");
Console.WriteLine(" /c, --culture Use the following string as the culture code in the output file name(s).");
Console.WriteLine(" /o, --overwrite Overwrites the input file(s) with the pseudo-localized version.");
Console.WriteLine(" /f, --force Suppresses the confirmation prompt for the --overwrite option.");
Console.WriteLine();
Console.WriteLine("The default options, if none are given, are: /l /a /b.");
}
}
public void ClearInputFiles()
{
_inputFiles.Clear();
}
public void AddInputFile(string filePath)
{
_inputFiles.Add(filePath);
}
private static bool ParseArguments(string[] args, Program instance)
{
instance.ClearInputFiles();
instance.UseDefaultOptions = true;
instance.EnableAccents = false;
instance.EnableExtraLength = false;
instance.EnableBrackets = false;
instance.EnableMirror = false;
instance.EnableUnderscores = false;
instance.OutputCulture = "qps-Ploc";
for (var i = 0; i < args.Length; i++)
{
string arg = args[i];
// File paths may start with '/' on Linux,
// so if a path is a file, use it as such.
if (File.Exists(arg))
{
instance.AddInputFile(arg);
continue;
}
if (arg.StartsWith("/", StringComparison.Ordinal) ||
arg.StartsWith("-", StringComparison.Ordinal))
{
string name = arg.TrimStart('-', '/');
switch (name.ToUpperInvariant())
{
case "L":
case "LENGTHEN":
instance.EnableExtraLength = true;
instance.UseDefaultOptions = false;
break;
case "A":
case "ACCENTS":
instance.EnableAccents = true;
instance.UseDefaultOptions = false;
break;
case "B":
case "BRACKETS":
instance.EnableBrackets = true;
instance.UseDefaultOptions = false;
break;
case "C":
case "CULTURE":
if (i == args.Length -1)
{
Console.WriteLine("ERROR: No output culture specified.", arg);
return false;
}
string culture = args[i + 1];
if (culture.StartsWith("/", StringComparison.Ordinal) ||
culture.StartsWith("-", StringComparison.Ordinal))
{
Console.WriteLine("ERROR: No output culture specified.", arg);
return false;
}
instance.OutputCulture = culture;
i++; // Consumed, so skip
break;
case "F":
case "FORCE":
instance.Force = true;
break;
case "M":
case "MIRROR":
instance.EnableMirror = true;
instance.UseDefaultOptions = false;
break;
case "O":
case "OVERWRITE":
instance.Overwrite = true;
break;
case "U":
case "UNDERSCORES":
instance.EnableUnderscores = true;
instance.UseDefaultOptions = false;
break;
case "H":
case "HELP":
return false;
case "V":
case "VERSION":
Console.WriteLine(FileVersion());
Environment.Exit(0);
return false; // Never reached
default:
Console.WriteLine("ERROR: Unknown option \"{0}\".", arg);
return false;
}
}
else
{
instance.AddInputFile(arg);
}
}
return instance.HasInputFiles;
}
private void Run()
{
foreach (var filePath in _inputFiles)
{
var processor = GetProcessor(filePath);
ProcessFile(filePath, processor);
}
}
private IProcessor GetProcessor(string filePath)
{
string extension = Path.GetExtension(filePath);
if (string.Equals(".xlf", extension, StringComparison.OrdinalIgnoreCase))
{
return new XlfProcessor(OutputCulture);
}
else
{
return new ResxProcessor();
}
}
private void ProcessFile(string inputFileName, IProcessor processor)
{
try
{
string writtenFileName;
if (Overwrite)
{
if (!Force)
{
Console.WriteLine("The file {0} will be overwritten.", inputFileName);
Console.Write("Are you sure? [y/n]: ", inputFileName);
switch (Console.ReadLine().ToUpperInvariant())
{
case "Y":
case "YES":
break;
default:
return;
}
Console.WriteLine();
}
using (var inputStream = File.Open(inputFileName, FileMode.Open, FileAccess.ReadWrite))
using (var outputStream = new MemoryStream())
{
Transform(processor, inputStream, outputStream);
inputStream.Seek(0, SeekOrigin.Begin);
inputStream.SetLength(0);
outputStream.Seek(0, SeekOrigin.Begin);
outputStream.CopyTo(inputStream);
outputStream.Flush();
}
writtenFileName = inputFileName;
}
else
{
string outputFileName = GetOutputFileName(inputFileName);
using (var inputStream = File.OpenRead(inputFileName))
using (var outputStream = File.OpenWrite(outputFileName))
{
Transform(processor, inputStream, outputStream);
}
writtenFileName = outputFileName;
}
Console.WriteLine("The file {0} was written successfully.", writtenFileName);
}
catch (Exception ex)
{
if (ex is PathTooLongException ||
ex is FileNotFoundException ||
ex is DirectoryNotFoundException ||
ex is IOException ||
ex is SecurityException)
{
Console.WriteLine(ex.Message);
}
else
{
throw;
}
}
}
private void Transform(IProcessor processor, Stream inputStream, Stream outputStream)
{
ITransformer transformer = CreateTransformer();
processor.TransformString += (_, e) => transformer.Apply(e);
processor.Transform(inputStream, outputStream);
}
private ITransformer CreateTransformer()
{
var transformers = new List<ITransformer>();
if (EnableExtraLength || UseDefaultOptions)
{
transformers.Add(ExtraLength.Instance);
}
if (EnableAccents || UseDefaultOptions)
{
transformers.Add(Accents.Instance);
}
if (EnableBrackets || UseDefaultOptions)
{
transformers.Add(Brackets.Instance);
}
if (EnableMirror)
{
transformers.Add(Mirror.Instance);
}
if (EnableUnderscores)
{
transformers.Add(Underscores.Instance);
}
return new Pipeline(transformers);
}
private string GetOutputFileName(string inputFileName)
{
string baseFileName = Path.GetFileNameWithoutExtension(inputFileName);
try
{
string existingCulture = baseFileName.Split('.').LastOrDefault();
if (existingCulture != null &&
!baseFileName.StartsWith(existingCulture) &&
!string.Equals(CultureInfo.CreateSpecificCulture(existingCulture).TwoLetterISOLanguageName, "iv", StringComparison.Ordinal))
{
baseFileName = baseFileName.Substring(0, baseFileName.LastIndexOf('.'));
}
}
catch (CultureNotFoundException)
{
}
return Path.Combine(
Path.GetDirectoryName(inputFileName),
baseFileName + "." + OutputCulture + Path.GetExtension(inputFileName));
}
}
}