我想使用NSLocale显示基于国家名称或国家/地区代码的货币符号我有所有国家/地区名称列表.假设我选择了USA然后返回$Currency
代码:
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCurrencyCode value: countryCode];
您可以使用以下代码构建自己的国家/地区代码列表和该国家/地区的货币符号:
Objective-C的:
- (void)listCountriesAndCurrencies {
NSArray *localeIds = [NSLocale availableLocaleIdentifiers];
NSMutableDictionary *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 *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$
Android根据语言适配货币符 ios – 使用NSLocale根据国家/地区代码或国家/地区名称获取货币符号...