42

I used this code for getting which country iPhone belong to:

NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];

and I want to get the country name always in English, but if the iPhone is in any other language it returns the country name in that language...

Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
yosifz8
  • 551
  • 2
  • 7
  • 11
  • 4
    Note that the locale and language are both user-configurable. For example, a native English-speaking user who has never left the US but just so happens to be learning Italian might change their iPhone language and locale to Italy, and someone with their phone set to US English can travel abroad. If you really want to find out what country the phone is in right now, use geolocation. – Jeremy W. Sherman May 30 '11 at 15:11
  • @JeremyW.Sherman: Agreed. Use locale to determine how to localize your content, not determine where the user is. – Alan May 01 '12 at 18:48

2 Answers2

104

Query an english locale for the displayName

like this:

NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

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

NSString *country = [usLocale displayNameForKey: NSLocaleCountryCode value: countryCode];
Mujib Saiyyed
  • 158
  • 10
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
12

Here is a bit a code for getting some informations about the available NSLocale-Objects in SWIFT, Just put the code into the Playground:

func printInEnglish() {

    // get all available Identifiers
    let allLocaleIdentifiers : Array<String> = NSLocale.availableLocaleIdentifiers as Array<String>

    // init an english NSLocale to get the english name of all NSLocale-Objects
    let englishLocale : NSLocale = NSLocale.init(localeIdentifier :  "en_US")

    // enumerate all available Identifiers
    for anyLocaleID in allLocaleIdentifiers {

        // get the english name
        var theEnglishName : String? = englishLocale.displayName(forKey: NSLocale.Key.identifier, value: anyLocaleID)
        if theEnglishName == nil {theEnglishName = "no english name available"}

        // create a NSLocale-Object
        let anyLocale : NSLocale  = NSLocale.init(localeIdentifier : anyLocaleID)

        // ask for CurrencyCode, CurrencySymbol and CountryCode, ... of the created NSLocale-Object
        var theCurrencyCode : String? = anyLocale.object(forKey: NSLocale.Key.currencyCode) as? String
        if theCurrencyCode == nil {theCurrencyCode = "no Currency Code available"}

        var theCurrencySymbol : String? = anyLocale.object(forKey: NSLocale.Key.currencySymbol) as? String
        if theCurrencySymbol == nil {theCurrencySymbol = "no currency symbol available"}

        var theCountryCode : String? = anyLocale.object(forKey: NSLocale.Key.countryCode) as? String
        if theCountryCode == nil {theCountryCode = "no country code available"}

        var theLanguageCode : String? = anyLocale.object(forKey: NSLocale.Key.languageCode) as? String
        if theLanguageCode == nil {theLanguageCode = "no language code available"}

        // print the result -> see the result in LoggingArea of xCode
        print("Identifier   : \(anyLocaleID)\nName         : \(theEnglishName!)\nCurrencyCode : \(theCurrencyCode!)\nSymbol       : \(theCurrencySymbol!)\nLanguageCode : \(theLanguageCode!)\nCountryCode  : \(theCountryCode!)\n----------------------------")
    }
}

printInEnglish()

You get this kind of information (example):

You get this kind of information (example):

M-P
  • 4,909
  • 3
  • 25
  • 31
LukeSideWalker
  • 7,399
  • 2
  • 37
  • 45