I want to display Currency Symbol based on Country name or country code using NSLocale I have all the country name list. suppose I have selected USA then it Return $ Currency
我想使用NSLocale显示基于国家名称或国家/地区代码的货币符号我有所有国家/地区名称列表。假设我选择了USA然后返回$ Currency
Code :
NSLocale *locale = [NSLocale currentLocale]; NSString *countryCode = [locale objectForKey: NSLocaleCountryCode]; NSString *country = [locale displayNameForKey: NSLocaleCurrencyCode value: countryCode];
4 个解决方案
#1
3
You can build your own list of country codes and that country's currency symbol using the following code:
您可以使用以下代码构建自己的国家/地区代码列表和该国家/地区的货币符号:
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); }}
This code starts with the list of all locale ids. From there it creates each associated NSLocale
. For each locale, the country code and currency symbol is extracted. These are used to build a dictionary of country codes and currency symbols which are then printed out.
此代码以所有区域设置ID的列表开头。从那里创建每个关联的NSLocale。对于每个区域设置,将提取国家/地区代码和货币符号。这些用于构建国家代码和货币符号的字典,然后打印出来。
Here's the same code translated to Swift 3:
这是转换为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)") }}
This results in the following list when run on a simulated iOS 10 device:
在模拟的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: Kzcountry: AR, currency: $country: AS, currency: $country: AT, currency: €country: AU, currency: $country: AW, currency: Afl.country: AX, currency: €country: AZ, currency: ₼country: BA, currency: KMcountry: BB, currency: $country: BD, currency: ৳country: BE, currency: €country: BF, currency: CFAcountry: BG, currency: лв.country: BH, currency: د.ب.country: BI, currency: FBucountry: BJ, currency: CFAcountry: BL, currency: €country: BM, currency: $country: BN, currency: $country: BO, currency: Bscountry: BQ, currency: $country: BR, currency: R$country: BS, currency: $country: BT, currency: Nu.country: BW, currency: Pcountry: BY, currency: р.country: BZ, currency: $country: CA, currency: $country: CC, currency: $country: CD, currency: FCcountry: CF, currency: FCFAcountry: CG, currency: FCFAcountry: CH, currency: CHFcountry: CI, currency: CFAcountry: CK, currency: $country: CL, currency: $country: CM, currency: FCFAcountry: 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: Fdjcountry: 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: Nfkcountry: ES, currency: €country: ET, currency: Brcountry: FI, currency: €country: FJ, currency: $country: FK, currency: £country: FM, currency: US$country: FO, currency: krcountry: FR, currency: €country: GA, currency: FCFAcountry: 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: Dcountry: GN, currency: FGcountry: GP, currency: €country: GQ, currency: FCFAcountry: GR, currency: €country: GT, currency: Qcountry: GU, currency: $country: GW, currency: CFAcountry: GY, currency: $country: HK, currency: HK$country: HN, currency: Lcountry: HR, currency: HRKcountry: HT, currency: Gcountry: HU, currency: HUFcountry: IC, currency: €country: ID, currency: Rpcountry: IE, currency: €country: IL, currency: ₪country: IM, currency: £country: IN, currency: ₹country: IO, currency: US$country: IQ, currency: IQDcountry: IR, currency: IRRcountry: IS, currency: ISKcountry: IT, currency: €country: JE, currency: £country: JM, currency: $country: JO, currency: د.أ.country: JP, currency: ¥country: KE, currency: Kshcountry: KG, currency: сомcountry: KH, currency: ៛country: KI, currency: $country: KM, currency: CFcountry: KN, currency: $country: KP, currency: KPWcountry: KR, currency: ₩country: KW, currency: د.ك.country: KY, currency: $country: KZ, currency: ₸country: LA, currency: ₭country: LB, currency: ل.ل.country: LC, currency: $country: LI, currency: CHFcountry: LK, currency: Rs.country: LR, currency: $country: LS, currency: Rcountry: LT, currency: €country: LU, currency: €country: LV, currency: €country: LY, currency: د.ل.country: MA, currency: MADcountry: MC, currency: €country: MD, currency: Lcountry: ME, currency: €country: MF, currency: €country: MG, currency: Arcountry: MH, currency: $country: MK, currency: dencountry: ML, currency: CFAcountry: MM, currency: Kcountry: MN, currency: ₮country: MO, currency: MOP$country: MP, currency: $country: MQ, currency: €country: MR, currency: UMcountry: MS, currency: $country: MT, currency: €country: MU, currency: Rscountry: MV, currency: MVRcountry: MW, currency: MKcountry: MX, currency: $country: MY, currency: RMcountry: MZ, currency: MTncountry: NA, currency: $country: NC, currency: FCFPcountry: NE, currency: CFAcountry: NF, currency: $country: NG, currency: ₦country: NI, currency: C$country: NL, currency: €country: NO, currency: krcountry: 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: FCFPcountry: PG, currency: Kcountry: PH, currency: ₱country: PK, currency: Rscountry: PL, currency: PLNcountry: 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: RONcountry: RS, currency: RSDcountry: RU, currency: RUBcountry: RW, currency: RFcountry: SA, currency: ر.س.country: SB, currency: $country: SC, currency: SRcountry: SD, currency: SDGcountry: SE, currency: krcountry: SG, currency: $country: SH, currency: £country: SI, currency: €country: SJ, currency: krcountry: SK, currency: €country: SL, currency: Lecountry: SM, currency: €country: SN, currency: CFAcountry: SO, currency: Scountry: SR, currency: $country: SS, currency: £country: ST, currency: Dbcountry: SV, currency: $country: SX, currency: NAf.country: SY, currency: ل.س.country: SZ, currency: Ecountry: TC, currency: US$country: TD, currency: FCFAcountry: TG, currency: CFAcountry: TH, currency: THBcountry: TJ, currency: сомcountry: TK, currency: $country: TL, currency: US$country: TM, currency: TMTcountry: TN, currency: د.ت.country: TO, currency: T$country: TR, currency: TRYcountry: TT, currency: $country: TV, currency: $country: TW, currency: NT$country: TZ, currency: TShcountry: UA, currency: ₴country: UG, currency: UShcountry: UM, currency: $country: US, currency: $country: UY, currency: $country: UZ, currency: soʻmcountry: VC, currency: $country: VE, currency: Bs.country: VG, currency: US$country: VI, currency: $country: VN, currency: ₫country: VU, currency: VTcountry: WF, currency: FCFPcountry: WS, currency: WS$country: XK, currency: €country: YE, currency: ر.ي.country: YT, currency: €country: ZA, currency: Rcountry: ZM, currency: Kcountry: ZW, currency: US$
#2
8
Xcode 9.3 • Swift 4.1
Xcode 9.3•Swift 4.1
var currencies: [String: (code: String, symbol: String)] = [:]Locale.isoRegionCodes.map({ let locale = Locale(identifier: Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: $0])) currencies[$0] = (locale.currencyCode ?? "", locale.currencySymbol ?? "")})
As a Locale static dictionary:
作为Locale静态字典:
extension Locale { static let currencies = Dictionary(uniqueKeysWithValues: Locale.isoRegionCodes.map { region -> (String, (code: String, symbol: String, locale: Locale)) in let locale = Locale(identifier: Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: region])) return (region, (locale.currencyCode ?? "", locale.currencySymbol ?? "", locale)) })}let currency = Locale.currencies["US"]!currency.code // "USD"currency.symbol // "US$"currency.locale // _US (fixed)
Xcode 9 • Swift 4
Xcode 9•Swift 4
let currencies = Locale.isoRegionCodes.reduce(into: [String: (code: String, symbol: String)]()) { let locale = Locale(identifier: Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: $1])) $0[$1] = (locale.currencyCode ?? "", locale.currencySymbol ?? "")}
currencies["US"] // (code "USD", symbol "US$")currencies["BR"] // (code "BRL", symbol "R$")currencies["GB"] // (code "GBP", symbol "£")
currencies.sorted{ $0.key<$1.key }.map{ print($0) }
#3
2
You can Get currency code from Country name, tested it on swift3, first of all add this extension
您可以从国家名称获取货币代码,在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}}
You will get the currency code from here
您将从此处获取货币代码
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
#4
1
You can't do that with the currency symbol alone. You have to let the system format your numbers as a currency with the correct locale.
单独使用货币符号就不能这样做。您必须让系统将您的数字格式化为具有正确语言环境的货币。
For example, there are over a dozen countries using Euros, and they display money in different ways. And the currency symbol for the USA is only "$" if you are in the USA - in Canada, for example, it is "US$" because Canadians use "$" for their own currency.
例如,有十几个国家使用欧元,他们以不同的方式展示资金。如果你在美国,美国的货币符号只是“$” - 例如,在加拿大,它是“美元”,因为加拿大人使用“$”代表他们自己的货币。
#1
3
You can build your own list of country codes and that country's currency symbol using the following code:
您可以使用以下代码构建自己的国家/地区代码列表和该国家/地区的货币符号:
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); }}
This code starts with the list of all locale ids. From there it creates each associated NSLocale
. For each locale, the country code and currency symbol is extracted. These are used to build a dictionary of country codes and currency symbols which are then printed out.
此代码以所有区域设置ID的列表开头。从那里创建每个关联的NSLocale。对于每个区域设置,将提取国家/地区代码和货币符号。这些用于构建国家代码和货币符号的字典,然后打印出来。
Here's the same code translated to Swift 3:
这是转换为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)") }}
This results in the following list when run on a simulated iOS 10 device:
在模拟的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: Kzcountry: AR, currency: $country: AS, currency: $country: AT, currency: €country: AU, currency: $country: AW, currency: Afl.country: AX, currency: €country: AZ, currency: ₼country: BA, currency: KMcountry: BB, currency: $country: BD, currency: ৳country: BE, currency: €country: BF, currency: CFAcountry: BG, currency: лв.country: BH, currency: د.ب.country: BI, currency: FBucountry: BJ, currency: CFAcountry: BL, currency: €country: BM, currency: $country: BN, currency: $country: BO, currency: Bscountry: BQ, currency: $country: BR, currency: R$country: BS, currency: $country: BT, currency: Nu.country: BW, currency: Pcountry: BY, currency: р.country: BZ, currency: $country: CA, currency: $country: CC, currency: $country: CD, currency: FCcountry: CF, currency: FCFAcountry: CG, currency: FCFAcountry: CH, currency: CHFcountry: CI, currency: CFAcountry: CK, currency: $country: CL, currency: $country: CM, currency: FCFAcountry: 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: Fdjcountry: 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: Nfkcountry: ES, currency: €country: ET, currency: Brcountry: FI, currency: €country: FJ, currency: $country: FK, currency: £country: FM, currency: US$country: FO, currency: krcountry: FR, currency: €country: GA, currency: FCFAcountry: 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: Dcountry: GN, currency: FGcountry: GP, currency: €country: GQ, currency: FCFAcountry: GR, currency: €country: GT, currency: Qcountry: GU, currency: $country: GW, currency: CFAcountry: GY, currency: $country: HK, currency: HK$country: HN, currency: Lcountry: HR, currency: HRKcountry: HT, currency: Gcountry: HU, currency: HUFcountry: IC, currency: €country: ID, currency: Rpcountry: IE, currency: €country: IL, currency: ₪country: IM, currency: £country: IN, currency: ₹country: IO, currency: US$country: IQ, currency: IQDcountry: IR, currency: IRRcountry: IS, currency: ISKcountry: IT, currency: €country: JE, currency: £country: JM, currency: $country: JO, currency: د.أ.country: JP, currency: ¥country: KE, currency: Kshcountry: KG, currency: сомcountry: KH, currency: ៛country: KI, currency: $country: KM, currency: CFcountry: KN, currency: $country: KP, currency: KPWcountry: KR, currency: ₩country: KW, currency: د.ك.country: KY, currency: $country: KZ, currency: ₸country: LA, currency: ₭country: LB, currency: ل.ل.country: LC, currency: $country: LI, currency: CHFcountry: LK, currency: Rs.country: LR, currency: $country: LS, currency: Rcountry: LT, currency: €country: LU, currency: €country: LV, currency: €country: LY, currency: د.ل.country: MA, currency: MADcountry: MC, currency: €country: MD, currency: Lcountry: ME, currency: €country: MF, currency: €country: MG, currency: Arcountry: MH, currency: $country: MK, currency: dencountry: ML, currency: CFAcountry: MM, currency: Kcountry: MN, currency: ₮country: MO, currency: MOP$country: MP, currency: $country: MQ, currency: €country: MR, currency: UMcountry: MS, currency: $country: MT, currency: €country: MU, currency: Rscountry: MV, currency: MVRcountry: MW, currency: MKcountry: MX, currency: $country: MY, currency: RMcountry: MZ, currency: MTncountry: NA, currency: $country: NC, currency: FCFPcountry: NE, currency: CFAcountry: NF, currency: $country: NG, currency: ₦country: NI, currency: C$country: NL, currency: €country: NO, currency: krcountry: 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: FCFPcountry: PG, currency: Kcountry: PH, currency: ₱country: PK, currency: Rscountry: PL, currency: PLNcountry: 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: RONcountry: RS, currency: RSDcountry: RU, currency: RUBcountry: RW, currency: RFcountry: SA, currency: ر.س.country: SB, currency: $country: SC, currency: SRcountry: SD, currency: SDGcountry: SE, currency: krcountry: SG, currency: $country: SH, currency: £country: SI, currency: €country: SJ, currency: krcountry: SK, currency: €country: SL, currency: Lecountry: SM, currency: €country: SN, currency: CFAcountry: SO, currency: Scountry: SR, currency: $country: SS, currency: £country: ST, currency: Dbcountry: SV, currency: $country: SX, currency: NAf.country: SY, currency: ل.س.country: SZ, currency: Ecountry: TC, currency: US$country: TD, currency: FCFAcountry: TG, currency: CFAcountry: TH, currency: THBcountry: TJ, currency: сомcountry: TK, currency: $country: TL, currency: US$country: TM, currency: TMTcountry: TN, currency: د.ت.country: TO, currency: T$country: TR, currency: TRYcountry: TT, currency: $country: TV, currency: $country: TW, currency: NT$country: TZ, currency: TShcountry: UA, currency: ₴country: UG, currency: UShcountry: UM, currency: $country: US, currency: $country: UY, currency: $country: UZ, currency: soʻmcountry: VC, currency: $country: VE, currency: Bs.country: VG, currency: US$country: VI, currency: $country: VN, currency: ₫country: VU, currency: VTcountry: WF, currency: FCFPcountry: WS, currency: WS$country: XK, currency: €country: YE, currency: ر.ي.country: YT, currency: €country: ZA, currency: Rcountry: ZM, currency: Kcountry: ZW, currency: US$
#2
8
Xcode 9.3 • Swift 4.1
Xcode 9.3•Swift 4.1
var currencies: [String: (code: String, symbol: String)] = [:]Locale.isoRegionCodes.map({ let locale = Locale(identifier: Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: $0])) currencies[$0] = (locale.currencyCode ?? "", locale.currencySymbol ?? "")})
As a Locale static dictionary:
作为Locale静态字典:
extension Locale { static let currencies = Dictionary(uniqueKeysWithValues: Locale.isoRegionCodes.map { region -> (String, (code: String, symbol: String, locale: Locale)) in let locale = Locale(identifier: Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: region])) return (region, (locale.currencyCode ?? "", locale.currencySymbol ?? "", locale)) })}let currency = Locale.currencies["US"]!currency.code // "USD"currency.symbol // "US$"currency.locale // _US (fixed)
Xcode 9 • Swift 4
Xcode 9•Swift 4
let currencies = Locale.isoRegionCodes.reduce(into: [String: (code: String, symbol: String)]()) { let locale = Locale(identifier: Locale.identifier(fromComponents: [NSLocale.Key.countryCode.rawValue: $1])) $0[$1] = (locale.currencyCode ?? "", locale.currencySymbol ?? "")}
currencies["US"] // (code "USD", symbol "US$")currencies["BR"] // (code "BRL", symbol "R$")currencies["GB"] // (code "GBP", symbol "£")
currencies.sorted{ $0.key<$1.key }.map{ print($0) }
#3
2
You can Get currency code from Country name, tested it on swift3, first of all add this extension
您可以从国家名称获取货币代码,在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}}
You will get the currency code from here
您将从此处获取货币代码
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
#4
1
You can't do that with the currency symbol alone. You have to let the system format your numbers as a currency with the correct locale.
单独使用货币符号就不能这样做。您必须让系统将您的数字格式化为具有正确语言环境的货币。
For example, there are over a dozen countries using Euros, and they display money in different ways. And the currency symbol for the USA is only "$" if you are in the USA - in Canada, for example, it is "US$" because Canadians use "$" for their own currency.
例如,有十几个国家使用欧元,他们以不同的方式展示资金。如果你在美国,美国的货币符号只是“$” - 例如,在加拿大,它是“美元”,因为加拿大人使用“$”代表他们自己的货币。