2012-06-08 169 views
4

在本地化中,像我这样的新手也有类似的问题,但我找不到一个能帮我实现这个技巧的人。根据ISOCountryCode获取货币符号和货币代码

这里是我的问题。我们可以通过表格[NSLocale ISOCountryCodes]的声明获取NSArray中的所有ISO国家代码。现在,我希望打印当地货币的每个国家以及该国使用的货币代码。这样做的适当方式是什么?

我也不会在这个意义上,我得到的形式 美国线条美的工作如下:(空)((空)) 的时候,而不是我想获得的形式 和美的系美国:$(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和标题都是NSString的指针,而且myDictionary是一个NSDictionary指针和AppLocale会是一个NSLocale指针。)。基本上,上面的代码将在pickerview中,我想要即时生成每行的标题。

非常感谢您的时间。基本上问题是,一旦我们拥有ISO国家代码,我们如何打印(在应用程序区域中)该特定国家的货币符号和货币代码。

+1

你能具体谈谈“不工作”?它是否会崩溃,你有没有预料到的事情清单,是否有太少的条目?不要让我们猜测。 – gaige

+0

谢谢。我更新了我的帖子。基本上问题是,一旦我们拥有ISO国家代码,我们如何打印(在应用程序区域中)该特定国家的货币符号和货币代码。 – MightyMouse

回答

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) 
+0

的NSString * localCurrencySymbol = [selectedLocale objectForKey:NSLocaleCurrencySymbol]; 和 的NSString * CURRENCYCODE = [selectedLocale objectForKey:NSLocaleCurrencyCode];是我的钥匙。谢谢。 –

+0

有没有办法让一个货币代码的描述性名称?像印度卢比代表“印度卢比”,美元代表“美元”等。 –

+0

,我不知道任何可可API本的... – FluffulousChimp

5

我觉得这里的问题是,你期待的是componentsFromLocaleIdentifer将返回信息关于语言环境。相反,它返回有关作为标识符传入的字符串的信息。虽然您可以接收NSLocalCurrencySymbol,但只有当您传入的字符串具有特定货币的覆盖(这种情况绝不会发生在您的情况中,因为您只使用标准数组)时才会出现。这种情况的一个例子是建立了一个FR系统但用美元货币的用户。

通常,您不应使用-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]; 
9

对于任何人只是想获得由3个字母的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 ¥