If you like my tutorials, you will love my book . It is chockful of advanced programming techniques and the only comprehensive barcode reference for serious iOS developers.
Our DNA is written in Objective-C

Translating NSTimeZone Geopolitical IDs

For my new version of Summertime I am building a time zone picker. You can get the known time zone names from the NSTimeZone class, but unfortunately Apple does not give us any localization of these. The localizedName:local: method gives you localized names of the time zones itself (e.g. “Pacific Standard Time”) in various formats. But what I found to be missing is a way to have the geo names localizable as well.

If I have my iPhone set to German I want to find my timezone by entering “Wien”, not “Vienna”.

My initial thought was to keep this to myself, but since I only speak German and English I can never hope to have the translations be perfect unless I would pay several translators to comb through them. And you know, Google Translate is great, but not 100%. So I started a new Open Source project on GitHub: NSTimeZone+Localization which aims to remedy this.

I wrote a quick command line tool which passes the knownTimeZoneNames through Google Translate for German, Spanish, French and Dutch as these are the languages that Summertime is currently localized in. More languages can be easily added the same way, but in general I would want to ask you a favor: if you speak any of these languages please have a quick look if you see any spelling or translation mistakes.

I had a modified version of LKGoogleTranslator handy which I had to modify again to include my API key. This still uses version 1.0 of the API while 2.0 is a newer one, but since it worked I did not bother.

#import "LKGoogleTranslator.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	LKGoogleTranslator *translator = [[LKGoogleTranslator alloc] init];
	NSMutableString *string = [NSMutableString string];
	for (NSString *timezoneName in [NSTimeZone knownTimeZoneNames])
		// replace slashes with commas, works better in Google Translate
		NSString *searchName = [timezoneName stringByReplacingOccurrencesOfString:@"/" withString:@","];
		searchName = [searchName stringByReplacingOccurrencesOfString:@"_" withString:@" "];
		NSString *translation = [translator translateText:searchName fromLanguage:@"en" toLanguage:@"nl"];
		translation = [translation stringByReplacingOccurrencesOfString:@"," withString:@"/"];
		translation = [translation stringByReplacingOccurrencesOfString:@"/ " withString:@"/"];
		translation = [translation stringByReplacingOccurrencesOfString:@" " withString:@"_"];
		[string appendFormat:@"\"%@\" = \"%@\";\n", timezoneName, translation];
		NSLog(@"%@ = %@", timezoneName, translation);
	NSLog(@"%@", string);
	[translator release];
    [pool drain];
    return 0;

The resulting string blog I just pasted into strings files. With the help of a small category extension on NSTimeZone you can now get the localized geopolitical name.

#import "NSTimeZone+Translation.h"
@implementation NSTimeZone (Translation)
- (NSString *)localizedGeoName
	return NSLocalizedStringFromTable(, @"LocalizableTimezones", @"NSTimeZone+Translation");

This works because the localizable strings files for this are called LocalizableTimezones.strings. Instead of the regular NSLocalizedString macro I’m using NSLocalizedStringFromTable with the middle parameter being the name of the strings file minus extension.

You can use this functionality in your apps if in turn you feed back errors to me so that I can correct them. If you want additional languages to be added, let me know. I’ll make the same starting list via Google Translate for any newly requested languages and then you can manually polish them.

Categories: Projects

%d bloggers like this: