Reputation: 13818
There are similar questions to this one from newbies like me in localization, but I couldn't find one that does the trick for me.
Here is my problem. We can get all the ISO country codes in an NSArray with a statement of the form [NSLocale ISOCountryCodes]
. Now, for each and every country of those I would like to print the local currency as well as the currency code used in that country. What would be the appropriate way of doing this?
I did the following that does not work in the sense that I get lines of the form US United States: (null) ((null)) when instead I would like to get lines of the form US United States: $ (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 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 a pickerview where I want to generate the title of each line on the fly.
Thank you very much for your time. Essentially the question is once we have the ISO country code how can we print (in the application locale) the currency symbol and the currency code for that specific country.
Upvotes: 5
Views: 11334
Reputation: 2744
For anyone just wanting to get the currency code from the 3 letter iso code ( commonISOCurrencyCodes ). You can simply do this.
NSString *localeId = @"JPY"; //[[NSLocale commonISOCurrencyCodes] objectAtIndex:1];
NSLocale *locale = [NSLocale currentLocale];
NSString *currency = [locale displayNameForKey:NSLocaleCurrencySymbol
value:localeId];
NSLog(@"locale %@ currency %@", localeId, currency);
Prints.
locale JPY currency ¥
Upvotes: 10
Reputation: 17481
I think your problem here is that you are expecting that componentsFromLocaleIdentifer
will return information about the locale. Instead, it returns information about the string that is passed in as the identifier. Although you can receive NSLocalCurrencySymbol, it will only be present when the string that you are passing in has an override for the particular currency (which will never happen in your case, since you are only using the standard array). An example of this in the wild would be a user who has set up a FR system, but with a USD currency.
Generally, you shouldn't need to use -displayNameForKey:value:
for the NSLocaleCurrencyCode
and NSLocaleCurrencySymbol
, since they both return international strings, not localized strings. Thus, once you have the preferred local, you should be able to get this information just by using -objectForKey:
.
The tricky part, in my testing, is that assuming that the locale in the list is sufficient to create a valid currency code and symbol isn't true, instead you need to have a language and country code. Fortunately, +[NSLocale canonicalLanguageIdentifierFromString:]
will provide you the right language, which you can then append to the country code (after a _
) to create the country/language string that will appropriately result in the currency information being retrieved.
Here's 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];
Upvotes: 5
Reputation: 9185
Try the following test app and adapt as needed
#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's 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 logs the following to the console:
2012-06-09 06:01:08.299 Untitled 2[11668:707] title = ES Spain: € (EUR)
Upvotes: 5