根据语言环境获取货币ISO 4217代码 [英] Get currency ISO 4217 code based on locale
问题描述
说,如果我使用 Locale::acceptFromHttp
是否有一种简单可靠的方法来基于此区域设置标识符获取用户的首选货币?就像美国"中的美元"一样.
Say, if I parse HTTP Accept-Language header with Locale::acceptFromHttp
is there an easy and reliable way to get user's preferred currency based on this locale identifier? Like "USD" for "en-US".
我希望有一种方法可以使用PHP intl扩展但到目前为止,在手册中找不到我的答案.我看到Zend Framework可以通过Zend_Currency做到这一点,但对于我的特定软件来说太过膨胀了.
I wish there was a way to do this with PHP intl extension but so far was unable to find my answer in the manual. I saw Zend Framework can do this with Zend_Currency but it's just too bloated for my particular software.
还有其他实现此目的的库或方法吗?由于必须有很多区域设置标识符,因此简单的switch
有点过分.
Any other libs or ways of achieving this? Since there must be a lot of locale identifiers a simple switch
is a bit of overkill.
推荐答案
有点晚了,但是您可以使用\NumberFormatter
来获取它:
A bit late but you can get it with \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;
},
[]
);
这篇关于根据语言环境获取货币ISO 4217代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!