2011-05-30 15 views
38

He utilizado este código para conseguir qué país iPhone pertenecen a:NSLocale y nombre del país

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

y quiero obtener el nombre del país siempre en Inglés, pero si el iPhone es en cualquier otro idioma que devuelve el nombre del país en ese idioma ...

+4

Tenga en cuenta que la configuración regional y el idioma son configurables por el usuario. Por ejemplo, un usuario nativo de habla inglesa que nunca ha salido de los EE. UU. Pero está aprendiendo italiano podría cambiar el idioma y la configuración de su iPhone a Italia, y alguien con su teléfono configurado en inglés estadounidense puede viajar al extranjero. Si realmente quieres saber en qué país está el teléfono, utiliza la geolocalización. –

+0

@ JeremyW.Sherman: De acuerdo. Use la configuración regional para determinar cómo localizar su contenido, no determinar dónde está el usuario. – Alan

Respuesta

90

consulta un local Inglés para el idioma

así:

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

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

NSString *country = [usLocale displayNameForKey: NSLocaleCountryCode value: countryCode]; 
11

Aquí es un poco un código para conseguir algunas informaciones sobre los NSLocale-objetos disponibles en SWIFT, Sólo hay que poner el código en el patio:

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() 

a conseguir este tipo de información (ejemplo):

You get this kind of information (example):

Cuestiones relacionadas