Saturday, May 11, 2013

NSLocale componentsFromLocaleIdentifier example ios


componentsFromLocaleIdentifier:

Returns a dictionary that is the result of parsing a locale ID.
+ (NSDictionary *)componentsFromLocaleIdentifier:(NSString *)string
Parameters
string
A locale ID, consisting of language, script, country, variant, and keyword/value pairs, for example, "en_US@calendar=japanese".
Return Value of [NSLocale componentsFromLocaleIdentifier]
A dictionary that is the result of parsing string as a locale ID. The keys are the constant NSString constants corresponding to the locale ID components, and the values correspond to constants where available. For the complete set of dictionary keys, see “Constants.”
Discussion of [NSLocale componentsFromLocaleIdentifier]
For example: the locale ID "en_US@calendar=japanese" yields a dictionary with three entries: NSLocaleLanguageCode=enNSLocaleCountryCode=US, andNSLocaleCalendar=NSJapaneseCalendar.
Example of [NSLocale componentsFromLocaleIdentifier]
// Create a pool for autoreleased objects

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Get the current country and locale information of the user
    NSLocale *locale = [NSLocale currentLocale];
    NSString *currentLocaleID = [locale localeIdentifier];    //returns en_US instead of the actual country stored in phone settings
    NSDictionary *localeDictionary = [NSLocale componentsFromLocaleIdentifier:currentLocaleID];     
    NSString *currentCountry = [locale displayNameForKey:NSLocaleCountryCode 
                                                   value:[localeDictionary objectForKey:NSLocaleCountryCode]];     // returns nil

    // Get the list of country codes
    NSArray *countryCodeArray = [NSLocale ISOCountryCodes];
    NSMutableArray *sortedCountryNameArray = [NSMutableArray array];


    for (NSString *countryCode in countryCodeArray) {
        NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode];
        [sortedCountryNameArray addObject:displayNameString]; 
    }

    // Drain the autoreleased pool
    [pool drain];
Example of [NSLocale componentsFromLocaleIdentifier]
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];