Get currency symbol and currency code based on ISOCountryCode

There are similar questions to this from beginners like me in localization, but I could not find what the trick does for me.

Here is my problem. We can get all ISO country codes in NSArray with the form [NSLocale ISOCountryCodes] . Now for each country in which I would like to print the local currency, as well as the currency code used in that country. What would be a suitable way to do this?

I did the following, which doesn’t work in the sense that I get US USA form strings: (null) ((null)) when instead I would like to get US form strings: $ (USD):

 myCountryCode = [[NSLocale ISOCountryCodes] objectAtIndex:row]; appLocale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"]; identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: myCountryCode forKey: NSLocaleCountryCode]]; myDictionary = [NSLocale componentsFromLocaleIdentifier: identifier]; myCountryName = [appLocale displayNameForKey:NSLocaleCountryCode value:[myDictionary objectForKey:NSLocaleCountryCode]]; localCurrencySymbol = [appLocale displayNameForKey:NSLocaleCurrencySymbol value:[myDictionary objectForKey:NSLocaleCurrencySymbol]]; currencyCode = [appLocale displayNameForKey:NSLocaleCurrencyCode value: [myDictionary objectForKey:NSLocaleCurrencyCode]]; title = [NSString stringWithFormat:@"%@ %@: %@ (%@)", myCountryCode, myCountryName, localCurrencySymbol, currencyCode]; [appLocale release]; 

(Above the identifier, myCountryCode, myCountryName, localCurrencySymbol, currencyCode and title are all NSString pointers. Moreover, myDictionary is an NSDictionary pointer, and appLocale is an NSLocale pointer). Essentially, the above code will be in pickerview, where I want to generate the title of each line on the fly.

Thanks so much for your time. In fact, the question is how do we ever have an ISO country code, how can we print (in the application locale) a currency symbol and a currency code for this particular country.

+4
source share
3 answers

For those who just want to get the currency code from the 3-character iso code (commonISOCurrencyCodes). You can just do it.

 NSString *localeId = @"JPY"; //[[NSLocale commonISOCurrencyCodes] objectAtIndex:1]; NSLocale *locale = [NSLocale currentLocale]; NSString *currency = [locale displayNameForKey:NSLocaleCurrencySymbol value:localeId]; NSLog(@"locale %@ currency %@", localeId, currency); 

Print.

 locale JPY currency Β₯ 
+9
source

Try the following test application and adapt if necessary

 #import <Foundation/Foundation.h> int main(int argc, char *argv[]) { NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init]; // select an arbitrary locale identifier; you could use your row index but your // indexing scheme would be different NSString *localeIdentifier = [[NSLocale availableLocaleIdentifiers] objectAtIndex:72]; // get the selected locale and the application locale NSLocale *selectedLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier]; NSLocale *appLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // country code and name (in app locale) NSString *countryCode = [selectedLocale objectForKey:NSLocaleCountryCode]; NSString *countryName = [appLocale displayNameForKey:NSLocaleCountryCode value:countryCode]; // symbol and currency code NSString *localCurrencySymbol = [selectedLocale objectForKey:NSLocaleCurrencySymbol]; NSString *currencyCode = [selectedLocale objectForKey:NSLocaleCurrencyCode]; NSString *title = [NSString stringWithFormat:@"%@ %@: %@ (%@)", countryCode, countryName, localCurrencySymbol, currencyCode]; [appLocale release]; NSLog(@"title = %@",title); [p release]; } 

This causes the following commands to be written to the console:

 2012-06-09 06:01:08.299 Untitled 2[11668:707] title = ES Spain: € (EUR) 
+5
source

I think your problem here is that you expect componentsFromLocaleIdentifer return locale information. Instead, it returns string information, which is passed as an identifier. Although you can get NSLocalCurrencySymbol, it will only be present when the line you are passing in has an override for a specific currency (which will never happen in your case, since you are only using a standard array). An example of this in the wild would be a user who has set up the FR system, but with the USD currency.

Generally, you do not need to use -displayNameForKey:value: for NSLocaleCurrencyCode and NSLocaleCurrencySymbol , since they return international strings, not localized strings. That way, as soon as you have your preferred local, you can get this information simply by using -objectForKey:

The hard part in my testing is that assuming that the locale on the list is sufficient to create the correct currency code and the character is not true, you need to have a language and country code instead. Fortunately, +[NSLocale canonicalLanguageIdentifierFromString:] will provide you with the desired language, which can then be added to the country code (after _ ) to create a country / language string that will accordingly extract the currency information.

Here is my revised code:

 myCountryCode = [[NSLocale ISOCountryCodes] objectAtIndex:row]; appLocale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"]; identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: myCountryCode forKey: NSLocaleCountryCode]]; myDictionary = [NSLocale componentsFromLocaleIdentifier: identifier]; myCountryName = [appLocale displayNameForKey:NSLocaleCountryCode value:[myDictionaryobjectForKey:NSLocaleCountryCode]]; // Create the currency language combination and then make our locale NSString *currencyLocaleLanguage= [NSLocale canonicalLanguageIdentifierFromString: myCountryCode]; NSString *countryLanguage= [NSString stringWithFormat: @"%@_%@", myCountryCode, currencyLocaleLanguage]; NSLocale *currencyLocale = [[NSLocale alloc] initWithLocaleIdentifier: countryLanguage]; localCurrencySymbol = [currencyLocale objectForKey:NSLocaleCurrencySymbol]; currencyCode = [currencyLocale objectForKey:NSLocaleCurrencyCode]; title = [NSString stringWithFormat:@"%@ %@: %@ (%@)", myCountryCode, myCountryName, localCurrencySymbol, currencyCode]; [currencyLocale release]; [appLocale release]; 
+5
source

All Articles