使用 NSLocale 根据国家代码或国家名称获取货币符号

Get Currency Symbol based on Country code or Country name using NSLocale

我想使用 NSLocale 根据国家名称或国家代码显示货币符号我有所有国家名称列表。假设我选择了 USA 然后它 Return $ Currency

代码:

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

您不能仅使用货币符号来做到这一点。您必须让系统将您的数字格式化为具有正确区域设置的货币。

例如,有十几个国家使用欧元,它们以不同的方式显示货币。如果你在美国,美国的货币符号只有“$”——例如在加拿大,它是 "US$",因为加拿大人使用“$”作为他们自己的货币。

您可以使用以下代码构建自己的国家代码列表和该国家/地区的货币符号:

Objective-C:

- (void)listCountriesAndCurrencies {
    NSArray<NSString *> *localeIds = [NSLocale availableLocaleIdentifiers];
    NSMutableDictionary<NSString *, NSString *> *countryCurrency = [NSMutableDictionary dictionary];
    for (NSString *localeId in localeIds) {
        NSLocale *locale = [NSLocale localeWithLocaleIdentifier:localeId];

        NSString *country = [locale objectForKey: NSLocaleCountryCode];
        if (country && country.length == 2) {
            NSString *currency = [locale objectForKey: NSLocaleCurrencySymbol];
            countryCurrency[country] = currency;
        }
    }

    NSArray<NSString *> *sorted = [countryCurrency.allKeys sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *country in sorted) {
        NSString *currency = countryCurrency[country];

        NSLog(@"country: %@, currency: %@", country, currency);
    }
}

此代码以所有区域设置 ID 的列表开头。它从那里创建每个关联的 NSLocale。对于每个语言环境,国家代码和货币符号被提取。这些用于构建国家代码和货币符号的字典,然后将其打印出来。

这是翻译成 Swift 3 的相同代码:

func listCountriesAndCurrencies() {
    let localeIds = Locale.availableIdentifiers
    var countryCurrency = [String: String]()
    for localeId in localeIds {
        let locale = Locale(identifier: localeId)

        if let country = locale.regionCode, country.characters.count == 2 {
            if let currency = locale.currencySymbol {
                countryCurrency[country] = currency
            }
        }
    }

    let sorted = countryCurrency.keys.sorted()
    for country in sorted {
        let currency = countryCurrency[country]!

        print("country: \(country), currency: \(currency)")
    }
}

当 运行 在模拟的 iOS 10 设备上时,这会产生以下列表:

