Saturday, May 11, 2013

NSLocale availableLocaleIdentifiers example ios


availableLocaleIdentifiers

Returns an array of NSString objects, each of which identifies a locale available on the system.
+ (NSArray *)availableLocaleIdentifiers
Return Value
An array of NSString objects, each of which identifies a locale available on the system.
Example of [NSLocale availableLocaleIdentifiers]
NSArray *test = [NSLocale availableLocaleIdentifiers];
NSLog(@"%@", test);

for (int i = 0; i < [test count]; i++) {
    NSLog(@"%@", [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:[test objectAtIndex:i]]);
}
Example of [NSLocale availableLocaleIdentifiers]
- (NSLocale *) findLocaleByCurrencyCode:(NSString *)_currencyCode
{
        NSArray *locales = [NSLocale availableLocaleIdentifiers];
        NSLocale *locale = nil;

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

        }

        return locale;
}
Example of [NSLocale availableLocaleIdentifiers]
- (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;
}