17
- (NSString *)countryNameByCode:(NSString*)countryCode
{
    NSString *identifier = [NSLocale localeIdentifierFromComponents:@{NSLocaleCountryCode: countryCode}];
    NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];

    return countryName;
}

This returned nil. Why?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Yuriy Bosov
  • 211
  • 2
  • 5
  • It issue: http://stackoverflow.com/questions/26603766/swift-nslocale-simulator-ios-8 – Alexandr Semeniuk Oct 28 '14 at 21:01
  • 1
    This problem only simulator. I tested it on a device today, I get correct country Name. – Yuriy Bosov Oct 29 '14 at 10:58
  • just ran into this issue myself on the simulators - I populate a tableview with a list of all countries and in the simulator it just crashed due to displayNameForKey returning null. I have tried it on all simulator devices with 8.1 and they all crash. iPhone4 and iPhone 5 simulators with iOS 7.1 run fine. My code also runs fine on physical devices iPhone4s and iPhone5 with 8.1 installed so i'm guessing its just an Xcode bug. solution - until apple fix, test on device - which is a bugger as i don't have 6 and 6 plus to hand :( – SimonTheDiver Oct 31 '14 at 12:16
  • possible duplicate of [NSLocale currentLocale always returns "en\_US" not user's current language](http://stackoverflow.com/questions/1522210/nslocale-currentlocale-always-returns-en-us-not-users-current-language) – Zorayr Nov 05 '14 at 23:04

5 Answers5

20

This is a known Apple issue for iOS 8.1 simulator only - not reproducible on 8.1 devices. See below issue description from Xcode 6.1 release notes:

Localization and Keyboard settings (including 3rd party keyboards) are not correctly honored by Safari, Maps, and developer apps in the iOS 8.1 Simulator. [NSLocale currentLocale] returns en_US and only the English and Emoji keyboards are available. (18418630, 18512161).

See Xcode 6.1 Release Notes for more details.

Zorayr
  • 23,770
  • 8
  • 136
  • 129
7

Please try below code, it should work. I tried it on my device as well as simulator 8.1

- (NSString *)countryNameByCode:(NSString*)countryCode {
  return [[NSLocale systemLocale] displayNameForKey:NSLocaleCountryCode value:countryCode];
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
1

This works for me

NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:[NSLocale currentLocale].localeIdentifier];

 for (AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) {
            NSString *languageLocalised = [currentLocale displayNameForKey:NSLocaleIdentifier value:voice.language];
            NSLog(@"%@ displayNameForKey %@: %@", currentLocale.localeIdentifier, voice.language, languageLocalised);
 }
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
davrg
  • 11
  • 3
  • Sidemark: Your first line contains redundant code. You could shorten it to NSLocale *currentLocale = [NSLocale currentLocale]; – JonEasy Jul 27 '15 at 07:10
0
- (NSString *)countryNameByCode:(NSString*)countryCode 
{
    NSString *identifier = [[NSLocale preferredLanguages] firstObject];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:identifier];
    NSString *countryName = [locale displayNameForKey:NSLocaleIdentifier value:countryCode];
    return countryName;
}

I don't understand why [[NSLocale currentLocale] displayNameForKey...] doesn't return country name in iOS 8 but the code above should resolve your problem.

zyxman
  • 72
  • 5
  • Apparently that returns the language name (French, German...), not the country. – Arnaud Oct 30 '14 at 03:17
  • 2
    Yeh, not working on my end either... for country code "US", I get an identifier, "_US" which results in a country name, nil. – Zorayr Nov 05 '14 at 22:41
  • The solution is working, you just need to use `NSLocaleCountryCode` instead of `NSLocaleIdentifier` – HiveHicks Dec 11 '14 at 12:45
0
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Bobby
  • 6,115
  • 4
  • 35
  • 36