Saturday, May 11, 2013

NSLocale displayNameForKey value example ios

displayNameForKey value

Returns the display name for the given value.
- (NSString *)displayNameForKey:(id)key value:(id)value
Specifies which of the locale property keys value is (see “Constants”),
A value for key.
Return Value of [NSLocale displayNameForKey value]
The display name for value.
Not all locale property keys have values with display name values.
Example of [NSLocale displayNameForKey value]
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

NSString *country = [usLocale displayNameForKey: NSLocaleCountryCode value: countryCode];
Example of [NSLocale displayNameForKey value]
NSLocale *french = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *frenchName = [french displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLocale *english = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
NSString *englishName = [english  displayNameForKey:NSLocaleIdentifier value:@"fr_FR"];

NSLog(@"French locale in French is called '%@'", frenchName);
NSLog(@"French locale in English is called '%@'", englishName);
Example of [NSLocale displayNameForKey value]
NSArray *languages = [NSLocale preferredLanguages];
for (NSString *language in languages) {
  NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:language] autorelease];
  NSLog(@"language code = %@, display name = %@, in language = %@",
        [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language],
        [locale displayNameForKey:NSLocaleIdentifier value:language]);