Re: Determining preferred localizations
Re: Determining preferred localizations
- Subject: Re: Determining preferred localizations
- From: Gregory Weston <email@hidden>
- Date: Thu, 22 Apr 2010 08:36:08 -0400
On Apr 22, 2010, at 7:50 AM, Rainer Brockerhoff wrote:
> At 12:02 -0700 21/04/10, email@hidden wrote:
>> From: Gregory Weston <email@hidden>
>> Date: Wed, 21 Apr 2010 09:59:48 -0400
>> Message-ID: <email@hidden>
>>
>> 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 (email@hidden)
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:
This email sent to email@hidden