How to get NSLocale from currency code?
Asked Answered
E

4

7

I am trying this:

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

It creates NSLocale object but it does`t contain any regional information. For example,

[localeForDefaultCurrency objectForKey:NSLocaleCountryCode];

returns nil;

Any idea how to get NSLocale from currency code?

Evslin answered 2/8, 2011 at 7:52 Comment(2)
I found a hint. Currency code consists of three letters and generally first and second letter is the country code. For example "USD" -> "US". Using country code the code above for some reason works. I am not 100% sure if it works in any cases.Evslin
I'm not sure this principle works for all currencies ;)Recitativo
E
8

As posted by some Guest in pastebin:

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

[localeForDefaultCurrency objectForKey:NSLocaleCountryCode];
Electrify answered 9/6, 2013 at 0:31 Comment(0)
S
6
    NSDictionary *localeInfo = @{NSLocaleCurrencyCode: currencyCode, NSLocaleLanguageCode: [[NSLocale preferredLanguages] objectAtIndex:0]};

    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:
                        [NSLocale localeIdentifierFromComponents:localeInfo]];
Submariner answered 23/12, 2013 at 18:34 Comment(1)
In my case I had both currency and country code. Works like a charm, thanks.Hit
R
5

There are tricks like creating locale with id but instead of normal locale id like "en_US" pass it currency code "USD", "EUR", etc., It seems to work for eur and usd in a way I could check it but this is wrong way IMHO.

The only right way I know is to get all locales and then in loop check their currency codes to compare with one you have. This way you will 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;
}
Recitativo answered 20/7, 2012 at 8:31 Comment(1)
What about cases where a currency is used in multiple countries?Hit
K
1

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
    }

}

In this way, 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)")
}
Krause answered 11/3, 2016 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.