根据ISOCountryCode获取货币符号和货币代码

时间:2012-06-08 17:14:39

标签: ios objective-c iso

在本地化方面,像我这样的新手也有类似的问题,但我找不到能帮我解决问题的问题。

这是我的问题。我们可以在NSArray中获取所有ISO国家/地区代码,其中包含[NSLocale ISOCountryCodes]形式的声明。现在,对于我想要打印当地货币以及该国家使用的货币代码的每个国家/地区。这样做的恰当方法是什么?

我执行了以下操作,因为我得到了表单的行 美国:( null)((null)) 相反,我希望获得表格的线条 美国:$(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国家/地区代码,我们如何打印(在应用程序区域设置中)该特定国家/地区的货币符号和货币代码。

3 个答案:

答案 0 :(得分:10)

对于任何想要从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 ¥

答案 1 :(得分: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)

答案 2 :(得分:5)

我认为您的问题是,您希望componentsFromLocaleIdentifer将返回有关区域设置的信息。相反,它返回有关作为标识符传入的字符串的信息。虽然您可以接收NSLocalCurrencySymbol,但只有当您传入的字符串具有特定货币的覆盖时才会出现(在您的情况下永远不会发生,因为您只使用标准数组)。在野外的一个例子是用户已经设置了FR系统,但是使用了美元货币。

通常,您不需要对-displayNameForKey:value:NSLocaleCurrencyCode使用NSLocaleCurrencySymbol,因为它们都返回国际字符串,而不是本地化字符串。因此,一旦您拥有首选本地,您应该只能使用-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];
相关问题