How to get NSLocale from a currency code?

I am trying to do this:

NSDictionary *components = [NSDictionary dictionaryWithObject:@"USD" forKey:NSLocaleCurrencyCode]; NSString *localeIdentifier = [NSLocale localeIdentifierFromComponents:components]; NSLocale *localeForDefaultCurrency = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]; 

It creates an NSLocale object, but it does not contain any regional information. For example,

 [localeForDefaultCurrency objectForKey:NSLocaleCountryCode]; 

returns nil;

Any idea how to get NSLocale from a currency code?

+7
source share
4 answers

As reported by some guest on pastebin:

 NSDictionary *components = [NSDictionary dictionaryWithObject:@"USD" forKey:NSLocaleCurrencyCode]; NSString *localeIdentifier = [NSLocale localeIdentifierFromComponents:components]; NSLocale *localeForDefaultCurrency = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]; [localeForDefaultCurrency objectForKey:NSLocaleCountryCode]; 
+7
source

There are tricks such as creating a locale with an identifier, but instead of a normal locale identifier such as "en_US", pass the currency code "USD", "EUR", etc., it seems to work on eur and usd so that I could verify this, but this is not true. IMHO.

The only correct way that I know is to get all the locales, and then check the currency codes in a loop to compare them with the one you have. This way you stop the loop when you find your code.

 - (NSLocale *) findLocaleByCurrencyCode:(NSString *)_currencyCode { NSArray *locales = [NSLocale availableLocaleIdentifiers]; NSLocale *locale = nil; NSString *localeId; for (localeId in locales) { locale = [[[NSLocale alloc] initWithLocaleIdentifier:localeId] autorelease]; NSString *code = [locale objectForKey:NSLocaleCurrencyCode]; if ([code isEqualToString:_currencyCode]) break; else locale = nil; } /* For some codes that locale cannot be found, init it different way. */ if (locale == nil) { NSDictionary *components = [NSDictionary dictionaryWithObject:_currencyCode forKey:NSLocaleCurrencyCode]; localeId = [NSLocale localeIdentifierFromComponents:components]; locale = [[NSLocale alloc] initWithLocaleIdentifier:localeId]; } return locale; } 
+5
source
  NSDictionary *localeInfo = @{NSLocaleCurrencyCode: currencyCode, NSLocaleLanguageCode: [[NSLocale preferredLanguages] objectAtIndex:0]}; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier: [NSLocale localeIdentifierFromComponents:localeInfo]]; 
+5
source

You can use the power of extensions in Swift :

 extension NSLocale { static func currencySymbolFromCode(code: String) -> String? { let localeIdentifier = NSLocale.localeIdentifierFromComponents([NSLocaleCurrencyCode : code]) let locale = NSLocale(localeIdentifier: localeIdentifier) return locale.objectForKey(NSLocaleCurrencySymbol) as? String } } 

Thus, anywhere in your code:

 let code = "EUR" let price = 10 if let currencySymbol = NSLocale.currencySymbolFromCode(code) as? String { print("You have to pay \(price)\(currencySymbol)") } 
+1
source

All Articles