Showing posts with label localeIdentifier. Show all posts
Showing posts with label localeIdentifier. Show all posts

Saturday, May 11, 2013

NSLocale localeIdentifier example ios


localeIdentifier

Returns the identifier for the receiver.
- (NSString *)localeIdentifier
Return Value of [NSLocale localeIdentifier]
The identifier for the receiver. This may not be the same string that the locale was created with, since NSLocale may canonicalize it.
Discussion
Equivalent to sending objectForKey: with key NSLocaleIdentifier.
Example of [NSLocale localeIdentifier]
 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 localeIdentifier]
NSLocale *locale = [NSLocale currentLocale];

NSString *language = [locale displayNameForKey:NSLocaleIdentifier 
                                         value:[locale localeIdentifier]];

Example of [NSLocale localeIdentifier]
- (NSLocale *) findLocaleByCurrencyCode:(NSString *)_currencyCode
{
        NSArray *locales = [NSLocale availableLocaleIdentifiers];
        NSLocale *locale = nil;
        NSString *localeId;

        for (localeId in locales) {
                locale = [[[NSLocale alloc] initWithLocaleIdentifier:localeId] autorelease];
                NSString *code = [locale objectForKey:NSLocaleCurrencyCode];
                if ([code isEqualToString:_currencyCode])
                        break;
                else
                        locale = nil;
        }

        /* For some codes that locale cannot be found, init it different way. */
        if (locale == nil) {
                NSDictionary *components = [NSDictionary dictionaryWithObject:_currencyCode
                                                                       forKey:NSLocaleCurrencyCode];

                localeId = [NSLocale localeIdentifierFromComponents:components];
                locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId];
        }
        return locale;
}