Tuesday, August 13, 2013

Testing iPhone translation coverage...

We are updating one of our apps to include translations.  Since I didn't do the NSLocalizableString() things from the beginning (5 years ago!) there is now some searching and updating.

We wanted to be able to test if everything had been covered, and figured out a very cool trick to do it. (Shawn, Pete and I; and a big part thanks to Pete being in England!)

I setup a British English translation for the project, and set my iPhone to use British English in the settings.  That way my iPhone still works in the language I know best, I'll live with it if it says "Colour" somewhere.



I made a very quick and dirty Mac program that took the regular Localizable.strings and changed so the British version is simply an uppercase copy of the regular text.

(Forgive the crappy formatting, Blogger doesn't do code and indents that well...)

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *text = [NSString stringWithContentsOfFile:@"/pathTo/English/Localizable.strings" encoding:NSUTF16StringEncoding error:nil]; 
NSMutableString *newText = [NSMutableString string];
NSArray *lines = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];for (int i=0; i<[lines count]; i++) {
NSArray *halves = [[lines objectAtIndex:i] componentsSeparatedByString:@"\" = \""]; 
if ([halves count]>1) {
//Does NOT take into account %f to %F format things...
         //But do fix some newline types...
         NSString    *rightHalf = [[halves objectAtIndex:1] uppercaseString];
         rightHalf = [rightHalf stringByReplacingOccurrencesOfString:@"\\N" withString:@"\\n"];         rightHalf = [rightHalf stringByReplacingOccurrencesOfString:@"\\R" withString:@"\\r"];         rightHalf = [rightHalf stringByReplacingOccurrencesOfString:@"\\T" withString:@"\\t"]; 
[newText appendString:[halves objectAtIndex:0]];
[newText appendString:@"\" = \""];
[newText appendString: rightHalf]; 
[newText appendString:@"\n"];
} else {
[newText appendString:[lines objectAtIndex:i]];
[newText appendString:@"\n"];
}
}
[newText writeToFile:@"/pathTo/British/Localizable.strings" atomically:YES encoding:NSUTF16StringEncoding error:nil];

}

Now its easy for us to run the app and see any items I've missed, like the tabs across the bottom.



PS: As a pretty awesome bonus, switching to British English in the settings changes Siri to be guy with an English accent.

No comments: