aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Source/xibLocalizationPostprocessor.m74
1 files changed, 66 insertions, 8 deletions
diff --git a/Source/xibLocalizationPostprocessor.m b/Source/xibLocalizationPostprocessor.m
index ad2e152d..8efa545a 100644
--- a/Source/xibLocalizationPostprocessor.m
+++ b/Source/xibLocalizationPostprocessor.m
@@ -5,20 +5,37 @@
#import <Cocoa/Cocoa.h>
+NSDictionary *load_kv_pairs(NSString *input);
int main(int argc, const char *argv[])
{
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc] init]; {
- if (argc != 3) {
- fprintf(stderr, "Usage: %s inputfile outputfile\n", argv[0]);
+ if (argc != 3 && argc != 4) {
+ fprintf(stderr, "Usage: xibLocalizationPostprocessor inputfile outputfile (Replace IB keys with their English string value)\n");
+ fprintf(stderr, " xibLocalizationPostprocessor transfile inputfile outputfile (Reverse mode: Change English keys back to IB keys in translation)\n");
exit (-1);
}
+
+ NSUInteger inputFileIndex = 1;
+ NSDictionary *translatedStrings = nil;
+ if(argc == 4) {
+ NSError *error = nil;
+ NSStringEncoding usedEncoding;
+ NSString *translatedFile = [NSString stringWithContentsOfFile:[NSString stringWithUTF8String:argv[1]] usedEncoding:&usedEncoding error:&error];
+ if (error) {
+ fprintf(stderr, "Error reading transfile %s: %s\n", argv[1], error.localizedDescription.UTF8String);
+ exit (-1);
+ }
+ translatedStrings = load_kv_pairs(translatedFile);
+
+ inputFileIndex++;
+ }
NSError *error = nil;
NSStringEncoding usedEncoding;
- NSString *rawXIBStrings = [NSString stringWithContentsOfFile:[NSString stringWithUTF8String:argv[1]] usedEncoding:&usedEncoding error:&error];
+ NSString *rawXIBStrings = [NSString stringWithContentsOfFile:[NSString stringWithUTF8String:argv[inputFileIndex]] usedEncoding:&usedEncoding error:&error];
if (error) {
- fprintf(stderr, "Error reading %s: %s\n", argv[1], error.localizedDescription.UTF8String);
+ fprintf(stderr, "Error reading inputfile %s: %s\n", argv[inputFileIndex], error.localizedDescription.UTF8String);
exit (-1);
}
@@ -44,9 +61,20 @@ int main(int argc, const char *argv[])
[outputStrings appendString:lastComment];
[outputStrings appendString:@"\n"];
}
- NSString *stringNeedingLocalization = [line substringFromIndex:NSMaxRange(quoteEqualsQuoteRange)]; // chop off leading: "blah" = "
+ 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];
+ if(translatedStrings) {
+ NSString *translation = [translatedStrings objectForKey:stringNeedingLocalization];
+ if(!translation) {
+ fprintf(stderr, "Warning: key \"%s\" not found in transfile.\n",[stringNeedingLocalization UTF8String]);
+ translation = stringNeedingLocalization; //fallback to untranslated
+ }
+ [outputStrings appendFormat:@"%@\" = \"%@\";\n\n", [line substringToIndex:quoteEqualsQuoteRange.location], translation];
+ }
+ else {
+ [outputStrings appendFormat:@"\"%@\" = \"%@\";\n\n", stringNeedingLocalization, stringNeedingLocalization];
+ }
+
continue;
}
}
@@ -54,9 +82,39 @@ int main(int argc, const char *argv[])
NSLog(@"Warning: skipped garbage input line %lu, contents: \"%@\"", (unsigned long)lineCount, line);
}
- if (outputStrings.length && ![outputStrings writeToFile:[NSString stringWithUTF8String:argv[2]] atomically:NO encoding:usedEncoding error:&error]) {
- fprintf(stderr, "Error writing %s: %s\n", argv[2], error.localizedDescription.UTF8String);
+ if (outputStrings.length && ![outputStrings writeToFile:[NSString stringWithUTF8String:argv[inputFileIndex+1]] atomically:NO encoding:usedEncoding error:&error]) {
+ fprintf(stderr, "Error writing %s: %s\n", argv[inputFileIndex+1], error.localizedDescription.UTF8String);
exit (-1);
}
} [autoreleasePool release];
}
+
+NSDictionary *load_kv_pairs(NSString *input)
+{
+ NSDictionary *result = [NSMutableDictionary dictionary];
+ __block NSUInteger lineCount = 0;
+ [input enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
+ lineCount++;
+
+ if (line.length == 0 || [line hasPrefix:@"/*"]) {
+ return;
+ }
+
+ if ([line hasPrefix:@"\""] && [line hasSuffix:@"\";"]) { // eg: "136.title" = "Quit Library";
+
+ NSRange quoteEqualsQuoteRange = [line rangeOfString:@"\" = \""];
+ if (quoteEqualsQuoteRange.length && NSMaxRange(quoteEqualsQuoteRange) < line.length - 1) {
+ NSRange keyRange = NSMakeRange(1,quoteEqualsQuoteRange.location - 1); //the first " is always at pos. 0 (we checked that above)
+ NSString *key = [line substringWithRange:keyRange];
+ NSString *value = [line substringFromIndex:NSMaxRange(quoteEqualsQuoteRange)]; // chop off leading: "blah" = "
+ value = [value substringToIndex:[value length] - 2]; // chop off trailing: ";
+ [result setValue:value forKey:key];
+ return;
+ }
+ }
+
+ NSLog(@"Warning: skipped garbage trans line %lu, contents: \"%@\"", (unsigned long)lineCount, line);
+
+ }];
+ return result;
+}