【问题标题】:Get currency ISO 4217 code based on locale根据区域设置获取货币 ISO 4217 代码
【发布时间】:2011-05-16 23:50:13
【问题描述】:

说,如果我用Locale::acceptFromHttp 解析 HTTP Accept-Language 标头,是否有一种简单可靠的方法可以根据此区域设置标识符获取用户的首选货币?比如“en-US”的“USD”。

我希望有一种方法可以使用 PHP intl extension 做到这一点,但到目前为止无法在手册中找到我的答案。我看到 Zend Framework 可以用 Zend_Currency 做到这一点,但它对于我的特定软件来说太臃肿了。

任何其他库或实现此目的的方法?由于必须有很多语言环境标识符,因此简单的switch 有点过头了。

【问题讨论】:

    标签: php intl


    【解决方案1】:

    有点晚了,但你可以通过\NumberFormatter得到它:

    <?php
    
    $currencyPerLocale = array_reduce(
        \ResourceBundle::getLocales(''),
        function (array $currencies, string $locale) {
            $currencies[$locale] = \NumberFormatter::create(
                $locale,
                \NumberFormatter::CURRENCY
            )->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
    
            return $currencies;
        },
        []
    );
    

    【讨论】:

      【解决方案2】:

      您可以在 PHP 4 和 PHP 5 中使用 setlocale()localeconv() 执行此操作:

      $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
      setlocale(LC_MONETARY, $locale);
      
      print_r(localeconv());
      

      样本输出:

      Array
      (
          [decimal_point] => .
          [thousands_sep] =>
          [int_curr_symbol] => EUR
          [currency_symbol] => €
          [mon_decimal_point] => ,
          [mon_thousands_sep] =>
          [positive_sign] =>
          [negative_sign] => -
          [int_frac_digits] => 2
          [frac_digits] => 2
          [p_cs_precedes] => 1
          [p_sep_by_space] => 1
          [n_cs_precedes] => 1
          [n_sep_by_space] => 1
          [p_sign_posn] => 1
          [n_sign_posn] => 2
          [grouping] => Array
              (
              )
      
          [mon_grouping] => Array
              (
                  [0] => 3
                  [1] => 3
              )
      
      )
      

      ISO 4217 代码包含在结果数组的 int_curr_symbol 键中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-14
        • 2014-12-31
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多