Saturday, May 11, 2013

NSLocale displayNameForKey example ios


displayNameForKey :value:

Returns the display name for the given value.
- (NSString *)displayNameForKey:(id)key value:(id)value
Parameters
key
Specifies which of the locale property keys value is (see “Constants”),
value
A value for key.
Return Value of [NSLocale displayNameForKey]
The display name for value.
Discussion
Not all locale property keys have values with display name values.
Example of [NSLocale displayNameForKey]
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]
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]
NSArray *languages = [NSLocale preferredLanguages];
for (NSString *language in languages) {
  NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:language] autorelease];
  NSLog(@"language code = %@, display name = %@, in language = %@",
        language,
        [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:language],
        [locale displayNameForKey:NSLocaleIdentifier value:language]);
}