diff options
author | Abhi Beckert <abhi@Twist-of-Lemon-2.local> | 2015-05-16 08:06:06 +1000 |
---|---|---|
committer | Abhi Beckert <abhi@Twist-of-Lemon-2.local> | 2015-05-16 08:06:06 +1000 |
commit | 57a6f6c73bdaa202164645370d37fcbe5d14a092 (patch) | |
tree | dd30aa6156064f1d4c0e10ea87059625470fc2f9 /Source/xibLocalizationPostprocessor.m | |
parent | b5e972f4504043dfb9c358e272e93fb59ae2127f (diff) | |
parent | 0f0c43eb74408b6a65a42e2c6fd46f4142ef8e3f (diff) | |
download | sequelpro-57a6f6c73bdaa202164645370d37fcbe5d14a092.tar.gz sequelpro-57a6f6c73bdaa202164645370d37fcbe5d14a092.tar.bz2 sequelpro-57a6f6c73bdaa202164645370d37fcbe5d14a092.zip |
Merge remote-tracking branch 'sequelpro/master'
Diffstat (limited to 'Source/xibLocalizationPostprocessor.m')
-rw-r--r-- | Source/xibLocalizationPostprocessor.m | 76 |
1 files changed, 68 insertions, 8 deletions
diff --git a/Source/xibLocalizationPostprocessor.m b/Source/xibLocalizationPostprocessor.m index ad2e152d..1806d6b2 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,41 @@ 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]; + + NSUInteger lineCount = 0; + //don't try [NSString enumerateLines...] here. It supports some obscure Unicode line breaks! + for (NSString *line in [input componentsSeparatedByString:@"\n"]) { + lineCount++; + + if (line.length == 0 || [line hasPrefix:@"/*"]) { + continue; + } + + if ([line hasPrefix:@"\""] && [line hasSuffix:@"\";"]) { // eg: "136.title" = "Quit Library"; + + NSRange quoteEqualsQuoteRange = [line rangeOfString:@"\" = \""]; + if (quoteEqualsQuoteRange.location != NSNotFound && 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]; + continue; + } + } + + NSLog(@"Warning: skipped garbage trans line %lu, contents: \"%@\"", (unsigned long)lineCount, line); + } + + return result; +} |