如何从货币代码获取NSLocale?

发布于 2024-11-27 13:50:00 字数 496 浏览 1 评论 0原文

我正在尝试这个:

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

它创建 NSLocale 对象,但它不包含任何区域信息。例如,

[localeForDefaultCurrency objectForKey:NSLocaleCountryCode];

返回nil;

知道如何从货币代码获取 NSLocale 吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

养猫人 2024-12-04 13:50:00

正如某些来宾在pastebin 中发布的:

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

[localeForDefaultCurrency objectForKey:NSLocaleCountryCode];

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];
無心 2024-12-04 13:50:00
    NSDictionary *localeInfo = @{NSLocaleCurrencyCode: currencyCode, NSLocaleLanguageCode: [[NSLocale preferredLanguages] objectAtIndex:0]};

    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:
                        [NSLocale localeIdentifierFromComponents:localeInfo]];
    NSDictionary *localeInfo = @{NSLocaleCurrencyCode: currencyCode, NSLocaleLanguageCode: [[NSLocale preferredLanguages] objectAtIndex:0]};

    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:
                        [NSLocale localeIdentifierFromComponents:localeInfo]];
Smile简单爱 2024-12-04 13:50:00

有一些技巧,例如使用 id 创建区域设置,但不是像“en_US”这样的正常区域设置 id,而是传递货币代码“USD”、“EUR”等,它似乎适用于欧元和美元,我可以检查它,但是这个恕我直言,这是错误的方式。

我知道的唯一正确的方法是获取所有语言环境,然后循环检查它们的货币代码以与您拥有的货币代码进行比较。这样,当您找到代码时,您将停止循环。

- (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;
}

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;
}
最单纯的乌龟 2024-12-04 13:50:00

您可以在 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
    }

}

这样,在代码中的任何位置:

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

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)")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文