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

Mig*_*use 5 iso objective-c ios

在本地化方面,像我这样的新手也有类似的问题,但我找不到一个可以解决这个问题的问题.

这是我的问题.我们可以在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];
Run Code Online (Sandbox Code Playgroud)

(上面的标识符,myCountryCode,myCountryName,localCurrencySymbol,currencyCode和title都是NSString指针.而且myDictionary是一个NSDictionary指针,appLocale是一个NSLocale指针).基本上上面的代码将在pickerview中,我想在运行中生成每一行的标题.

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

Ant*_*ick 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);
Run Code Online (Sandbox Code Playgroud)

打印.

locale JPY currency ¥
Run Code Online (Sandbox Code Playgroud)


Flu*_*imp 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];
}
Run Code Online (Sandbox Code Playgroud)

这会将以下内容记录到控制台:

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

  • 要获取货币名称,您可以使用以下代码:NSLocale*locale = [NSLocale currentLocale]; for(NSString*[NSLocale ISOCurrencyCodes]中的代码){NSLog(@"%@:%@",代码,[locale displayNameForKey:NSLocaleCurrencyCode value:code]); } (2认同)

gai*_*ige 5

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

一般情况下,你不应该需要使用-displayNameForKey:value:NSLocaleCurrencyCodeNSLocaleCurrencySymbol,因为他们都返回国际字符串,而不是本地化的字符串.因此,一旦您拥有首选本地,您应该只需使用即可获得此信息-objectForKey:.

在我的测试中,棘手的部分是假设列表中的区域设置足以创建有效的货币代码并且符号不正确,而是需要有语言和国家/地区代码.幸运的是,+[NSLocale canonicalLanguageIdentifierFromString:]它将为您提供正确的语言,然后您可以将其附加到_国家/ 地区代码(在a之后)以创建将适当地导致检索货币信息的国家/语言字符串.

这是我修改后的代码:

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];
Run Code Online (Sandbox Code Playgroud)