Saturday, May 11, 2013

NSLocale canonicalLanguageIdentifierFromString example ios


canonicalLanguageIdentifierFromString:

Returns a canonical language identifier by mapping an arbitrary locale identification string to the canonical identifier.
+ (NSString *)canonicalLanguageIdentifierFromString:(NSString *)string
Parameters
string
A string representation of an arbitrary locale identifier.
Return Value of [NSLocale canonicalLanguageIdentifierFromString]
A string that represents the canonical language identifier for the specified arbitrary locale identifier.
Example of [NSLocale canonicalLanguageIdentifierFromString]
NSArray *arr = [NSLocale preferredLanguages];
for (NSString *lan in arr) {
    NSLog(@"%@: %@ %@",lan, [NSLocale canonicalLanguageIdentifierFromString:lan], [[[NSLocale alloc] initWithLocaleIdentifier:lan] displayNameForKey:NSLocaleIdentifier value:lan]);
}
Example of [NSLocale canonicalLanguageIdentifierFromString]
myCountryCode = [[NSLocale ISOCountryCodes] objectAtIndex:row];
appLocale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary
        dictionaryWithObject: myCountryCode 
        forKey: NSLocaleCountryCode]];
myDictionary = [NSLocale componentsFromLocaleIdentifier: identifier];
myCountryName = [appLocale displayNameForKey:NSLocaleCountryCode 
         value:[myDictionaryobjectForKey:NSLocaleCountryCode]];

// Create the currency language combination and then make our locale
NSString *currencyLocaleLanguage= [NSLocale canonicalLanguageIdentifierFromString: myCountryCode];
NSString *countryLanguage= [NSString stringWithFormat: @"%@_%@", myCountryCode, currencyLocaleLanguage];
NSLocale *currencyLocale = [[NSLocale alloc] initWithLocaleIdentifier: countryLanguage];

localCurrencySymbol = [currencyLocale objectForKey:NSLocaleCurrencySymbol];
currencyCode = [currencyLocale objectForKey:NSLocaleCurrencyCode];

title = [NSString stringWithFormat:@"%@ %@: %@ (%@)", myCountryCode, myCountryName, localCurrencySymbol, currencyCode];
[currencyLocale release];
[appLocale release];