Created
June 27, 2014 18:09
-
-
Save douglashill/644f3b468347f2bc1f77 to your computer and use it in GitHub Desktop.
A rough guess at the implementation of +[NSBundle preferredLocalizationsFromArray:]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSArray *)dh_preferredLocalizationsFromArray:(NSArray *)requestedLocalisations | |
{ | |
NSMutableSet *possibleLocalisations = [NSMutableSet setWithArray:requestedLocalisations]; | |
BOOL shouldLimitToAvailableLocalisations = YES; // Set to YES to match what NSBundle seems to do, or NO to be more like the documentation. | |
if (shouldLimitToAvailableLocalisations) { | |
[possibleLocalisations intersectSet:[NSSet setWithArray:[[NSBundle mainBundle] localizations]]]; | |
} | |
NSMutableArray *preferredLocalisations = [NSMutableArray array]; | |
for (NSString * loc in [NSLocale preferredLanguages]) { | |
if ([possibleLocalisations containsObject:loc]) { | |
[preferredLocalisations addObject:loc]; | |
#warning Need to check it finds a general localisation. | |
NSString * const generalLoc = [loc substringToIndex:[loc rangeOfString:@"-"].location]; | |
if ([possibleLocalisations containsObject:generalLoc]) { | |
[preferredLocalisations addObject:generalLoc]; | |
} | |
break; | |
} | |
} | |
return preferredLocalisations; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment