On Apr 22, 2010, at 7:50 AM, Rainer Brockerhoff wrote:

> At 12:02 -0700 21/04/10, cocoa-dev-requ...@lists.apple.com wrote:
>> From: Gregory Weston <gwes...@mac.com>
>> Date: Wed, 21 Apr 2010 09:59:48 -0400
>> Message-ID: <a977463a-b8f0-4b24-9284-03012bee3...@mac.com>
>> 
>> I'm trying to display a localized list of attached displays, and getting 
>> unexpected results in Carbon and Cocoa when attempting to determine the best 
>> localization. I did all the obvious-to-me Google searches without finding 
>> much except a couple of other people over the years having similar issues 
>> and no real resolution. Sample code and results follow. Hoping someone can 
>> point me in the right direction.
>> ...
>> 
>> - (NSString*)bestLocalization:(NSArray*)inChoices
>> {
>> NSUserDefaults* theDefaults = [NSUserDefaults standardUserDefaults];
>> NSArray* theUserSettings = [theDefaults objectForKey:@"AppleLanguages"];
>> NSLog(@"User Settings: %@", theUserSettings);
>> 
>> NSLog(@"Choices: %@", inChoices);
>> 
>> NSArray* theBestOnes = [NSBundle preferredLocalizationsFromArray:inChoices];
>> NSLog(@"Chose: %@", theBestOnes);
>> 
>> return [theBestOnes objectAtIndex:0];
>> }
> 
> I can't double-check this right now, but I solved a similar problem this way:
> 
> NSArray* inChoices = [[NSBundle mainBundle] localizations];   // probably 
> that's what you're passing in?
> NSArray* theBestOnes = [NSBundle preferredLocalizationsFromArray:inChoices 
> forPreferences:[NSLocale preferredLanguages]];

Thanks, but no. What I'm passing in is the keys for the dictionary I get as a 
result of this code:

io_connect_t thePort = CGDisplayIOServicePort(theScreenNumber);
CFDictionaryRef theInfo = 
(CFDictionaryRef)IODisplayCreateInfoDictionary(thePort, 0);
CFDictionaryRef theNames = CFDictionaryGetValue(theInfo, 
CFSTR(kDisplayProductName));

theNames will look something like this:

"en_US" -> "Color LCD"
"en_GB" -> "Colour LCD"
"fr_FR" -> "LCD couleur"
....

My workaround for the moment is to use NSLocale's currentLocale method, and if 
I don't find a key matching that I'll loop over the preferred language list 
until I find a key that has the language under consideration as a prefix. 
Problem is that seems needlessly convoluted and if I do end up with a 
dictionary that has multiple locales for the same language I'll essentially be 
picking among them at random. As I'm typing I've realized that the rest of the 
system uses the language list in preference to the current locale, though, so I 
have to fix that anyway.

Greg
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to