根据ISO国家代码获取货币符号和货币代码

5

有像我一样的新手在本地化方面有类似的问题,但我找不到适合我的解决方法。

这是我的问题。我们可以使用形如[NSLocale ISOCountryCodes]的语句获取NSArray中的所有ISO国家代码。现在,对于每个国家,我想打印出本地货币以及该国家使用的货币代码。应该如何适当地做到这一点?

我尝试了以下方法,但不起作用,因为我得到的行是 US United States: (null) ((null)), 而我想要的是 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];

(上述标识符,myCountryCode,myCountryName,localCurrencySymbol,currencyCode和title都是NSString指针。此外,myDictionary是一个NSDictionary指针,appLocale是一个NSLocale指针)。本质上,上述代码将在pickerview中,我希望动态生成每行的标题。
非常感谢您的时间。本质上问题是,一旦我们有了ISO国家代码,如何打印(在应用程序语言环境中)特定国家的货币符号和货币代码。

1
你能更具体地说明“不起作用”是什么意思吗?它会崩溃吗?你是否得到了一些意外的列表?或者条目太少了?请不要让我们猜测。 - gaige
谢谢。我更新了我的帖子。本质上问题是,一旦我们有了ISO国家代码,如何打印(在应用程序语言环境中)该特定国家的货币符号和货币代码。 - MightyMouse
3个回答

10

如果您只想从三个字母的ISO代码(commonISOCurrencyCodes)获取货币代码,您可以简单地执行此操作。

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

打印。

locale JPY currency ¥

5
尝试下面的测试应用程序,并根据需要进行调整。
#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];
}

这将以下信息记录到控制台:

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

NSString *localCurrencySymbol = [selectedLocale objectForKey:NSLocaleCurrencySymbol]; 和 NSString *currencyCode = [selectedLocale objectForKey:NSLocaleCurrencyCode]; 对我来说是关键。谢谢。 - Joshua Dance
有没有一种方法可以获取货币代码的描述性名称?比如INR代表“印度卢比”,USD代表“美元”等等。 - Vishal Chaudhry
2
获取货币名称,您可以使用以下代码:NSLocale *locale = [NSLocale currentLocale]; for (NSString *code in [NSLocale ISOCurrencyCodes]) { NSLog(@"%@ : %@", code, [locale displayNameForKey:NSLocaleCurrencyCode value:code]); } - invoodoo

5
我觉得你的问题在于你期望componentsFromLocaleIdentifer返回有关语言环境的信息。实际上,它返回的是传入的标识符字符串的信息。虽然你可以收到NSLocalCurrencySymbol,但只有在你传入的字符串具有特定货币的覆盖时才会出现(在你的情况下永远不会发生,因为你只使用标准数组)。这在实际应用中的一个例子是用户设置了FR系统,但使用USD货币。
通常情况下,你不需要使用-displayNameForKey:value:来获取NSLocaleCurrencyCodeNSLocaleCurrencySymbol,因为它们都返回国际化字符串而不是本地化字符串。因此,一旦你获得了首选语言环境,你就可以通过使用-objectForKey:来获取这些信息。
在我的测试中,棘手的部分是假设列表中的语言环境足以创建有效的货币代码和符号,这是不正确的,相反,你需要有一个语言和国家代码。幸运的是,+[NSLocale canonicalLanguageIdentifierFromString:]将为你提供正确的语言,你可以将其附加到国家代码后面(在_之后),以创建适当的国家/语言字符串,从而恰当地检索货币信息。
以下是我修改后的代码:
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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接