Подтвердить что ты не робот

NSLocale и название страны

Я использовал этот код для получения того, к какой стране принадлежит iPhone:

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

и я хочу, чтобы название страны всегда было на английском языке, но если iPhone находится на любом другом языке, он возвращает название страны на этом языке...

4b9b3361

Ответ 1

Запросить английский язык для displayName

вот так:

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

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

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

Ответ 2

Вот немного кода для получения информации о доступных NSLocale-объектах в SWIFT, просто поместите код в 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()

Вы получаете такую ​​информацию (пример):

You get this kind of information (example):