Iphone NSLocale和国家/地区名称

Iphone NSLocale和国家/地区名称,iphone,objective-c,Iphone,Objective C,我使用以下代码获取iPhone所属的国家: NSLocale *locale = [NSLocale currentLocale]; NSString *countryCode = [locale objectForKey: NSLocaleCountryCode]; NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode]; 我希望国家名称始终使用英语,但如果iPhone使用任

我使用以下代码获取iPhone所属的国家:

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

我希望国家名称始终使用英语,但如果iPhone使用任何其他语言,它将返回该语言的国家名称…

查询显示名称的英语区域设置

像这样:

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

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

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

下面是一段代码,用于获取有关SWIFT中可用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()
func printInEnglish(){
//获取所有可用的标识符
让allLocaleIdentifiers:Array=NSLocale.availableLocaleIdentifiers作为数组
//初始化english NSLocale以获取所有NSLocale对象的英文名称
让englishLocale:NSLocale=NSLocale.init(localeIdentifier:“en_US”)
//枚举所有可用标识符
对于AllLocalEIdentiers中的anyLocaleID{
//获取英文名称
var theEnglishName:String?=englishLocale.displayName(forKey:NSLocale.Key.identifier,值:anyLocaleID)
如果theEnglishName==nil{theEnglishName=“没有可用的英文名称”}
//创建NSLocale对象
让anyLocale:NSLocale=NSLocale.init(localeIdentifier:anyLocaleID)
//询问创建的NSLocale对象的CurrencyCode、CurrencySymbol和CountryCode
var theCurrencyCode:String?=anyLocale.object(forKey:NSLocale.Key.currencyCode)作为?字符串
如果货币代码==nil{theCurrencyCode=“无可用货币代码”}
var theCurrencySymbol:String?=anyLocale.object(forKey:NSLocale.Key.currencySymbol)作为?字符串
如果货币符号==nil{theCurrencySymbol=“无可用货币符号”}
var theCountryCode:String?=anyLocale.object(forKey:NSLocale.Key.countryCode)作为?字符串
如果CountryCode==nil{theCountryCode=“没有可用的国家代码”}
变量theLanguageCode:String?=anyLocale.object(forKey:NSLocale.Key.languageCode)作为?字符串
如果语言代码==nil{theLanguageCode=“无可用语言代码”}
//打印结果->在xCode的LoggingArea中查看结果
打印(“标识符:\(任何本地ID)\n名称:\(英语名称!)\n货币代码:\(货币代码!)\n符号:\(货币符号!)\n语言代码:\(语言代码!)\n国家代码:\(国家代码!)\n------------------------------------------------------------------------”)
}
}
printInEnglish()
您将获得此类信息(示例):


请注意,区域设置和语言都是用户可配置的。例如,一个从未离开过美国但碰巧正在学习意大利语的母语为英语的用户可能会将其iPhone语言和地区更改为意大利,而将手机设置为美国英语的用户可以出国旅行。如果你真的想知道手机现在在哪个国家,请使用geolocation.@JeremyW.Sherman:同意。使用区域设置来确定如何本地化内容,而不是确定用户所在的位置。