【问题标题】:NSLocale and country nameNSLocale 和国家名称
【发布时间】:2011-05-30 14:04:53
【问题描述】:

我使用此代码获取 iPhone 属于哪个国家/地区:

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

我希望始终使用英语获取国家/地区名称,但如果 iPhone 使用任何其他语言,它会返回该语言的国家/地区名称...

【问题讨论】:

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

标签: iphone objective-c


【解决方案1】:

查询显示名称的英语语言环境

像这样:

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

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

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

【讨论】:

    【解决方案2】:

    这里有一段代码,用于获取有关 SWIFT 中可用 NSLocale-Objects 的一些信息,只需将代码放入 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()
    

    你得到这样的信息(例子):

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      相关资源
      最近更新 更多