country: AD, currency: €
country: AE, currency: د.إ.‏
country: AF, currency: ؋
country: AG, currency: $
country: AI, currency: $
country: AL, currency: Lekë
country: AM, currency: ֏
country: AO, currency: Kz
country: AR, currency: $
country: AS, currency: $
country: AT, currency: €
country: AU, currency: $
country: AW, currency: Afl.
country: AX, currency: €
country: AZ, currency: ₼
country: BA, currency: KM
country: BB, currency: $
country: BD, currency: ৳
country: BE, currency: €
country: BF, currency: CFA
country: BG, currency: лв.
country: BH, currency: د.ب.‏
country: BI, currency: FBu
country: BJ, currency: CFA
country: BL, currency: €
country: BM, currency: $
country: BN, currency: $
country: BO, currency: Bs
country: BQ, currency: $
country: BR, currency: R$
country: BS, currency: $
country: BT, currency: Nu.
country: BW, currency: P
country: BY, currency: р.
country: BZ, currency: $
country: CA, currency: $
country: CC, currency: $
country: CD, currency: FC
country: CF, currency: FCFA
country: CG, currency: FCFA
country: CH, currency: CHF
country: CI, currency: CFA
country: CK, currency: $
country: CL, currency: $
country: CM, currency: FCFA
country: CN, currency: ¥
country: CO, currency: $
country: CR, currency: ₡
country: CU, currency: $
country: CV, currency: ​
country: CW, currency: NAf.
country: CX, currency: $
country: CY, currency: €
country: CZ, currency: Kč
country: DE, currency: €
country: DG, currency: US$
country: DJ, currency: Fdj
country: DK, currency: kr.
country: DM, currency: $
country: DO, currency: RD$
country: DZ, currency: د.ج.‏
country: EA, currency: €
country: EC, currency: $
country: EE, currency: €
country: EG, currency: ج.م.‏
country: EH, currency: د.م.‏
country: ER, currency: Nfk
country: ES, currency: €
country: ET, currency: Br
country: FI, currency: €
country: FJ, currency: $
country: FK, currency: £
country: FM, currency: US$
country: FO, currency: kr
country: FR, currency: €
country: GA, currency: FCFA
country: GB, currency: £
country: GD, currency: $
country: GE, currency: ₾
country: GF, currency: €
country: GG, currency: £
country: GH, currency: GH₵
country: GI, currency: £
country: GL, currency: kr.
country: GM, currency: D
country: GN, currency: FG
country: GP, currency: €
country: GQ, currency: FCFA
country: GR, currency: €
country: GT, currency: Q
country: GU, currency: $
country: GW, currency: CFA
country: GY, currency: $
country: HK, currency: HK$
country: HN, currency: L
country: HR, currency: HRK
country: HT, currency: G
country: HU, currency: HUF
country: IC, currency: €
country: ID, currency: Rp
country: IE, currency: €
country: IL, currency: ₪
country: IM, currency: £
country: IN, currency: ₹
country: IO, currency: US$
country: IQ, currency: IQD
country: IR, currency: IRR
country: IS, currency: ISK
country: IT, currency: €
country: JE, currency: £
country: JM, currency: $
country: JO, currency: د.أ.‏
country: JP, currency: ¥
country: KE, currency: Ksh
country: KG, currency: сом
country: KH, currency: ៛
country: KI, currency: $
country: KM, currency: CF
country: KN, currency: $
country: KP, currency: KPW
country: KR, currency: ₩
country: KW, currency: د.ك.‏
country: KY, currency: $
country: KZ, currency: ₸
country: LA, currency: ₭
country: LB, currency: ل.ل.‏
country: LC, currency: $
country: LI, currency: CHF
country: LK, currency: Rs.
country: LR, currency: $
country: LS, currency: R
country: LT, currency: €
country: LU, currency: €
country: LV, currency: €
country: LY, currency: د.ل.‏
country: MA, currency: MAD
country: MC, currency: €
country: MD, currency: L
country: ME, currency: €
country: MF, currency: €
country: MG, currency: Ar
country: MH, currency: $
country: MK, currency: den
country: ML, currency: CFA
country: MM, currency: K
country: MN, currency: ₮
country: MO, currency: MOP$
country: MP, currency: $
country: MQ, currency: €
country: MR, currency: UM
country: MS, currency: $
country: MT, currency: €
country: MU, currency: Rs
country: MV, currency: MVR
country: MW, currency: MK
country: MX, currency: $
country: MY, currency: RM
country: MZ, currency: MTn
country: NA, currency: $
country: NC, currency: FCFP
country: NE, currency: CFA
country: NF, currency: $
country: NG, currency: ₦
country: NI, currency: C$
country: NL, currency: €
country: NO, currency: kr
country: NP, currency: नेरू
country: NR, currency: $
country: NU, currency: $
country: NZ, currency: $
country: OM, currency: ر.ع.‏
country: PA, currency: B/.
country: PE, currency: S/.
country: PF, currency: FCFP
country: PG, currency: K
country: PH, currency: ₱
country: PK, currency: Rs
country: PL, currency: PLN
country: PM, currency: €
country: PN, currency: $
country: PR, currency: $
country: PS, currency: ₪
country: PT, currency: €
country: PW, currency: US$
country: PY, currency: Gs.
country: QA, currency: ر.ق.‏
country: RE, currency: €
country: RO, currency: RON
country: RS, currency: RSD
country: RU, currency: RUB
country: RW, currency: RF
country: SA, currency: ر.س.‏
country: SB, currency: $
country: SC, currency: SR
country: SD, currency: SDG
country: SE, currency: kr
country: SG, currency: $
country: SH, currency: £
country: SI, currency: €
country: SJ, currency: kr
country: SK, currency: €
country: SL, currency: Le
country: SM, currency: €
country: SN, currency: CFA
country: SO, currency: S
country: SR, currency: $
country: SS, currency: £
country: ST, currency: Db
country: SV, currency: $
country: SX, currency: NAf.
country: SY, currency: ل.س.‏
country: SZ, currency: E
country: TC, currency: US$
country: TD, currency: FCFA
country: TG, currency: CFA
country: TH, currency: THB
country: TJ, currency: сом
country: TK, currency: $
country: TL, currency: US$
country: TM, currency: TMT
country: TN, currency: د.ت.‏
country: TO, currency: T$
country: TR, currency: TRY
country: TT, currency: $
country: TV, currency: $
country: TW, currency: NT$
country: TZ, currency: TSh
country: UA, currency: ₴
country: UG, currency: USh
country: UM, currency: $
country: US, currency: $
country: UY, currency: $
country: UZ, currency: soʻm
country: VC, currency: $
country: VE, currency: Bs.
country: VG, currency: US$
country: VI, currency: $
country: VN, currency: ₫
country: VU, currency: VT
country: WF, currency: FCFP
country: WS, currency: WS$
country: XK, currency: €
country: YE, currency: ر.ي.‏
country: YT, currency: €
country: ZA, currency: R
country: ZM, currency: K
country: ZW, currency: US$

你可以从国家名称中获取货币代码,在swift3上测试过,首先添加这个扩展

extension NSLocale {
class func locales1(countryName1 : String) -> String {
    let locales : String = ""
    for localeCode in NSLocale.isoCountryCodes {
        let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode)
        if countryName1.lowercased() == countryName?.lowercased() {
            return localeCode
        }
    }
    return locales
}

}

您将从这里获得货币代码

        let countryCode = NSLocale.locales1(countryName1: "\(place.name)")

        let countryCodeCA = countryCode 
        let localeIdCA = NSLocale.localeIdentifier(fromComponents: [ NSLocale.Key.countryCode.rawValue : countryCodeCA])
        let localeCA = NSLocale(localeIdentifier: localeIdCA)
        let currencySymbolCA = localeCA.object(forKey: NSLocale.Key.currencySymbol)
        let currencyCodeCA = localeCA.object(forKey: NSLocale.Key.currencyCode)

        print("\(currencyCodeCA!)")
        self.currencyKey = currencyCodeCA! as! String

Xcode 10 • Swift 4.2 或更高版本

extension Locale {
    static let currency: [String: (code: String?, symbol: String?, name: String?)] = isoRegionCodes.reduce(into: [:]) {
        let locale = Locale(identifier: identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: ]))
        [=10=][] = (locale.currencyCode, locale.currencySymbol, locale.localizedString(forCurrencyCode: locale.currencyCode ?? ""))
    }
}

Locale.currency["US"]   // (code "USD", symbol "$", name "US Dollar")
Locale.currency["BR"]   // (code "BRL", symbol "R$", name "Brazilian Real")
Locale.currency["GB"]   // (code "GBP", symbol "£", name "British Pound")
Locale.currency["PT"]   // (code "EUR", symbol "€", name "Euro")

对于较旧的 Swift 语法,请查看 post 编辑历史记录