【问题标题】:Get Currency Symbol and Currency Code based on ISOCountryCode根据 ISOCountryCode 获取货币符号和货币代码
【发布时间】:2012-06-12 18:31:31
【问题描述】:

像我这样的本地化新手也有类似的问题,但我找不到一个对我有用的问题。

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

我做了以下在我得到表格行的意义上不起作用 美国 美国: (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 国家代码,我们如何打印(在应用程序区域设置中)该特定国家的货币符号和货币代码。

【问题讨论】:

  • 您能否更具体地了解“不起作用”?它是否崩溃,您是否得到了您不期望的东西的列表,条目是否太少?不要让我们猜测。
  • 谢谢。我更新了我的帖子。本质上的问题是,一旦我们有了 ISO 国家代码,我们如何打印(在应用程序区域设置中)该特定国家的货币符号和货币代码。

标签: ios objective-c iso


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    尝试以下测试应用并根据需要进行调整

    #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];是我的钥匙。谢谢。
    • 有没有办法获得货币代码的描述性名称?就像 INR 代表“印度卢比”,USD 代表“美元”等等。
    • 要获取货币名称,您可以使用以下代码: NSLocale *locale = [NSLocale currentLocale]; for (NSString *code in [NSLocale ISOCurrencyCodes]) { NSLog(@"%@ : %@", code, [locale displayNameForKey:NSLocaleCurrencyCode value:code]); }
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-12-07
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多