Ad

Our DNA is written in Swift
Jump

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(self.name, @"LocalizableTimezones", @"NSTimeZone+Translation");
}
 
@end

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

6 Comments »

  1. Nice post Oliver.

    I faced the same kind of problem when working on one of my own projects.

    As you mention, Google translate is not perfect. Especially when a city name has a different meaning when directly translated. For instance then small danish town of “Tarm” has a very different meaning when translated using Google 🙂

    I have found a free alternative to Google on geonames.org that are directly targeted at GIS data. They have a service that allows you to get the city name translated into a language of your choice, eg. for Vienna below:

    http://api.geonames.org/searchJSON?name=vienna&maxRows=1&featureClass=P&lang=de&username=demo

    You might need to change the username to something other than demo, since that’s limited to 30000 queries per day. You can create your own free account at their site in less than a minute.

    Cheers,
    Claus

  2. I had similar problem with Locale and Localization. As you know it’s different.
    NSDate translation like “Today” or “Tomorrow” depend on Region Format, but app translation depends on Localization – global iOS Language. As a result you can get main part of a text in local language, but Date part in English. It confuses a bit.

  3. Can’t add Russina translation because API doesn’t give me more than ~100 results. It returns NULL starting from Asia/..
    It’s a pity.

  4. The API is only limited it you don’t set an API key. I can add Russian if you proof-read it then.

  5. No, want to add my self.
    Where to set an API key?