-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxibLocalizationPostprocessor.m
More file actions
63 lines (51 loc) · 2.78 KB
/
xibLocalizationPostprocessor.m
File metadata and controls
63 lines (51 loc) · 2.78 KB
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
// xibLocalizationPostprocessor.m
//
// Created by William Shipley on 4/14/08.
// Copyright © 2005-2009 Golden % Braeburn, LLC.
#import <Cocoa/Cocoa.h>
int main(int argc, const char *argv[]) {
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init]; {
if (argc != 3) {
fprintf(stderr, "Usage: %s inputfile outputfile\n", argv[0]);
exit (-1);
}
NSError *error = nil;
NSStringEncoding usedEncoding;
NSString *rawXIBStrings = [NSString stringWithContentsOfFile:[NSString stringWithUTF8String:argv[1]] usedEncoding:&usedEncoding error:&error];
if (error) {
fprintf(stderr, "Error reading %s: %s\n", argv[1], error.localizedDescription.UTF8String);
exit (-1);
}
NSMutableString *outputStrings = [NSMutableString string];
NSUInteger lineCount = 0;
NSString *lastComment = nil;
for (NSString *line in [rawXIBStrings componentsSeparatedByString:@"\n"]) {
lineCount++;
if ([line hasPrefix:@"/*"]) { // eg: /* Class = "NSMenuItem"; title = "Quit Library"; ObjectID = "136"; */
lastComment = line;
continue;
} else if (line.length == 0) {
lastComment = nil;
continue;
} else if ([line hasPrefix:@"\""] && [line hasSuffix:@"\";"]) { // eg: "136.title" = "Quit Library";
NSRange quoteEqualsQuoteRange = [line rangeOfString:@"\" = \""];
if (quoteEqualsQuoteRange.length && NSMaxRange(quoteEqualsQuoteRange) < line.length - 1) {
if (lastComment) {
[outputStrings appendString:lastComment];
[outputStrings appendString:@"\n"];
}
NSString *stringNeedingLocalization = [line substringFromIndex:NSMaxRange(quoteEqualsQuoteRange)]; // chop off leading: "blah" = "
stringNeedingLocalization = [stringNeedingLocalization substringToIndex:stringNeedingLocalization.length - 2]; // chop off trailing: ";
[outputStrings appendFormat:@"\"%@\" = \"%@\";\n\n", stringNeedingLocalization, stringNeedingLocalization];
continue;
}
}
NSLog(@"Warning: skipped garbage input line %lu, contents: \"%@\"", (unsigned long)lineCount, line);
}
if (outputStrings.length && ![outputStrings writeToFile:[NSString stringWithUTF8String:argv[2]] atomically:NO encoding:NSUTF8StringEncoding error:&error]) {
fprintf(stderr, "Error writing %s: %s\n", argv[2], error.localizedDescription.UTF8String);
exit (-1);
}
} [autoreleasePool release];
return 0;
}