首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSLocale和国家/地区名称

NSLocale和国家/地区名称
EN

Stack Overflow用户
提问于 2011-05-30 22:04:54
回答 2查看 50.1K关注 0票数 42

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

代码语言:javascript
复制
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-30 22:55:27

查询displayName的英语区域设置

如下所示:

代码语言:javascript
复制
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

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

NSString *country = [usLocale displayNameForKey: NSLocaleCountryCode value: countryCode];
票数 104
EN

Stack Overflow用户

发布于 2014-12-18 19:26:12

这是一个代码,用于获取有关SWIFT中可用的NSLocale-Objects的一些信息,只需将代码放入Playground:

代码语言:javascript
复制
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()

您将获得此类信息(示例):

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6177309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档