Skip to content

Instantly share code, notes, and snippets.

@j4johnfox
Created July 9, 2010 17:40
Show Gist options
  • Save j4johnfox/469758 to your computer and use it in GitHub Desktop.
Save j4johnfox/469758 to your computer and use it in GitHub Desktop.
- (CPString)bundleLocaleWithUserLanguage:(CPString)aUserLanguage
{
var defaultLocale = [self objectForInfoDictionaryKey:@"CPBundleDevelopmentRegion"];
var availableLocales = [self objectForInfoDictionaryKey:@"CPBundleLocalizedResourceKeys"];
var bestMatch, myLocale= nil;
UserLanguage = aUserLanguage;
// First try matching against the UserLanguage
if (UserLanguage && [UserLanguage length])
{
for (var i = 0; i < [availableLocales count]; i++)
{
myLocale = availableLocales[i];
if (UserLanguage && (myLocale == UserLanguage))
{
bestMatch = myLocale;
break;
}
}
}
// No match? Try the browser language
if (!bestMatch)
{
for (var i = 0; i < [availableLocales count]; i++)
{
myLocale = availableLocales[i];
if (myLocale == [self browserLanguage])
{
bestMatch = myLocale;
break;
}
}
}
// Still nothing? Look for a default, or hope your users understand English.
if (!bestMatch)
{
bestMatch = ([defaultLocale length]) ? defaultLocale : "en";
}
return bestMatch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment