forked from shanselman/Psuedoizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Psuedoizer.cs
392 lines (364 loc) · 9.98 KB
/
Psuedoizer.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
381
382
383
384
385
386
387
388
389
390
391
392
using System;
using System.Resources;
using System.IO;
using System.Collections;
using System.Text;
namespace Pseudo.Globalization
{
///Takes an English resource file (resx) and creates an artificial");/
///but still readable Euro-like language to exercise your i18n code");
///without a formal translation.");
class Psuedoizer
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Psuedoizer: Adapted from MSDN BugSlayer 2004-Apr i18n Article.");
if(args.Length < 2)
{
Console.WriteLine("Purpose: Takes an English resource file (resx) and creates an artificial");
Console.WriteLine(" but still readable Euro-like language to exercise your i18n code");
Console.WriteLine(" without a formal translation.");
Console.WriteLine(String.Empty);
Console.WriteLine("Psuedoizer.exe infile outfile [/b] [/a]");
Console.WriteLine(" Example:");
Console.WriteLine(" Psuedoizer.exe strings.en.resx strings.ja-JP.resx");
Console.WriteLine(" /b - Include blank resources");
Console.WriteLine(" /a - Append only new entries");
System.Environment.Exit(1);
}
string fileName = args[0];
string fileSaveName = args[1];
bool IncludeBlankResources = (args.Length >= 3 && args[2] == "/b");
bool appenOnlyNewEntries = false;
for(int i = 2; i < args.Length; i++)
{
if (args[i] != @"/a") continue;
appenOnlyNewEntries = true;
break;
}
// Open the input file.
ResXResourceReader reader = new ResXResourceReader ( fileName ) ;
try
{
// Get the enumerator. If this throws an ArguementException
// it means the file is not a .RESX file.
IDictionaryEnumerator enumerator = reader.GetEnumerator ( ) ;
// Allocate the list for this instance.
SortedList textResourcesList = new SortedList ( ) ;
// Run through the file looking for only true text related
// properties and only those with values set.
foreach ( DictionaryEntry dic in reader )
{
// Only consider this entry if the value is something.
if ( null != dic.Value )
{
// Is this a System.String.
if ( "System.String" == dic.Value.GetType().ToString())
{
String KeyString = dic.Key.ToString ( ) ;
// Make sure the key name does not start with the
// "$" or ">>" meta characters and is not an empty
// string (or we're explicitly including empty strings).
if ( ( false == KeyString.StartsWith ( ">>" ) ) &&
( false == KeyString.StartsWith ( "$" ) ) &&
( IncludeBlankResources || "" != dic.Value.ToString() ) )
{
// We've got a winner.
textResourcesList.Add ( dic.Key , Psuedoizer.ConvertToFakeInternationalized(dic.Value.ToString()) ) ;
}
// Special case the Windows Form "$this.Text" or
// I don't get the form titles.
if ( 0 == String.Compare ( KeyString,"$this.Text"))
{
textResourcesList.Add ( dic.Key , Psuedoizer.ConvertToFakeInternationalized(dic.Value.ToString()) ) ;
}
}
}
}
// It's entirely possible that there are no text strings in the
// .ResX file.
if ( textResourcesList.Count > 0 )
{
if ( null != fileSaveName )
{
//Check if we are only appending new ones
//check if we are only appending
if ( appenOnlyNewEntries && File.Exists(fileSaveName) )
{
// Open the output file.
ResXResourceReader outReader = new ResXResourceReader( fileSaveName );
foreach ( DictionaryEntry outdic in outReader )
{
if ( textResourcesList.ContainsKey( outdic.Key.ToString() ))
{
textResourcesList[outdic.Key.ToString()] = outdic.Value.ToString();
}
}
outReader.Close();
}
// Create the new file.
ResXResourceWriter writer =
new ResXResourceWriter ( fileSaveName ) ;
foreach ( DictionaryEntry textdic in textResourcesList )
{
writer.AddResource ( textdic.Key.ToString(), textdic.Value.ToString());
}
writer.Generate ( ) ;
writer.Close ( ) ;
Console.WriteLine("Converted " + textResourcesList.Count + " text resource(s).");
}
}
else
{
Console.Write("WARNING: No text resources found in " + fileName);
System.Environment.Exit(2);
}
}
catch(Exception e)
{
Console.Write(e.ToString());
System.Environment.Exit(1);
}
}
/// <summary>
/// Converts a string to a pseudo internationized string.
/// </summary>
/// <remarks>
/// Primarily for latin based languages. This will need updating to
/// work with Eastern languages.
/// </remarks>
/// <param name="inputString">
/// The string to use as a base.
/// </param>
/// <returns>
/// A longer and twiddled string.
/// </returns>
public static String ConvertToFakeInternationalized ( String inputString )
{
// Calculate the extra space necessary for pseudo
// internationalization. The rules, according to "Developing
// International Software" is that < 10 characters you should grow
// by 400% while >= 10 characters should grow by 30%.
int OrigLen = inputString.Length ;
int PseudoLen = 0 ;
if ( OrigLen < 10 )
{
PseudoLen = (OrigLen * 4) + OrigLen ;
}
else
{
PseudoLen = ((int)(OrigLen * 0.3)) + OrigLen ;
}
StringBuilder sb = new StringBuilder ( PseudoLen ) ;
// The pseudo string will always start with a "[" and end
// with a "]" so you can tell if strings are not built
// correctly in the UI.
sb.Append ( "[" ) ;
bool waitingForEndBrace = false;
bool waitingForGreaterThan = false;
foreach ( Char currChar in inputString )
{
switch ( currChar )
{
case '{' :
waitingForEndBrace = true;
break;
case '}' :
waitingForEndBrace = false;
break;
case '<' :
waitingForGreaterThan = true;
break;
case '>' :
waitingForGreaterThan = false;
break;
}
if(waitingForEndBrace || waitingForGreaterThan)
{
sb.Append(currChar);
continue;
}
switch ( currChar )
{
case 'A' :
sb.Append ( 'Å' ) ;
break ;
case 'B' :
sb.Append ( 'ß') ;
break ;
case 'C' :
sb.Append ( 'C' ) ;
break ;
case 'D' :
sb.Append ( 'Đ' ) ;
break ;
case 'E' :
sb.Append ( 'Ē' ) ;
break ;
case 'F' :
sb.Append ( 'F' ) ;
break ;
case 'G' :
sb.Append ( 'Ğ' ) ;
break ;
case 'H' :
sb.Append ( 'Ħ' ) ;
break ;
case 'I' :
sb.Append ( 'Ĩ' ) ;
break ;
case 'J' :
sb.Append ( 'Ĵ' ) ;
break ;
case 'K' :
sb.Append ( 'Ķ' ) ;
break ;
case 'L' :
sb.Append ( 'Ŀ' ) ;
break ;
case 'M' :
sb.Append ( 'M' ) ;
break ;
case 'N' :
sb.Append ( 'Ń' ) ;
break ;
case 'O' :
sb.Append ( 'Ø' ) ;
break ;
case 'P' :
sb.Append ( 'P' ) ;
break ;
case 'Q' :
sb.Append ( 'Q' ) ;
break ;
case 'R' :
sb.Append ( 'Ŗ' ) ;
break ;
case 'S' :
sb.Append ( 'Ŝ' ) ;
break ;
case 'T' :
sb.Append ( 'Ŧ' ) ;
break ;
case 'U' :
sb.Append ( 'Ů' ) ;
break ;
case 'V' :
sb.Append ( 'V' ) ;
break ;
case 'W' :
sb.Append ( 'Ŵ' ) ;
break ;
case 'X' :
sb.Append ( 'X' ) ;
break ;
case 'Y' :
sb.Append ( 'Ÿ' ) ;
break ;
case 'Z' :
sb.Append ( 'Ż' ) ;
break ;
case 'a' :
sb.Append ( 'ä' ) ;
break ;
case 'b' :
sb.Append ( 'þ') ;
break ;
case 'c' :
sb.Append ( 'č' ) ;
break ;
case 'd' :
sb.Append ( 'đ' ) ;
break ;
case 'e' :
sb.Append ( 'ę' ) ;
break ;
case 'f' :
sb.Append ( 'ƒ' ) ;
break ;
case 'g' :
sb.Append ( 'ģ' ) ;
break ;
case 'h' :
sb.Append ( 'ĥ' ) ;
break ;
case 'i' :
sb.Append ( 'į' ) ;
break ;
case 'j' :
sb.Append ( 'ĵ' ) ;
break ;
case 'k' :
sb.Append ( 'ĸ' ) ;
break ;
case 'l' :
sb.Append ( 'ľ' ) ;
break ;
case 'm' :
sb.Append ( 'm' ) ;
break ;
case 'n' :
sb.Append ( 'ʼn' ) ;
break ;
case 'o' :
sb.Append ( 'ő' ) ;
break ;
case 'p' :
sb.Append ( 'p' ) ;
break ;
case 'q' :
sb.Append ( 'q' ) ;
break ;
case 'r' :
sb.Append ( 'ř' ) ;
break ;
case 's' :
sb.Append ( 'ş' ) ;
break ;
case 't' :
sb.Append ( 'ŧ' ) ;
break ;
case 'u' :
sb.Append ( 'ū' ) ;
break ;
case 'v' :
sb.Append ( 'v' ) ;
break ;
case 'w' :
sb.Append ( 'ŵ' ) ;
break ;
case 'x' :
sb.Append ( 'χ' ) ;
break ;
case 'y' :
sb.Append ( 'y' ) ;
break ;
case 'z' :
sb.Append ( 'ž' ) ;
break ;
default:
sb.Append ( currChar ) ;
break ;
}
}
// Poke on extra text to fill out the string.
const String PadStr = " !!!" ;
int PadCount = (PseudoLen - OrigLen - 2) / PadStr.Length ;
if ( PadCount < 2 )
{
PadCount = 2 ;
}
for ( int x = 0 ; x < PadCount ; x++ )
{
sb.Append ( PadStr ) ;
}
// Pop on the trailing "]"
sb.Append ( "]" ) ;
return ( sb.ToString ( ) ) ;
}
}
}