Skip to content

Instantly share code, notes, and snippets.

@douglashill
Created June 27, 2014 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglashill/644f3b468347f2bc1f77 to your computer and use it in GitHub Desktop.
Save douglashill/644f3b468347f2bc1f77 to your computer and use it in GitHub Desktop.
A rough guess at the implementation of +[NSBundle preferredLocalizationsFromArray:]
+ (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