2011-05-30 14 views
38

मैं प्राप्त करने के लिए इस कोड का इस्तेमाल किया है जो देश iPhone संबंधित:NSLocale और देश का नाम

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

और मैं अंग्रेजी में हमेशा देश का नाम प्राप्त करना चाहते हैं, लेकिन अगर iPhone किसी अन्य भाषा में है यह रिटर्न उस भाषा में देश का नाम ...

+4

ध्यान दें कि लोकेल और भाषा उपयोगकर्ता-कॉन्फ़िगर करने योग्य दोनों हैं। उदाहरण के लिए, एक मूल अंग्रेजी भाषी उपयोगकर्ता जिसने कभी अमेरिका छोड़ दिया नहीं है, लेकिन ऐसा ही होता है कि इतालवी सीखना इतालवी भाषा में अपनी आईफोन भाषा और लोकेल बदल सकता है, और कोई भी व्यक्ति जो अपने फोन के साथ यूएस अंग्रेज़ी में जाता है, विदेश यात्रा कर सकता है। यदि आप वास्तव में यह जानना चाहते हैं कि फोन किस देश में है, तो भौगोलिक स्थान का उपयोग करें। –

+0

@ जेरेमी डब्ल्यू। शेरमेन: सहमत। अपनी सामग्री को स्थानीयकृत करने का तरीका निर्धारित करने के लिए लोकेल का उपयोग करें, यह निर्धारित न करें कि उपयोगकर्ता कहां है। – Alan

उत्तर

90

क्वेरी DISPLAYNAME के ​​लिए एक अंग्रेज़ी लोकेल

इस तरह:

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

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

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

यहाँ एक सा है स्विफ्ट में उपलब्ध NSLocale-वस्तुओं के बारे में कुछ जानकारियां प्राप्त करने के लिए एक कोड, बस खेल का मैदान में कोड डाल:

:

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

संबंधित मुद्दे