开发者

Converting "fr_FR" into "French (France)" in both directions. (Objective-C iPhone)

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 us开发者_运维问答er... 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?


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)"];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