views:

120

answers:

2

I can easily change a short region-code (en_US) into a longer string... but it there an easy way to also move in the other direction?

[displayInEnglish displayNameForKey:NSLocaleIdentifier value:regionCountryCode];

"en_US" becomes "English (United States)".

"English (United States)" becomes "en_US".

I currently store the short region-code in a database.... but when I show some aggregate results... I need to display the longer strings to the user.

Or should I just store the longer strings right in the database... and not even worry about "converting" them later?

I'm trying to show a "dollars total" for each country.

If you were a user... which would you more likely wish to see (for a currency-total list)?

  • A "French" total
  • A "France" total
  • A "French (France)" total
  • A "fr_Fr" total?
+1  A: 

One option is to build the map yourself and store it. You have all the codes you want, and you can convert them into the longer string, so just store the longer string => code mapping:

NSMutableDictionary * nameToCode = [NSMutableDictionary dictionary];
NSArray * codes = [NSArray arrayWithObjects:@"en_US", @"en_GB", @"fr_FR", @"pt_PT"];
NSLocale * english = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];

for (NSString * code in codes) {
  NSString * displayName = [english displayNameForKey:NSLocaleIdentifier value:code];
  [nameToCode setObject:code forKey:displayName];
}

Then you can do:

NSString * code = [nameToCode objectForKey:@"French (France)"];
Dave DeLong
A: 

I agree with David (who types faster than me): Build a lookup table. Simple. Cheap. quick. If you know that you will have already called displayInEnglish then structure as a cache rather than building a big table with unnecessary entries.

Andiih